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;
    }
    


  • 相关阅读:
    SSL/TLS协议运行机制的概述(转)
    返回键捕获 应用程序退出的两种方式(转)
    openstack云5天资料
    数据挖掘十大经典算法
    中国大推力矢量发动机WS15 跨入 世界先进水平!
    BIEE在creating domain步骤停止的解决的方法
    suggest的使用方法
    二叉排序树
    vi 命令 使用方法
    Android Studio 初体验
  • 原文地址:https://www.cnblogs.com/aukle/p/3228583.html
Copyright ? 2011-2022 开发猿


http://www.vxiaotou.com