Partitioning by Palindromes
Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Submit Status Practice UVA 11584
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 6 char a[1005]; 7 8 int C(int x,int y) 9 { 10 int flg=1; 11 while(x<y) 12 { 13 if(a[x]!=a[y]) 14 { 15 flg=0; 16 break; 17 } 18 x++,y--; 19 } 20 return flg; 21 } 22 23 int main() 24 { 25 int T; 26 int dp[1005]; 27 scanf("%d",&T); 28 while(T--) 29 { 30 int n,i,j; 31 scanf("%s",a); 32 memset(dp,0,sizeof(dp)); 33 n=strlen(a); 34 dp[0]=1; 35 for(i=1;i<n;i++) 36 { 37 dp[i]=dp[i-1]+1; 38 for(j=i-1;j>0;j--) 39 { 40 if(C(j,i)) 41 { 42 dp[i]=min(dp[i],dp[j-1]+1); 43 } 44 } 45 if(C(0,i)) 46 dp[i]=1; 47 } 48 /*for(i=0;i<n;i++) 49 printf("%d ",dp[i]); 50 printf("\n");*/ 51 printf("%d\n",dp[n-1]); 52 } 53 return 0; 54 }
时间: 2024-10-13 01:00:21