HDU
来源:互联网 发布:理财软件哪款靠谱 编辑:程序博客网 时间:2024/05/21 10:39
Pieces
HDU - 4628You 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.
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.
T<=10.
2aaabb
12
思路:状压DP。用d[i]表示删除状态为i的字符串的删除次数。先处理出所有回文子串使其d[i]=1。并用tp[MAX]保存下来。再来循环(1<<n)*MAX次。
#include<bits/stdc++.h>using namespace std;char s[20];int d[1<<16],n,tp[1<<16],MAX;bool check(int x) //判断状态为i的字符串是不是回文的{string a="";for(int i=0;i<n;i++,x/=2)if(x%2)a+=s[i];for(int i=0,j=a.size()-1;j>=i;i++,j--)if(a[i]!=a[j])return 0;return 1;}int main(){int T;cin>>T;while(T--){scanf("%s",s);n=strlen(s);memset(d,0x3f3f3f3f,sizeof d);MAX=0;d[0]=0;tp[MAX++]=0;for(int i=1;i<(1<<n);i++)if(check(i))d[i]=1,tp[MAX++]=i;for(int i=0;i<(1<<n);i++){for(int j=0;j<MAX;j++){if(i&tp[j])continue;d[i|tp[j]]=min(d[i|tp[j]],d[i]+d[tp[j]]);}}cout<<d[(1<<n)-1]<<endl;}return 0;}
阅读全文
1 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- 【大数据部落】基于LDA主题模型聚类的商品评论文本挖掘
- 【2017"百度之星"程序设计大赛资格赛 1003】度度熊与邪恶大魔王(DP 背包问题)
- 邪能炸弹
- css选择器
- Java变量的声明、初始化和作用域
- HDU
- C语言各种数据类型取值范围
- poj1284 Primitive Roots
- 呼叫中心KPI
- python之禅
- UVa806 习题6-8 空间结构(Spatial Structures,ACM/ICPC World Finals 1998)
- 遗传算法(4):交叉概率和变异概率的自适应化
- bzoj1096 [ZJOI2007]仓库建设(斜率优化)
- Linux查看服务命令与端口使用状态