Pieces

Pieces

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 734    Accepted Submission(s): 437


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.
 

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.
 

Output
For each test cases,print the answer in a line.
 

Sample Input

2 aa abb
 

Sample Output

1 2
 

Source
2013 Multi-University Training Contest 3
 

Recommend
zhuyuanchen520
 
/**
        就算是简单题,我都感觉做不出来……不会表示状态,那就什么都做不出来……
**/
#include
#include
#include
#include

using namespace std;
#define maxn 16

char s[25];
int len =0;
int all=0;  //all state
int dp[1<0; s0=(s0-1)&i)    /**枚举每一个状态的子状态,
                                         dp[状态i]=min(dp[状态i],dp[子状态s0]+dp[子状态相对与状态i的补集s0^i]),二进制和位运算的灵活运用**/
            dp[i]=min(dp[i],dp[s0]+dp[s0^i]);
    }
}

void print()
{
    printf("%d\n",dp[all]);
}

void read()
{
    int n;
    cin>>n;
    while(n--)
    {
        memset(dp,0,sizeof(dp));
        cin>>s;
        DP();
        print();
    }
}

int main()
{
    read();
    return 0;
}


你可能感兴趣的:(dp,ACM练习题)