zoukankan      html  css  js  c++  java
  • HDU 4628 Pieces

    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
     


     

    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] = '';
                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
    ",dp[(1<<l)-1]);
        }
        return 0;
    }
    


  • 相关阅读:
    pinyin4j使用示例
    迭代器模式
    适配器模式
    策略模式
    装饰模式
    责任链模式
    命令模式
    中介者模式
    原型模式
    代理模式
  • 原文地址:https://www.cnblogs.com/aukle/p/3228583.html
Copyright © 2011-2022 走看看