您的位置:首页 > 其它

hdu 4628 Pieces 状态压缩DP

2013-08-02 17:13 363 查看
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
= 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;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: