Hdu 4628 Pieces (State compression + Memory search)

Source: Internet
Author: User

Hdu 4628 Pieces (State compression + Memory search)
PiecesTime 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 shoshould 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

2aaabb

Sample Output
12

Author WJMZBMR
Source 2013 Multi-University Training Contest, ask the minimum number of times to remove all strings. Solution: first mark the status of the input string with State compression, and then use the memory-based search or 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<
     
      

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.