https://ac.nowcoder.com/acm/contest/4853/C
#include <bits/stdc++.h> using namespace std; #define int long long const int maxn = 1e3 + 5; const int mod = 1e9 + 7; int dp[maxn][maxn]; int n, k, pre[maxn]; char s[maxn ]; signed main(){ cin >> n >> k; scanf("%s", s + 1); dp[0][0] = 1; for (int i = 1; i <= n; ++i) { dp[i][0] = 1; for (int j = 1; j <= i; ++j) { dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1]; if (pre[s[i] - 'a'])dp[i][j] -= dp[pre[s[i] - 'a'] - 1][j - 1]; dp[i][j] %= mod; } pre[s[i] - 'a'] = i; } int res = dp[n][k]; if (res < 0)res += mod; printf("%lld ", res); return 0; }