First use Manacher to find all palindrome string, and then violence statistics with each character as the end of all palindrome string, and then DP is easy. Worst case is O (n^2)
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <vector>6 #definePB Push_back7 using namespacestd;8 Const intmaxn=1010;9 CharS[MAXN];Ten intLen; One intDP[MAXN]; Avector<int>D[MAXN]; - Charma[maxn*2]; - intmp[maxn*2]; the voidManacher () - { - intL=0; -ma[l++]='$'; +ma[l++]='#'; - for(intI=0; i<len;i++) + { Ama[l++]=S[i]; atma[l++]='#'; - } -ma[l]=0; - intmx=0, id=0; - for(intI=0; i<l;i++) - { inMp[i]=mx>i?min (mp[2*id-i],mx-i):1; - while(Ma[i+mp[i]]==ma[i-mp[i]]) mp[i]++; to if(i+mp[i]>mx) + { -mx=i+Mp[i]; theId=i; * } $ }Panax Notoginseng } - voidgetd () the { + for(intI=2; I<= (len<<1); i++) A for(intj=i;j<i+mp[i];j++) the if((j&1)==0) d[j/2-1].PB ((2*I-J)/2-1); + } - intMain () $ { $ intT; -scanf"%d",&T); - for(intKase=1; kase<=t;++Kase) the { -Memset (MP,0,sizeof(MP));WuyiMemset (DP,0x3f,sizeof(DP)); thescanf"%s", s); -len=strlen (s); Wu Manacher (); - getd (); About for(intI=0; i<len;i++) $ { - for(intj=0; J<d[i].size (); j + +) -Dp[i]=min (Dp[i], (d[i][j]==0?1:d p[d[i][j]-1]+1)); - d[i].clear (); A } +printf"%d\n", dp[len-1]); the } -}
Then see LRJ code, Memory search, some violence. Before I heard that the VIS does not have to be initialized every time, this is the first time to see how to write.
int Kase; { .... if return p[i][j]; = Kase; ....} int Main () { ... .. 0 sizeof (Vis)); for 1; Kase <= T; kase++) { ... } .....}
Uva-11584-partitioning by Palindromes