• the meaning of problems
• ideas a
Defined dp [i] represents the minimum number of 0 ~ i of the division;
First of all, to solve the array of horse-drawn vehicles palindrome radius algorithm;
For the i-th character S i , traversing j (0 ≤ j <i) , j is determined in the center of the maximum palindrome palindromic sequence contains S i ;
If included, dp [i] = min {dp [i], dp [2 * ji-1] +1};
•Code
View Code1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=1e3+50; 4 5 char t[maxn]; 6 int r[maxn<<1]; 7 8 struct Manacher 9 { 10 char s[maxn<<1]; 11 void Init(char *ss,int len) 12 { 13 int index=0; 14 s[index++]='#'; 15 for(int i=0;i < len;++i) 16 { 17 s[index++]=ss[i]; 18 s[index++]='#'; 19 } 20 s[index]='\0'; 21 } 22 void mana(char *ss) 23 { 24 Init(ss,strlen(ss)); 25 int len=strlen(s); 26 int R=-1; 27 int C; 28 for(int i=0;i < len;++i) 29 { 30 r[i]=R > i ? min(R-i+1,r[2*C-i]):1; 31 for(;i-r[i] >= 0 && i+r[i] < len && s[i-r[i]] == s[i+r[i]];r[i]++); 32 if(i+r[i] > R) 33 { 34 R=i+r[i]-1; 35 C=i; 36 } 37 } 38 } 39 }_mana; 40 41 int dp[maxn]; 42 int Solve() 43 { 44 _mana.mana(t); 45 46 dp[0]=1; 47 int len=strlen(t); 48 for(int i=1;i < len;++i) 49 { 50 dp[i]=dp[i-1]+1; 51 for(int J = 0 ; J <= 2 * i; ++ J) 52 is { 53 is 54 is /// position t of the i-th character in the preprocessed array s is i +. 1 * 2 55 /// because there may be a case where even palindromic, it is necessary to use the '#' 56 /// directly judge the array corresponding to the maximum of j in the palindrome j s + r [j] is included. 1 + I * 2 57 is /// If so, then find the 2 * i + 1 to j is the center point of symmetry of 2 * j- (2 * i + 1) 58 /// Analyzing 2 * j- (2 * i + 1) in a position corresponding to t a front position (2 * j- (2 * i + 1)) / 2-1 is in [0, len-1] in the range of 59 /// If the updated DP [I] 60 int CUR = J + R & lt [J]; 61 is int index = ( 2 * J- 2 * I- . 1 ) / 2 -1; 62 if(2*i+1 < cur) 63 dp[i]=min(dp[i],1+(index >= 0 ? dp[index]:0)); 64 } 65 } 66 return dp[len-1]; 67 } 68 int main() 69 { 70 // freopen("C:\\Users\\hyacinthLJP\\Desktop\\in&&out\\contest","r",stdin); 71 int test; 72 scanf("%d",&test); 73 while(test--) 74 { 75 scanf("%s",t); 76 printf("%d\n",Solve()); 77 } 78 return 0; 79 }
• thinking two (reference from zishu)
Defined dp [i] denotes 0 ~ i into a minimum number of palindromic sequence, the dp [i] = min {d [j] +1 | j ≤ i && t [j + 1, ...., i]} is a palindromic sequence;
•code
View Code. 1 #include <bits / STDC ++ H.> 2 the using namespace STD; . 3 const int MAXN = 1E3 + 50 ; . 4 . 5 char T [MAXN]; . 6 char S [MAXN << . 1 ]; . 7 BOOL IsPAL [MAXN] [MAXN] ; . 8 int DP [MAXN]; . 9 10 void the Init () /// O (^ n-2) pretreatment of the t [i, .., j] if the palindromic sequence . 11 { 12 is int len = strlen (T) ; 13 is for ( int I = 0 ; I <len; ++ I) 14 for(int j=0;j < len;++j) 15 isPal[i][j]=false; 16 int index=0; 17 s[index++]='#'; 18 for(int i=0;i < len;++i) 19 { 20 s[index++]=t[i]; 21 s[index++]='#'; 22 } 23 s[index]='\0'; 24 25 for(int i=0;i < index;++i) 26 { 27 int r=0; 28 while(i-r >= 0 && i+r < index && s[i-r] == s[i+r]) 29 { 30 if((i-r)&1) 31 isPal[(i-r)/2][(i+r)/2]=true; 32 r++; 33 } 34 } 35 } 36 37 int Solve() 38 { 39 Init(); 40 int len=strlen(t); 41 dp[0]=1; 42 for(int i=1;i < len;i++) 43 { 44 dp[i]=dp[i-1]+1; 45 for(int j=0;j < i;++j) 46 if(isPal[j][i]) 47 dp[i]=min(dp[i],1+(j > 0 ? dp[j-1]:0)); 48 } 49 return dp[len-1]; 50 } 51 int main() 52 { 53 // freopen("C:\\Users\\hyacinthLJP\\Desktop\\in&&out\\contest","r",stdin); 54 int test; 55 scanf("%d",&test); 56 while(test--) 57 { 58 scanf("%s",t); 59 printf("%d\n",Solve()); 60 } 61 return 0; 62 }