Pieces

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

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
 
 
这一题的对字符串的回文判断是可以想到的。
关键在于如果更新它,这里有技巧。
看了别人的题解 ,过的这道题。当时觉得没有思路,现在返回去看。
有一个地方是很值得学习的。
思路:
   1.预处理,求出输入的串的子串中,那些满足回文情况。
   2.更新,dp[1<<k]。

  for(i=1;i<k;i++)//枚举每一种情况。
  {
     for(j=i;j>=1;j--)//对于每一种情况,首选就是它自身了。
    {
      if(flag[j])//判断是否为回文情况
         {
         if(dp[i-j]+1<dp[i])
         dp[i]=dp[i-j]+1;//更新
         }
       j=i&j;//这句很重要
    }
  }

 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std; char a[];
int dp[<<];
bool flag[<<];
void solve(int n)
{
int i,j,k,ans,len;
int f[];
k=<<n;
for(i=;i<k;i++)//预处理
{
ans=;
for(j=;j<=;j++)
if( (i&(<<j))>)
f[++ans]=j;
len=ans/; for(j=;j<=len;j++)
if( a[f[j]]!=a[f[ans-j+]]) break;
if(j>len) flag[i]=true;
else flag[i]=false;
}
for(i=;i<k;i++)
dp[i]=;
dp[]=;
for(i=;i<k;i++)
{
for(j=i;j>=;j--)
{
if(flag[j])
{
if(dp[i-j]+<dp[i])
dp[i]=dp[i-j]+;
}
j=i&j;
}
}
printf("%d\n",dp[k-]);
}
int main()
{
int T,n;
while(scanf("%d",&T)>)
{
while(T--)
{
scanf("%s",a);
n=strlen(a);
solve(n);
}
}
return ;
}

hdu 4628 Pieces 状态压缩dp的更多相关文章

  1. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  2. HDU 3001(状态压缩dp)

    状态压缩dp的第一题! 题意:Mr ACMer想要进行一次旅行,他决定访问n座城市.Mr ACMer 可以从任意城市出发,必须访问所有的城市至少一次,并且任何一个城市访问的次数不能超过2次.n座城市间 ...

  3. hdu 4856 Tunnels 状态压缩dp

    Tunnels Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem ...

  4. HDU 3001【状态压缩DP】

    题意: 给n个点m条无向边. 要求每个点最多走两次,要访问所有的点给出要求路线中边的权值总和最小. 思路: 三进制状态压缩DP,0代表走了0次,1,2类推. 第一次弄三进制状态压缩DP,感觉重点是对数 ...

  5. hdu 4628 Pieces 状压dp

    题目链接 枚举所有状态, 1表示这个字符还在原来的串中, 0表示已经取出来了. 代码中j = (j+1)|i的用处是枚举所有包含i状态的状态. #include <iostream> #i ...

  6. hdu 5045 Contest(状态压缩DP)

    题解:我们使用一个二位数组dp[i][j]记录进行到第i个任务时,人组合为j时的最大和(这里的j我们用二进制的每位相应一个人). 详细见代码: #include <iostream> #i ...

  7. hdu 3091 Necklace 状态压缩dp *******

    Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)Total ...

  8. HDU 2167 Pebbles 状态压缩dp

    Pebbles Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  9. HDU 1074 (状态压缩DP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...

随机推荐

  1. leecode刷题(14)-- 有效的字母异位词

    leecode刷题(14)-- 有效的字母异位词 有效的字母异位词 描述: 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s = " ...

  2. 集合的addAll方法--list.addAll(null)会报错--java.lang.NullPointerException

    Exception in thread "main" java.lang.NullPointerException at java.util.ArrayList.addAll(Ar ...

  3. MySQL5.7 mysql.user创建用户

    mysql -uroot -proot MySQL5.7 mysql.user表没有password字段改 authentication_string: 一. 创建用户: 命令:CREATE USER ...

  4. iOS 之NSOperation(一)

    一.NSOperation的介绍 1.NSOperation的作用 配合使用NSOperation和NSOperationQueue实现多线程编程 2.实现多线程的具体步骤 1)将需要执行的操作封装到 ...

  5. TX2 五种功耗模式

    工作模式介绍 Jetson TX2由一个GPU和一个CPU集群组成,CPU集群由双核丹佛2处理器和四核ARM Cortex-A57组成,通过高性能互连架构连接. 拥有6个CPU核心和一个GPU,您可以 ...

  6. python全栈开发_day5_字符串及列表类型

    一:字符串 1)优先掌握知识点. a=" 21j3:b12jk:b3j12:3bjk12 " #内置方法之strip print(a.strip(" ")) # ...

  7. 128th LeetCode Weekly Contest Pairs of Songs With Total Durations Divisible by 60

    In a list of songs, the i-th song has a duration of time[i] seconds. Return the number of pairs of s ...

  8. vue使用nprogress页面加载进度条

    vue使用nprogress页面加载进度条 NProgress是页面跳转是出现在浏览器顶部的进度条 官网:http://ricostacruz.com/nprogress/ github:https: ...

  9. 05-oralce转换函数

    to_char() 设置日期的显示格式 to_char()  进行数字格式化{如:to_char(987654321.789,'999,999,999,999,999.9999') 将数字三位一逗号显 ...

  10. 使用Hive Rest API 连接HDInsight

    以下连接是微软最新的关于HDInsight中Hive命令的RestAPI示例地址.. 使用 HDInsight .NET SDK 运行 Hive 查询 请使用接口有异常的同学检查是否使用的是下面地址中 ...