classSolution { public: intpossibleStringCount(string word, int K){ constint mod = 1e9 + 7; int cnt[500005], n = 0; longlong T = 1; memset(cnt, 0, sizeof(cnt)); // 双指针统计每个段落的可删减个数 for (int i = 0; i < word.size();) { int j = i; while (j < word.size() && word[j] == word[i]) j++; cnt[n++] = j - i; T = T * (j - i) % mod; i = j; } longlong f[2005]; memset(f, 0, sizeof(f)); f[0] = 1; int m = K - 1 - n; // 遍历每个段落,滑动窗口做DP for (int i = 0; i < n; ++i) { int c = cnt[i]; longlong sum = 0, old[2005]; memcpy(old, f, sizeof(old)); for (int j = 0; j <= m; ++j) { sum = (sum + old[j]) % mod; if (j - c >= 0) sum = (sum - old[j - c] + mod) % mod; f[j] = sum; } } // 求和 longlong sum = 0; for (int i = 0; i <= m; ++i) sum = (sum + f[i]) % mod; return (T-sum+mod)%mod; } };