HDU -4628 Pieces

http://acm.hdu.edu.cn/showproblem.php?pid=4628

Pieces

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1610    Accepted Submission(s):
850

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

2013
Multi-University Training Contest 3

题意 :  给一个字符串,每次可以删除一个回文串 ,比如 axbdyhba ;可以删 abyba ,剩下 xdh ; 问最少要删多少次能把字符串删除完 ;

分析; 首先状态压缩, 因为长度只有16 ;所以顶多是2^16-1 ;每个位1表示存在字母,0表示删除了这个字母 : 则一个所有0和1组成的二进制就可以表示出所有字符串的状态 : 比如 abcde : 他的状态是 11111 , ab_d_ 的状态是11010 ,表示删除第四个和第五个 :所以2^5-1能表示出所有你的子集,也就是字符串的所有状态 .而一个二进制对应着一个整数,利用哈希表,可以记录状态 is[i]=1 ,表示这个状态是回文串,

is[i]=0;表示这个状态不是回文串 ; 比如 串 acda , a_da 是一个回文串,二进制位1011 ,对应的整数位11 ;所有 is[11]=1 ;

当枚举出了所有的状态后,就可以dp过了 ; d[i] 表示 最少需要多少步能到达 i状态 ; 所以最终答案是, 最少需要多少步能到达全部删除完的状态,也就是 ,000000 , 即 d[0] ; 而 d[i]是通过后面得 d[j]状态通过删除字母到达d[i]状态的, 所以j>i 并且 j表示的二进制数的1的个数一定要比i多 ,因为1的个数就代表字母的个数, j状态要通过删除一些字母才能到达i状态,所以j的二进制里的1个数要比i多;也就是

要保证 (j|i)==j ,才能使用状态转移方程 d[i]=min(d[i],d[j]+1) ;

注意优化,我就是超时。

#include<stdio.h>
#include<string.h>
#define inf 1<<27
char str[20];
int dp[65536],len,mark[65536];
int min(int x,int y)
{
    if(x>y)
      return y;
    else
      return x;
}
int judge(char c[])
{
     int i,j,n;
     n=strlen(c);
         j=n-1;
    for(i=0;i<=n/2-1&&j>=n/2-1;i++)
    {
        if(c[i]!=c[j])
            {
                return 0;
            }
         j--;
    }
    return 1;
}
int solve(int x)
{
    char a[20];
     int r=0;
     int y,temp,q=0;
      temp=x;
      while(temp)
      {
          int cnt=temp%2;
          if(cnt==1)
              a[r++]=str[q];
             q++;
         temp=temp/2;
      }
       a[r]=‘\0‘;
     if(judge(a))
         return 1;
      else
        return 0;
}
void init()
{
    int i;
    for(i=1;i<1<<len;i++)
  {
     if(solve(i)==1)
         mark[i]=1;
      else
         mark[i]=0;
  }
}
int count(int n)
 {
     int num = 0;
     while(n){
        n &= (n - 1);
        num++;
       }
     return num;
 }
int main()
{
    int t,i,k,j;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        memset(mark,0,sizeof(mark));
        scanf("%s",str);
        len=strlen(str);
           init();
           if(judge(str))
               {
                   printf("1\n");
                   continue;
               }
               int s=(1<<len)-1;
               dp[s]=0;
             for(i =s-1;i>=0;i--)

           {
                 dp[i] = inf;
            for(j = i+1;j<=s;j=(j+1)|i)#include<stdio.h>
#include<string.h>
#define inf 1<<27
char str[20];
int dp[65536],len,mark[65536];
int min(int x,int y)
{
    if(x>y)
      return y;
    else
      return x;
}
int judge(char c[])
{
     int i,j,n;
     n=strlen(c);
         j=n-1;
    for(i=0;i<=n/2-1&&j>=n/2-1;i++)
    {
        if(c[i]!=c[j])
            {
                return 0;
            }
         j--;
    }
    return 1;
}
int solve(int x)
{
    char a[20];
     int r=0;
     int y,temp,q=0;
      temp=x;
      while(temp)
      {
          int cnt=temp%2;
          if(cnt==1)
              a[r++]=str[q];
             q++;
         temp=temp/2;
      }
       a[r]=‘\0‘;
     if(judge(a))
         return 1;
      else
        return 0;
}
void init()
{
    int i;
    for(i=1;i<1<<len;i++)
  {
     if(solve(i)==1)
         mark[i]=1;
      else
         mark[i]=0;
  }
}
int count(int n)
 {
     int num = 0;
     while(n){
        n &= (n - 1);
        num++;
       }
     return num;
 }
int main()
{
    int t,i,k,j;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        memset(mark,0,sizeof(mark));
        scanf("%s",str);
        len=strlen(str);
           init();
           if(judge(str))
               {
                   printf("1\n");
                   continue;
               }
               int s=(1<<len)-1;
               dp[s]=0;
             for(i =s-1;i>=0;i--)

           {
                 dp[i] = inf;
            for(j = i+1;j<=s;j=(j+1)|i)//#include<stdio.h>
#include<string.h>
#define inf 1<<27
char str[20];
int dp[65536],len,mark[65536];
int min(int x,int y)
{
    if(x>y)
      return y;
    else
      return x;
}
int judge(char c[])//判断回文串
{
     int i,j,n;
     n=strlen(c);
         j=n-1;
    for(i=0;i<=n/2-1&&j>=n/2-1;i++)
    {
        if(c[i]!=c[j])
            {
                return 0;
            }
         j--;
    }
    return 1;
}
int solve(int x)
{
    char a[20];
     int r=0;
     int y,temp,q=0;
      temp=x;
      while(temp)
      {
          int cnt=temp%2;
          if(cnt==1)
              a[r++]=str[q];
             q++;
         temp=temp/2;
      }
       a[r]=‘\0‘;
     if(judge(a))
         return 1;
      else
        return 0;
}
void init()
{
    int i;
    for(i=1;i<1<<len;i++)
  {
     if(solve(i)==1)
         mark[i]=1;
      else
         mark[i]=0;
  }
}
int count(int n)
 {
     int num = 0;
     while(n){
        n &= (n - 1);
        num++;
       }
     return num;
 }
int main()
{
    int t,i,k,j;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        memset(mark,0,sizeof(mark));
        scanf("%s",str);
        len=strlen(str);
           init();
           if(judge(str))
               {
                   printf("1\n");
                   continue;
               }
               int s=(1<<len)-1;
               dp[s]=0;
             for(i =s-1;i>=0;i--)

           {
                 dp[i] = inf;
            for(j = i+1;j<=s;j=(j+1)|i)// j = (j+1)|i能省去很多状态
            {
                if(mark[j-i])
                     {
                     dp[i] = min(dp[i],dp[j]+1);
                     }
              }
           }
      printf("%d\n",dp[0]);
    }
    return 0;
}

            {
                if(mark[j-i])
                     {
                     dp[i] = min(dp[i],dp[j]+1);
                     }
              }
           }
      printf("%d\n",dp[0]);
    }
    return 0;
}

            {
                if(mark[j-i])
                     {
                     dp[i] = min(dp[i],dp[j]+1);
                     }
              }
           }
      printf("%d\n",dp[0]);
    }
    return 0;
}

HDU -4628 Pieces,布布扣,bubuko.com

时间: 2024-12-28 08:13:47

HDU -4628 Pieces的相关文章

[状压dp] hdu 4628 Pieces

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4628 Pieces Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1665    Accepted Submission(s): 862 Problem Description You heart broke into pieces

hdu 4628 Pieces(状态压缩+记忆化搜索)

Pieces Time 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

[kmp+dp] hdu 4628 Pieces

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4622 Reincarnation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 2096    Accepted Submission(s): 715 Problem Description Now you are back,and

HDU 4628 Pieces(状态压缩DP)

题目链接:传送门 题意: 给定一个长度小于16的字符串然后每次可以去掉它的一个回文子序列,问最少删除多少次可以使这个字符串为空. 分析: 首先预处理出这个字符串的所有回文子序列,然后将其压缩成二进制x,然后dp[x]表示这个序列删除所需要的最小的步数,然后dp[x] = min(dp[x],dp[sub])sub表示x的所有子序列. 代码如下: #include <iostream> #include <string> #include <cstring> #inclu

HDU 4628 Pieces(状压DP)题解

题意:n个字母,每次可以删掉一组非连续回文,问你最少删几次 思路:把所有回文找出来,然后状压DP 代码: #include<set> #include<map> #include<cmath> #include<queue> #include<cstdio> #include<vector> #include<cstring> #include <iostream> #include<algorithm&

【HDOJ】4628 Pieces

最开始的想法是搜索,发现不对,后来发现数据量很小,可以状态压缩+DP. 1 /* 4628 */ 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 6 #define MAXN 17 7 #define INF 9999 8 9 char s[MAXN]; 10 char ss[MAXN]; 11 int dp[1<<MAXN]; 12 int len; 13 14 inline

HDU 4628 状态压缩

//又被题虐了MD #include<stdio.h> #include<string.h> char ch[18]; int dis[(1<<17)],dp[(1<<17)]; int min(int a,int b) { if(a<b)return a; return b; } int main() { int i,j,n,m; while(scanf("%d",&n)!=EOF) { scanf("%s&qu

hdu 5640 King&#39;s Cake(模拟)

Problem Description It is the king's birthday before the military parade . The ministers prepared a rectangle cake of size n×m(1≤n,m≤10000) . The king plans to cut the cake himself. But he has a strange habit of cutting cakes. Each time, he will cut

HDU 1114 (dp 完全背包)

鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 Problem Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The i