题目链接

枚举所有状态, 1表示这个字符还在原来的串中, 0表示已经取出来了。

代码中j = (j+1)|i的用处是枚举所有包含i状态的状态。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
int mark[<<], len, dp[<<];
string s;
int check(int x) {
if(!x)
return ;
int i = , j = len-;
while(i<j) {
while(((<<i)&x)==)
i++;
while(((<<j)&x)==)
j--;
if(s[i]!=s[j])
return ;
i++, j--;
}
return ;
}
int main()
{
int n;
cin>>n;
while(n--) {
cin>>s;
len = s.size();
int sta = <<len;
for(int i = ; i<sta; i++) {
mark[i] = check(i);
}
mem2(dp);
dp[sta-] = ;
for(int i = sta-; i>=; i--) {
for(int j = i; j<sta; j = (j+)|i) {
if(!mark[i^j])
continue;
if(dp[i]>dp[j]+)
dp[i] = dp[j]+;
}
}
printf("%d\n", dp[]);
}
return ;
}

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

  1. HDU 4284Travel(状压DP)

    HDU 4284    Travel 有N个城市,M条边和H个这个人(PP)必须要去的城市,在每个城市里他都必须要“打工”,打工需要花费Di,可以挣到Ci,每条边有一个花费,现在求PP可不可以从起点1 ...

  2. HDU 4336 容斥原理 || 状压DP

    状压DP :F(S)=Sum*F(S)+p(x1)*F(S^(1<<x1))+p(x2)*F(S^(1<<x2))...+1; F(S)表示取状态为S的牌的期望次数,Sum表示 ...

  3. HDU 3001 Travelling ——状压DP

    [题目分析] 赤裸裸的状压DP. 每个点可以经过两次,问经过所有点的最短路径. 然后写了一发四进制(真是好写) 然后就MLE了. 懒得写hash了. 改成三进制,顺利A掉,时间垫底. [代码] #in ...

  4. HDU - 5117 Fluorescent(状压dp+思维)

    原题链接 题意 有N个灯和M个开关,每个开关控制着一些灯,如果按下某个开关,就会让对应的灯切换状态:问在每个开关按下与否的一共2^m情况下,每种状态下亮灯的个数的立方的和. 思路1.首先注意到N< ...

  5. hdu 4114(状压dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4114 思路:首先是floyd预处理出任意两点之间的最短距离.dp[state1][state2][u] ...

  6. HDU 3091 - Necklace - [状压DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3091 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  7. HDU 3811 Permutation 状压dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3811 Permutation Time Limit: 6000/3000 MS (Java/Othe ...

  8. hdu 4628 Pieces 状态压缩dp

    Pieces Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total S ...

  9. HDU 5838 (状压DP+容斥)

    Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的 ...

随机推荐

  1. AssetBundle的使用

    using UnityEngine; using System.Collections; using UnityEditor; using System.IO; public class Editor ...

  2. AJAX应用中必须要掌握的知识!

    AJAX是什么? 是Asynchronous Javascript And XML的首字母的缩写, 它不是一门的新的语言,而是对现有技术的综合利用. 其本质是在HTTP协议的基础上以异步的方式与服务器 ...

  3. python dbhelper(simple orm)

    # coding:utf- import pymysql class Field(object): pass class Expr(object): def __init__(self, model, ...

  4. Linux网络管理——网关作用

    1. 网络基础 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB",&q ...

  5. jQuery获取Select选择的Text和Value(详细汇总)

    语法解释: 1. $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发 2. var ch ...

  6. virtualbox 虚拟机网络设置

    1.宿主机网卡设置 virtualbox 第一块网卡设置 virtualbox第二块网卡设置 2.虚拟机网络设置 找到以上设置中MAC地址对应的那个网卡: 配置网络: 重启网络,ok.

  7. centos之tomcat安装

    1.环境说明     系统:centos, 2.6.32-573.el6.x86_64; tomcat: apache-tomcat-7.0.68 2.下载文件并上传     下载apache-tom ...

  8. 记一次SQL联合查询注入工具的编写

    这是一个ASP网站的简单SQL注入检测和利用的工具,主要的功能是简单的检测出SQL注入漏洞,可以使用该id存在的SQL注入来获取数据库中的网站管理员的表名和字段名,猜解数据库中该表的字段数,最后通过联 ...

  9. vim note

    2016-1-22 vim plugin collections: (参考 https://www.youtube.com/watch?v=0QFR-_wUoA0) vim-pathogen  插件管 ...

  10. MySQLdb autocommit

    MySQLdb 中 autocommit 默认是关闭的,下面是例子. import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root ...