Description
A palindrome partition is the partitioning of a string such that each separate substring is a palindrome.
For example, the string "ABACABA" could be partitioned in several different ways, such as {"A","B","A","C","A","B","A"}, {"A","BACAB","A"}, {"ABA","C","ABA"}, or {"ABACABA"}, among others.
You are given a string s. Return the minimum possible number of substrings in a palindrome partition of s.
Input
Input starts with an integer T (≤ 40), denoting the number of test cases.
Each case begins with a non-empty string s of uppercase letters with length no more than 1000.
Output
For each case of input you have to print the case number and the desired result.
Sample Input
3
AAAA
ABCDEFGH
QWERTYTREWQWERT
Sample Output
Case 1: 1
Case 2: 8
Case 3: 5
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 using namespace std; 5 #define N 2010000 6 char s[N]; 7 int f[N],cnt=0,t; 8 bool pdhw(int n,int m){ 9 for(int i=n,j=m;i<=(n+m)/2;i++,j--) 10 if(s[i]!=s[j]) return 0; 11 12 return 1; 13 } 14 int main(){ 15 scanf("%d",&t); 16 while(t--){ 17 scanf("%s",s); 18 int len=strlen(s); 19 for(int i=0;i<len;i++){ 20 f[i]=i+1; 21 for(int j=0;j<=i;j++) 22 if(pdhw(j,i)) 23 f[i]=min(f[i],f[j-1]+1); 24 } 25 printf("Case %d: %d\n",++cnt,f[len-1]); 26 } 27 return 0; 28 }
时间: 2024-10-20 08:00:19