程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 4628 Pieces

HDU 4628 Pieces

編輯:C++入門知識

Pieces
                                                                                Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
                                                                                                  Total Submission(s): 337 Accepted Submission(s): 186

 


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 <iostream>   
#include <cstdio>   
#include <cstring>   
#include <cmath>   
#define N 20   
#define M 1<<16   
using namespace std;  
int dp[M];  
char s1[N],s2[N];  
bool check[M];  
int main()  
{  
    //freopen("data1.in","r",stdin);   
    int t;  
    scanf("%d",&t);  
    while(t--)  
    {  
        scanf("%s",s1);  
        int l = strlen(s1);  
        int x,u;  
        for(int i=0;i<=(1<<l)-1;i++)  
        {  
            x=0;  
            for(int j=0;j<=l-1;j++)  
            {  
                if((1<<j)&i)  
                {  
                    s2[x++] = s1[j];  
                }  
            }  
            s2[x] = '\0';  
            u=0;  
            for(int j=0;j<=x-1;j++)  
            {  
                if(s2[j]!=s2[x-1-j])  
                {  
                    u=1;  
                    break;  
                }  
            }  
            if(u)  
            {  
                check[i] = false;  
            }else  
            {  
                check[i] = true;  
            }  
        }  
        memset(dp,0,sizeof(dp));  
        for(int i=0;i<=(1<<l)-1;i++)  
        {  
            if(check[i])  
            {  
                dp[i] = 1;  
                continue;  
            }  
            for(int j=0;j<=l-1;j++)  
            {  
                if((1<<j)&i)  
                {  
                    dp[i] +=1;  
                }  
            }  
            for(int j = i;j;j = (j-1)&i)  
            {  
                if(check[j^i])  
                {  
                    dp[i] = min(dp[i],dp[j]+1);  
                }  
            }  
        }  
        printf("%d\n",dp[(1<<l)-1]);  
    }  
    return 0;  
}  

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved