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. [转贴]VC编译器版本号_MSC_VER and _MSC_FULL_VER

    Visual Studio version and discrimination macros Abbreviation Product name [Visual Studio version] †1 ...

  2. debian下安装 locale 并设置 zh_CN.UTF-8 [转]

    root@Linkstation :~# apt-get install locales root@Linkstation:~# dpkg-reconfigure locales root@Links ...

  3. linux使用rsync、inotify-tools实现多台服务器文件实时同步

    需求:将本地192.168.1.10上的/data/wwwroot目录同步到 1.来源服务器上安装rsync.inotify-tools yum -y install rsync yum -y ins ...

  4. leetcode-475-Heaters

    题目描述: Winter is coming! Your first job during the contest is to design a standard heater with fixed ...

  5. docker搭建tomcat环境

    1.拉取镜像 docker pull tomcat 2.运行容器 docker run --name tomcat -p : -v /data/tomcat/test:/usr/local/tomca ...

  6. 2018-2019-2 网络对抗技术 20165314 Exp6 信息搜集与漏洞扫描

    一.原理与实践说明 1.实践内容 本实践的目标是掌握信息搜集的最基础技能.具体有: 1.各种搜索技巧的应用 2.DNS IP注册信息的查询 3.基本的扫描技术:主机发现.端口扫描.OS及服务版本探测. ...

  7. 关于云主机Thinkphp框架Session跨页失效的问题

    在网站部署到云主机之后,前台一直能够正常显示,后台确登录不上去,验证码也无法显示,研究半天,才确定是Session跨页传递失效的问题.找网上各种解决方法,都是关于Php.ini文件的设置,可又解决不了 ...

  8. Q606 根据二叉树创建字符串

    你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串. 空节点则用一对空括号 "()" 表示.而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空 ...

  9. python yield、yield from与协程

    从生成器到协程 协程是指一个过程,这个过程与调用方协作,产出由调用方提供的值.生成器的调用方可以使用 .send(...)方法发送数据,发送的数据会成为yield表达式的值.因此,生成器可以作为协程使 ...

  10. Map.Entry使用详解

    1.Map.Entry说明 Map是java中的接口,Map.Entry是Map的一个内部接口. Map提供了一些常用方法,如keySet().entrySet()等方法,keySet()方法返回值是 ...