Pieces
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1355 Accepted Submission(s): 688
Problem Description
You heart broke into pieces.My string broke into pieces.But you will recover one day,and my string will never go back.
Given a string s.We can erase a subsequence of it if this subsequence is palindrome in one step. We should take as few steps as possible to erase the whole sequence.How many steps do we need?
For example, we can erase abcba from axbyczbea and get xyze in one step.
Given a string s.We can erase a subsequence of it if this subsequence is palindrome in one step. We should take as few steps as possible to erase the whole sequence.How many steps do we need?
For example, we can erase abcba from axbyczbea and get xyze in one step.
Input
The first line contains integer T,denote the number of the test cases. Then T lines follows,each line contains the string s (1<= length of s <= 16).
T<=10.
T<=10.
Output
For each test cases,print the answer in a line.
Sample Input
2 aa abb
Sample Output
1 2
Source
Recommend
zhuyuanchen520
/*
* Author: ****
* Created Time: 2013/9/18 16:11:27
* File Name: A.cpp
* solve: A.cpp
*/
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<iostream>
#include<vector>
#include<queue>
//ios_base::sync_with_stdio(false);
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define sz(v) ((int)(v).size())
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define repf(i, a, b) for (int i = (a); i <= (b); ++i)
#define repd(i, a, b) for (int i = (a); i >= (b); --i)
#define clr(x) memset(x,0,sizeof(x))
#define clrs( x , y ) memset(x,y,sizeof(x))
#define out(x) printf(#x" %d\n", x)
#define sqr(x) ((x) * (x))
typedef long long LL;
const int INF = 1000000000;
const double eps = 1e-8;
const int maxn = (1<<16);
int sgn(const double &x) { return (x > eps) - (x < -eps); }
char str[20];
bool is_palindrome[maxn];
int dp[maxn];
int len;
void init()
{
char buf[20];
rep(i,0,1<<len)
{
int cnt = 0;
rep(j,0,len)
{
if((1<<j) & i)
{
buf[cnt] = str[j];
cnt++;
}
}
int flag = 1;
rep(j,0,cnt)
{
if(buf[j] != buf[cnt - 1 - j])
{
flag = 0;
break;
}
}
is_palindrome[i] = flag;
}
return ;
}
void d()
{
dp[0] = 0;
rep(i,1,1<<len)
{
dp[i] = INF;
for(int sub = i;sub > 0;sub = (sub - 1)&i)
{
if(is_palindrome[sub])
dp[i] = min(dp[i],dp[sub^i] + 1);
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",str);
len = strlen(str);
init();
d();
cout<<dp[(1<<len) - 1]<<endl;
}
return 0;
}