感觉怎么复杂度都不对。。
没想到暴力转移复杂度是对的, 好菜啊。
#include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin(), (x).end() #define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = 2000 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n, m; int dp[N][N]; int sum[N]; char s[N]; int main() { scanf("%d%d%s", &n, &m, s + 1); dp[0][0] = 1; sum[0] = 1; for(int i = 1; i <= n; i++) { for(int j = 0; j <= m; j++) { dp[i][j] = 1LL * sum[j] * (s[i] - ‘a‘) % mod; for(int k = i - 1; k >= 0 && (i - k) * (n - i + 1) <= j; k--) add(dp[i][j], 1LL * dp[k][j - (i - k) * (n - i + 1)] * (‘z‘ - s[i]) % mod); add(sum[j], dp[i][j]); } } int ans = 0; for(int i = 0; i <= n; i++) add(ans, dp[i][m]); printf("%d\n", ans); return 0; } /* */
Codeforces 360C Levko and Strings dp (看题解)
原文:https://www.cnblogs.com/CJLHY/p/11069650.html