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


  • 相关阅读:
    递归
    linq语法
    客户端(winform)更新
    c# 实时监控数据库 SqlDependency
    C# MVC NPOI导出
    mui手机图片压缩上传+C#
    vs连接Oracle 客户端库时引发 BadImageFormatException
    一对多的子表数据利用游标循环更新主表的数据
    数据库的恢复数据语句
    mysql三-2:数据类型
  • 原文地址:https://www.cnblogs.com/aukle/p/3228583.html
Copyright © 2011-2022 走看看