hdu 4628 Pieces
http://acm.hdu.edu.cn/showproblem.php?pid=4628
状态压缩DP 时间复杂度应该是 16*(2^32)
但是运行时要远小于这个数 所以加一定剪枝就可以过
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<set>
using namespace std; typedef long long ll;
typedef pair<double,double>ppd;
const double PI = acos(-1.);
const double eps = (1e-9);
const int N=20;
const int M=(1<<16);
bool dp[N][M];
bool ok[M];
string s;
char st[N];
int main()
{
//freopen("data.in","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
int n;
cin>>s;
n=s.length();
int m=(1<<n);
memset(ok,true,sizeof(ok));
for(int i=1;i<m;++i)
{
int ln=0;
for(int j=0;j<n;++j)
{
if((i&(1<<j)))
st[ln++]=s[j];
}
for(int j=0;j<ln;++j)
if(st[j]!=st[ln-1-j])
{ok[i]=false;break;}
}
memset(dp,false,sizeof(dp));
dp[0][m-1]=true;
int ans=n;
for(int i=0;i<n;++i)
{
for(int j=0;j<m;++j)
if(dp[i][j])
{
if(ok[j])
{dp[i+1][0]=true;break;} for(int k=j;k>0;k=j&(k-1))
if(ok[k])
dp[i+1][j^k]=true; }
if(dp[i+1][0]==true)
{ans=i+1;break;}
}
printf("%d\n",ans);
}
return 0;
}
hdu 4628 Pieces的更多相关文章
- HDU 4628 Pieces(DP + 状态压缩)
Pieces 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4628 题目大意:给定一个字符串s,如果子序列中有回文,可以一步删除掉它,求把整个序列删除 ...
- HDU 4628 Pieces(状态压缩+记忆化搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=4628 题意:给个字符窜,每步都可以删除一个字符窜,问最少用多少步可以删除一个字符窜分析:状态压缩+记忆化搜索 ...
- [kmp+dp] hdu 4628 Pieces
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4622 Reincarnation Time Limit: 6000/3000 MS (Java/Ot ...
- hdu 4628 Pieces 状态压缩dp
Pieces Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total S ...
- hdu 4628 Pieces(状态压缩+记忆化搜索)
Pieces Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total S ...
- hdu 4628 Pieces 状压dp
题目链接 枚举所有状态, 1表示这个字符还在原来的串中, 0表示已经取出来了. 代码中j = (j+1)|i的用处是枚举所有包含i状态的状态. #include <iostream> #i ...
- HDU 4628 Pieces(状压DP)题解
题意:n个字母,每次可以删掉一组非连续回文,问你最少删几次 思路:把所有回文找出来,然后状压DP 代码: #include<set> #include<map> #includ ...
- hdu 4628(状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4628 思路:首先把所有的回文找出来,如果当前状态为回文,则dp[state]=1,否则dp[state ...
- hdu 4614 pieces 状态DP
题意:给你一个长度小于等于16的字符串,每次可以删除一个回文传,问你最少删除干净的字数. 状态+dp dp[i] = min(dp[i],dp[j]+dp[j^i]);(j是i的字串): 连接:htt ...
随机推荐
- linux学习笔记2-命令总结4
帮助命令 help - 帮助命令 man - 获取帮助信息 用户管理命令 useradd - 添加新用户 passwd - 设置用户密码 who - 显示所有用户 w - 查看更详细的用户信息 use ...
- python使用open经常报错:TypeError: an integer is required的解决方案
错误是由于从os模块引入了所有的函数导致的,os模块下有一个open函数,接受整型的文件描述符和打开模式,from os import *引入os模块的open函数,覆盖了python内建的open函 ...
- 无限极分类,传递一个子ID得到所有父集,用于在前台分层显示标题
方法: static public function getParents($data,$id){ $arr=array(); foreach ($data as $v) { if ($v['id'] ...
- overflow:auto/hidden的应用
一.自适应两栏布局 <!DOCTYPE html><html lang="zh-CN"><head> <meta charset=&quo ...
- datagrid实现行的上移和下移
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- Python标准库06 子进程 (subprocess包)
这里的内容以Linux进程基础和Linux文本流为基础.subprocess包主要功能是执行外部的命令和程序.比如说,我需要使用wget下载文件.我在Python中调用wget程序.从这个意义上来说, ...
- python把str转换为int
def str2int(s): def fn(x,y): return x+y def char2num(s): ':9}[s] return reduce(fn,map(char2num,s)) ' ...
- 在Eclipse中设置Java类上面的注释(包含作者、日期等)
希望在Eclipse中,让Java类上面的注释像下面这样,改如何设置呢? 在Eclipse中,点击菜单中的Window-->Preferences,打开如下窗口:
- 开机使用root用户登录
有的fedora版本默认不支持开机以root用户登录,这是出于安全机制的考虑,可以通过设置实现开机root用户登录 步骤: 1.修改.etc/pam.d/gdm文件,注释掉auth pam_succ ...
- python unicode字符串
程序开发中,不同语言文字的显示,不同字符集之间的转换非常麻烦,在python的unicode的使用中,对这点感触颇深.所以,以下总结了python中对unicode字符处理的一些理解. 程序存储.传输 ...