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. [ActionScript 3.0] AS向php发送二进制数据方法之——在URLRequest中构造HTTP协议发送数据

    主类 HTTPSendPHP.as package { import com.JPEGEncoder.JPGEncoder; import com.fylib.httpRequest.HttpRequ ...

  2. 4 个快速的 Python 编译器 for 2018

    简评:Python 和其他的解释型语言一样经常被吐槽性能不行,所以开发人员为了提升性能创建了不少编译器,本文则选取其中的四个做了基准测试. Python 其实是一种相当快的语言,但它并不像编译型语言那 ...

  3. 关于在iOS应用中跳转到AppStore

    1.获取app 在AppStore上的网址 eg: NSString * appURLStr = @"https://itunes.apple.com/cn/app/shi-ke-zu-qi ...

  4. 基础篇:1)时代的发展与结构设计--3d与2d设计的变迁

    本章目的:机械设计需要追寻时代的脚步. 1.机械作图时代的划分 这张图是作者个人对机械作图发展的简单划分,并非哪个网站的数据.所以粗糙或错误请海涵. 2.2d作图时代 纯2d时代中,工程师开只绘制二维 ...

  5. DP 免费馅饼 HDU1176

    DP 免费馅饼 HDU1176 vjudge题面 一道基本的DP题,状态转移很好想,每一个状态的位置\(pos\)都只能由上一秒的\(pos-1, pos, pos+1\)三个位置转移而来(当然要判断 ...

  6. [转] Jenkins pipeline 中获取 exit code, stdout and stderr 返回值和输出

    [From] https://issues.jenkins-ci.org/browse/JENKINS-44930 其做法是,把stdout定向到一个文件,sh 配置 returnStatus: tr ...

  7. vue源码-检查对象是否全相等

    /** * Check if two values are loosely equal - that is, * if they are plain objects, do they have the ...

  8. 二分--POJ-3258

    POJ-3258,二分 题目 Description Every year the cows hold an event featuring a peculiar version of hopscot ...

  9. shell map使用

    # 定义初始化map declare -A map=([") # 输出所有key echo ${map[@]} # 输出key对应的值 "]} # 遍历map for key in ...

  10. nginx实现多语言跳转不同的url

    nginx实现多语言跳转不同的url server { listen 80; server_name www.text.com; location / { if ($http_accept_langu ...