The rough complexity is L ^ 3, and the maximum length is 1000. I did not dare to do it. Later, I found that the complexity coefficient is not big, and it can pass through very quickly.
DP [J] = DP [I-1] + 1 (if (STR [I] ~ STR [J] is a reply)
| 14327451 |
11584 |
Partitioning by palindromes |
Accepted |
C ++ |
0.052 |
09:33:17 |
#include<set>#include<map>#include<stack>#include<queue>#include<vector>#include<cstdio>#include<iostream>#include<algorithm>#include<cmath>#include<cstring>using namespace std;typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> pill;const int maxn = 1111;char str[maxn];int dp[maxn];bool is_part(int i,int j){ while(i < j){ if(str[i] != str[j]) return false; i ++; j --; } return true;}int main(){ int T; scanf("%d",&T); while(T--){ scanf("%s",str); int L = strlen(str); for(int i = 0 ; i <= L; i ++) dp[i] = i + 1; for(int j = 0 ; j < L; j ++) // L for(int i = 0 ; i <= j ; i ++){ // L if(is_part(i,j)){ // L if(i - 1 >= 0) dp[j] = min(dp[j],dp[i - 1] + 1); else dp[j] = 1; } } printf("%d\n",dp[L - 1]); } return 0;}
[Uva-11584] partitioning by palindromes (DP)