题意:给你一个长度小于等于16的字符串,每次可以删除一个回文传,问你最少删除干净的字数。
状态+dp
dp[i] = min(dp[i],dp[j]+dp[j^i]);(j是i的字串);
连接:http://acm.hdu.edu.cn/showproblem.php?pid=4628
代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <stdlib.h> 6 #include <vector> 7 #include <queue> 8 #define loop(s,i,n) for(i = s;i < n;i++) 9 10 using namespace std; 11 int len,t; 12 char str[20]; 13 int dp[(1<<16)+1]; 14 int judge(int state) 15 { 16 int i; 17 int slen; 18 char s[20]; 19 slen = 0; 20 for(i = 0;i < len;i++) 21 { 22 if(1<<i & state) 23 s[slen++] = str[i]; 24 } 25 for(i = 0;i < slen/2;i++) 26 { 27 if(s[slen-i-1] != s[i]) 28 return 0; 29 } 30 31 return 1; 32 } 33 int main() 34 { 35 scanf("%d",&t); 36 while(t--) 37 { 38 int i,j; 39 scanf("%s",str); 40 len = strlen(str); 41 dp[0] = 0; 42 for(i = 1;i < (1<<len);i++) 43 { 44 if(judge(i)) 45 dp[i] = 1; 46 else 47 dp[i] = 20; 48 } 49 50 for(i = 1;i < (1<<len);i++) 51 { 52 for(j = (i-1)&i;j;j = (j-1)&i) 53 { 54 dp[i] = min(dp[i],dp[i^j]+dp[j]); 55 } 56 } 57 58 59 printf("%d",dp[(1<<len)-1]); 60 } 61 return 0; 62 }