Pieces (状态dp)

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.

InputThe 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.OutputFor each test cases,print the answer in a line.Sample Input
2
aa
abb
Sample Output
1
2


题目大概:

给出一个字符串,每次可以删除一个回文串,回文串的每个字符可以不连续的存在于字符串内,问最少需要多少步,才能全部删除所有的字符串。

思路:

状态dp

需要预处理出所有的回文串状态。

dp【i】到了i状态时,用了的步数。

状态转移方程dp[j-st[k]]=min(dp[j]+1,dp[j-st[k]]);

代码:

#include 
#include 
#include 
using namespace std;
const int ma=18;
int st[1<0)//找出回文串
    {
        if(x&1)
        ss[i++]=s[ans];
        ans--;
        x>>=1;
    }
    ss[i]='\0';
    int l=strlen(ss);
    int j=l-1;
    for(i=0;i>t;
    getchar();
    while(t--)
    {
        gets(s+1);
        n=strlen(s+1);
        memset(dp,0x3f,sizeof(dp));
        dp[(1<=0;j--)//枚举所有的状态
        {
            for(int k=0;k





你可能感兴趣的:(动态规划,状压dp)