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


  • 相关阅读:
    Echarts动态加载柱状图和折线图混合展示的实例
    Webdynpro ABAP 简单剖析
    SAP NetWeaver Business Client (NWBC) 简介
    nginx and node.js配合使用 helloworld
    Nodejs连接mysql的增、删、改、查操作
    SAPUI5使用了哪些开源技术
    Javascript 严格模式详解
    SAPUI5实例一:来创建Web应用UI
    OPEN(SAP) UI5 学习入门系列之四:更好的入门系列-官方Walkthrough
    OPEN(SAP) UI5 学习入门系列之三:MVC (下)
  • 原文地址:https://www.cnblogs.com/aukle/p/3228583.html
Copyright © 2011-2022 走看看