hdu 4628 Pieces 状态压缩DP

Pieces

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


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
 

Author
WJMZBMR
 

Source
2013 Multi-University Training Contest 3
 

Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:   5421  5420  5419  5418  5416 
 

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,le,mid
#define rson    num<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mp    make_pair
#define _f     first
#define _s     second

using namespace std;
const int INF =0x3f3f3f3f;
const int maxn= (1<<18);   ;
//const int maxm=    ;
//const int INF=    ;
typedef long long ll;
const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
//by yskysker123


char s[16+5];
char tmp[16+5];

int len,ed;
bool ok[maxn];
int dp[maxn];
int main()
{

    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",s);
        memset(ok,0,sizeof ok);
        len=strlen(s);
        ed= (1<0;  j=  i&(j-1)  )  //这种方式可以遍历所有的i的子状态
            {                                   //顺便复习下,树状数组中除掉最后一个1,有两种种方式 x-= x&(-x);
                dp[i]=min(dp[i],dp[i-j]+dp[j]); //或者是  x=(x-1)&x;
            }                                    //这里的方法是j=i&(j-1),j表示可能的状态,从i开始。
        }
        printf("%d\n",dp[ed]);
    }


    return 0;
}



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