HDU 4628 Pieces(状压DP)题解
2024-08-29 06:06:39
题意:n个字母,每次可以删掉一组非连续回文,问你最少删几次
思路:把所有回文找出来,然后状压DP
代码:
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 16 + 5;
const int M = maxn * 30;
const ull seed = 131;
const int INF = 0x3f3f3f3f;
const int MOD = 1e4 + 7;
char s[maxn];
int pa[1 << maxn];
int dp[1 << maxn];
int n, cnt;
bool check(int st){
char tmp[20];
int len = 0;
for(int i = 0; i < n; i++){
if(st & (1 << i)){
tmp[++len] = s[i];
}
}
for(int i = 1; i <= len / 2; i++){
if(tmp[i] != tmp[len - i + 1]) return false;
}
return true;
}
int main(){
int T;
scanf("%d", &T);
while(T--){
scanf("%s", &s);
n = strlen(s);
cnt = 0;
memset(dp, INF, sizeof(dp));
for(int i = 1; i < (1 << n); i++){
if(check(i)){
pa[cnt++] = i;
dp[i] = 1;
}
}
for(int i = 0; i < (1 << n); i++){
for(int j = 0; j < cnt; j++){
if(i & pa[j]) continue;
dp[i | pa[j]] = min(dp[i | pa[j]], dp[i] + 1);
}
}
printf("%d\n", dp[(1 << n) - 1]);
}
return 0;
}
最新文章
- psoc学习
- pem转换成der
- 跟我学习Storm_Storm基本架构
- 移动web前端之meta标签
- CodeIgniter报错: You must use the ";set"; method to update an entry
- MusigCV安装
- Unity3D ShaderLab 模拟纹理运动
- Selenium 实现联想下拉框
- docker入门(二)
- js大文件分割上传
- QT 信号与槽 QT简单加法器的实现
- JavaScript之一: 闭包、执行环境、作用域链
- 基于Hadoop(M/R)的MySQL到Oracle海量数据切割
- C 真正理解二级指针
- Angular4图片上传预览路径不安全问题
- bzoj 4318 OSU 概率期望dp
- leetcode-algorithms-19 Remove Nth Node From End of List
- windows8 Metro App用Javascript来调用C#的library
- define 常量的定义和读取
- Android Kernel save defalut config