题意:给定一个字符串,求出它最少可分成几个回文串。

析:dp[i] 表示前 i 个字符最少可分成几个回文串,dp[i] = min{ 1 + dp[j-1] | j-i是回文}。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#define debug() puts("++++");
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 5;
const int mod = 2000;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
char s[maxn];
int dp[maxn]; bool judge(int i, int j){
while(i < j){
if(s[i] != s[j]) return false;
++i, --j;
}
return true;
} int main(){
int T; cin >> T;
while(T--){
scanf("%s", s);
n = strlen(s);
memset(dp, INF, sizeof dp);
dp[0] = 1;
for(int i = 1; i < n; ++i){
for(int j = 0; j < i; ++j)
if(judge(j, i)) dp[i] = min(dp[i], 1 + dp[j-1]);
else dp[i] = min(dp[i], dp[i-1]+1);
}
printf("%d\n", dp[n-1]); }
return 0;
}

最新文章

  1. PHP变量
  2. 【BZOJ-2730】矿场搭建 Tarjan 双连通分量
  3. 用实例讲解RSA加密算法(精)
  4. lambda的使用ret = filter(lambda x : x &gt; 22 ,[11,22,33,44])
  5. javascript动态添加form表单元素
  6. 【BZOJ】1002: [FJOI2007]轮状病毒(DP+规律+高精度)
  7. Android的Task和Activity相关
  8. Ubuntu16.04 install mysql5.X
  9. Linux之增加系统调用[内核编译]
  10. Netty学习记录
  11. nginx配置url重写
  12. hdu-6434-欧拉函数
  13. SPOJ - HIGH Highways(矩阵树定理)
  14. 如何在虚拟机安装的Win10系统里快速打开【此电脑】图标?(图文详解)
  15. JVM调优总结(4):分代垃圾回收
  16. 以About Us为范例在Zen cart中增加页面
  17. bzoj 1927 [Sdoi2010]星际竞速——网络流
  18. python开发_自己开发的一个小游戏
  19. js创建ActiveXObject无效
  20. sudo mount -o loop pm.img /mnt/floppy

热门文章

  1. Thrift --- 支持双向通信
  2. flask结合令牌桶算法实现上传和下载速度限制
  3. Cocos2d-x JSB 自己主动绑定bindings
  4. python各种类型转换
  5. 在windows中增加linux命令
  6. HDU 3118 Arbiter 判定奇圈
  7. Linux C++的多线程编程(转)
  8. ubuntu 16.04 更新后搜狗输入法无法输入中文的问题
  9. GTK入门学习:布局容器之水平布局
  10. 初识glib(1)