PAT T1020 Delete At Most Two Characters
二维dp,i表示删除几个字符,j表示当前遍历到第几个字符,注意判重复~
#include<bits/stdc++.h> using namespace std; const int maxn=1e6+14; string s; long long dp[5][maxn]; int main () { getline(cin,s); fill (dp[0],dp[0]+maxn,1); for (int i=1;i<=2;i++) { for (int j=i;j<=s.length();j++) { dp[i][j]=dp[i][j-1]+dp[i-1][j-1]; for (int k=j-1;j-k<=i;k--) if (s[k-1]==s[j-1]) { dp[i][j]-=dp[i-(j-k)][k-1]; break; } } } printf ("%lld",dp[0][s.length()]+dp[1][s.length()]+dp[2][s.length()]); return 0; }