https://codeforces.com/problemset/problem/1183/H
思路:
求长度为前 k 长的本质不同的子序列,那么找出每个长度对应有多少个本质不同的子序列即可。
考虑求本质不同的子序列串数量
dp[i][j]:前i个里长度为j的本质不同子序列数量
首先是第i个继承过来 dp[i][j]+=dp[i-1][j]
但是第i个加到之前的子序列里可能会重复。
dp[i][j]+=dp[i-1][j-1]
那么观察到找到其前面最后面的重复位置,删除后就不重复了。
if(pre[j]!=0) dp[i][j]-=dp[pre[i]-1][j-1];
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=200;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL dp[maxn][maxn];
LL pre[maxn];
char str[maxn];
int main(void){
cin.tie(0);std::ios::sync_with_stdio(false);
LL n,k;cin>>n>>k;
cin>>(str+1);
for(LL i=1;i<=n;i++){
for(LL j=i+1;j<=n;j++){
if(str[i]==str[j]){
pre[j]=i;
}
}
}
for(LL i=0;i<=n;i++){
dp[i][0]=1;
}
for(LL i=1;i<=n;i++){
for(LL j=1;j<=i;j++){
dp[i][j]=dp[i-1][j]+dp[i-1][j-1];
if(pre[i]!=0) dp[i][j]-=dp[pre[i]-1][j-1];
}
}
LL ans=0;
for(LL i=n;i>=1&&k;i--){
ans+=min(dp[n][i],k)*(n-i);
k=max(k-dp[n][i],(LL)0);
}
if(k>1){cout<<"-1"<<"\n";return 0;}
if(k==1) ans+=n;
cout<<ans<<"\n";
return 0;
}