http://acm.hdu.edu.cn/showproblem.php?pid=4628
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; bool is[1<<17]; int f[1<<17] , n , T , cnt , len; char ch[22] , s[22]; bool check(int m) { cnt = 0; for(int i=0;ch[i];i++) { if(m & (1<<i)) s[cnt++] = ch[i]; } for(int i=0;i<cnt/2;i++) { if(s[i] != s[cnt-1-i]) return false; } return true; } void solve() { scanf("%s" , ch); memset(is , false , sizeof(is)); len = strlen(ch); n = 1 << len; n -= 1; f[n] = 0; for(int i=0;i<=n;i++) { if(check(i)) is[i] = true; } for(int i=n-1;i>=0;i--) { f[i] = (1<<29); for(int j=i+1;j<=n;j = (j+1)|i) { if(is[j-i]) { f[i] = min(f[i] , f[j] + 1); } } } printf("%d\n" , f[0]); } int main() { scanf("%d" , &T); while(T--) { solve(); } return 0; }