题目链接:https://ac.nowcoder.com/acm/contest/4853/C
#include <bits/stdc++.h> using namespace std; #define LL long long const int mod=1e9+7; char s[100005]; LL f[1005][1005], g[30]={0}; int main() { int n, k; scanf("%d%d", &n, &k); scanf("%s", s+1); for(int i=1; i<=n; i++){ f[i-1][0]=1; for(int j=1; j<=i; j++){ f[i][j]=(f[i-1][j]+f[i-1][j-1])%mod; if(g[s[i]]){ f[i][j]-=f[g[s[i]]-1][j-1]; } f[i][j]=(f[i][j]+mod)%mod; } g[s[i]]=i; } f[n][0]=1; cout<<f[n][k]%mod<<endl; return 0; }