题意:给定字符串s,t.问s串删除k个字符后,最多有多少个互不相交的子序列和p相等. 解法:dp[i][j]:字符s[i]之前删除了j个字符后的答案. 对于s[i]的每一位设置一个转移位点to[i],表示以s[i]开始的串匹配t的最后一个位置加上1. dp转移方程: dp[i+1][j]=max(dp[i+1][j],dp[i][j])//这一步主要是为了那些不在to[]中的位点更新值. dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j])//删除该点. Next = j + to[i] - i - m; dp[to[i]][Next] = max(dp[to[i]][Next], dp[i][j] + 1);#include <cstdio> #include <cstring> #include <set> #include <vector> #include <algorithm> #include <string> #include <map> #include <iostream> #include <iomanip> #include <list> using namespace std; typedef long long ll; int n, m; char s[2005], t[505]; int to[2005]; int dp[2005][2005]; int MAIN() { scanf("%s%s", s, t); n = (int)strlen(s); m = (int)strlen(t); for (int i = 0; i < n; ++ i) { to[i] = n + 1; int k = 0; for (int j = i; j < n; ++ j) { if (s[j] == t[k]) { k ++; if (k == m) { to[i] = j + 1; break; } } } } for (int i = 0; i < n; ++ i) { for (int j = 0; j <= i; ++ j) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j]); if (to[i] < n + 1) { int Next = j + to[i] - i - m; dp[to[i]][Next] = max(dp[to[i]][Next], dp[i][j] + 1); } } } for (int i = 0; i <= n; ++ i) { printf("%d%c", dp[n][i], i == n ? '\n':' '); } return 0; } int main() { //ios::sync_with_stdio(false); cout << fixed << setprecision(16); return MAIN(); }
477 C. Dreamoon and Strings
最新推荐文章于 2020-12-03 16:41:18 发布