题意: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;
}

最新文章

  1. psoc学习
  2. pem转换成der
  3. 跟我学习Storm_Storm基本架构
  4. 移动web前端之meta标签
  5. CodeIgniter报错: You must use the &quot;set&quot; method to update an entry
  6. MusigCV安装
  7. Unity3D ShaderLab 模拟纹理运动
  8. Selenium 实现联想下拉框
  9. docker入门(二)
  10. js大文件分割上传
  11. QT 信号与槽 QT简单加法器的实现
  12. JavaScript之一: 闭包、执行环境、作用域链
  13. 基于Hadoop(M/R)的MySQL到Oracle海量数据切割
  14. C 真正理解二级指针
  15. Angular4图片上传预览路径不安全问题
  16. bzoj 4318 OSU 概率期望dp
  17. leetcode-algorithms-19 Remove Nth Node From End of List
  18. windows8 Metro App用Javascript来调用C#的library
  19. define 常量的定义和读取
  20. Android Kernel save defalut config

热门文章

  1. [Poi2005]Piggy Banks小猪存钱罐
  2. StringBuilder和输入输出
  3. java之 Request
  4. 房产基于Swoole的PHP RPC框架设计
  5. MFA
  6. redis 代码结构与阅读顺序
  7. scala 时间,时间格式转换
  8. Hive 使用总结
  9. centos6.5安装KVM,并在KVM中安装虚拟6.5系统
  10. Java开发中POJO和JSON互转时如何忽略隐藏字段