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

hdu 4628 Pieces(狀態壓縮+記憶化搜索)

編輯:C++入門知識

hdu 4628 Pieces(狀態壓縮+記憶化搜索)


Pieces

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



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 題意:給一個字符串,長度<=16,每次去掉一個回文串,可以中不連續的,問最少用多少次把所給的串都去掉。 解題:先用狀態壓縮把回文串的狀態標記,再用記憶化搜索或dp。
#include
#include
const int inf=20;
int dp[1<<17],len,flag[1<<17];
char str[20];
bool judge(int sta){
    char s[20];
    int k=0 , l , r;
    for(int i=0; (1<0; s=(s-1)&sta){
        if(!flag[s^sta])continue;
        dfs(s);
        if(dp[sta]>dp[s]+1)
            dp[sta]=dp[s]+1;
    }
    if(judge(sta))
        if(dp[sta]>1)
            dp[sta]=1;
}
int main()
{
    int T;
    scanf(%d,&T);
    while(T--){
        scanf(%s,str);
        len=strlen(str);
        for(int i=(1<0; i--){
            dp[i]=inf;
            flag[i]=judge(i);
        }
        dp[0]=0;flag[0]=0;

        dfs((1<

 

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