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

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

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #include <unordered_map>
  17. #include <unordered_set>
  18. #define debug() puts("++++");
  19. #define freopenr freopen("in.txt", "r", stdin)
  20. #define freopenw freopen("out.txt", "w", stdout)
  21. using namespace std;
  22.  
  23. typedef long long LL;
  24. typedef pair<int, int> P;
  25. const int INF = 0x3f3f3f3f;
  26. const double inf = 0x3f3f3f3f3f3f;
  27. const double PI = acos(-1.0);
  28. const double eps = 1e-8;
  29. const int maxn = 1e3 + 5;
  30. const int mod = 2000;
  31. const int dr[] = {-1, 1, 0, 0};
  32. const int dc[] = {0, 0, 1, -1};
  33. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  34. int n, m;
  35. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  36. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  37. inline bool is_in(int r, int c){
  38. return r >= 0 && r < n && c >= 0 && c < m;
  39. }
  40. char s[maxn];
  41. int dp[maxn];
  42.  
  43. bool judge(int i, int j){
  44. while(i < j){
  45. if(s[i] != s[j]) return false;
  46. ++i, --j;
  47. }
  48. return true;
  49. }
  50.  
  51. int main(){
  52. int T; cin >> T;
  53. while(T--){
  54. scanf("%s", s);
  55. n = strlen(s);
  56. memset(dp, INF, sizeof dp);
  57. dp[0] = 1;
  58. for(int i = 1; i < n; ++i){
  59. for(int j = 0; j < i; ++j)
  60. if(judge(j, i)) dp[i] = min(dp[i], 1 + dp[j-1]);
  61. else dp[i] = min(dp[i], dp[i-1]+1);
  62. }
  63. printf("%d\n", dp[n-1]);
  64.  
  65. }
  66. return 0;
  67. }

UVa 11584 Partitioning by Palindromes (简单DP)的更多相关文章

  1. uva 11584 Partitioning by Palindromes 线性dp

    // uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...

  2. UVA - 11584 Partitioning by Palindromes[序列DP]

    UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...

  3. UVa 11584 Partitioning by Palindromes【DP】

    题意:给出一个字符串,问最少能够划分成多少个回文串 dp[i]表示以第i个字母结束最少能够划分成的回文串的个数 dp[i]=min(dp[i],dp[j]+1)(如果从第j个字母到第i个字母是回文串) ...

  4. UVA 11584 "Partitioning by Palindromes"(DP+Manacher)

    传送门 •题意 •思路一 定义 dp[i] 表示 0~i 的最少划分数: 首先,用马拉车算法求解出回文半径数组: 对于第 i 个字符 si,遍历 j (0 ≤ j < i),判断以 j 为回文中 ...

  5. 区间DP UVA 11584 Partitioning by Palindromes

    题目传送门 /* 题意:给一个字符串,划分成尽量少的回文串 区间DP:状态转移方程:dp[i] = min (dp[i], dp[j-1] + 1); dp[i] 表示前i个字符划分的最少回文串, 如 ...

  6. UVA 11584 - Partitioning by Palindromes DP

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  7. UVA 11584 Partitioning by Palindromes (字符串区间dp)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  8. UVa 11584 - Partitioning by Palindromes(线性DP + 预处理)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. UVA - 11584 Partitioning by Palindromes(划分成回文串)(dp)

    题意:输入一个由小写字母组成的字符串,你的任务是把它划分成尽量少的回文串,字符串长度不超过1000. 分析: 1.dp[i]为字符0~i划分成的最小回文串的个数. 2.dp[j] = Min(dp[j ...

随机推荐

  1. BUPT复试专题—科学计算器(2009)

    题目描述 给你一个不带括号的表达式,这个表达式只包含加.减.乘.除,请求出这个表 达式的最后结果,最后结果一定是整数: 输入 一个数学表达式,只包括数字,数字保证是非负整数,以及五种运算符 " ...

  2. TCP/IP Protocol Architecture

    原文: https://technet.microsoft.com/en-sg/library/cc958821.aspx 1. 主机到网络层 2.网络互连层(互连这个翻译好) ----------- ...

  3. 出现异常时直接把e输出比输出e.getMessage()好得多

    之前研究态度不好,出异常时处理草草了事,今天出现问题才觉得该认真对待每个分支.

  4. SolidEdge如何复制特征 建立类似于UG 块的概念

    直接Ctrl+C和Ctrl+V可以实现特征的复制粘贴   按N键可以改变特征方向 已经复制完成的特征要进行定位,则右击该特征,编辑轮廓,可以进行聪慧尺寸的标注   使用特征库的方式,就像UG的块一样, ...

  5. bsp开发之驱动开发

    驱动程序是可以管理虚拟设备或者物理设备,协议,服务等得软件模块,操作系统仅仅有通过驱动程序才干訪问硬件.针对windows ce开发设备驱动.就是通过platform builder创建一个新的平台, ...

  6. HBase技术简介

    一.HBase简介 HBase – Hadoop Database,是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群. ...

  7. php闭包实例

    php闭包函数,一个典型的实例 function getMoney() { $rmb = 1; $dollar = 6; $func = function($dollar) use (&$rm ...

  8. HTTP的上传文件实例分析

    这个是http文件传输的一种格式,当时不知道这种格式,废弃. HTTP的上传文件实例分析 由于论坛不支持Word写文章发帖. 首先就是附件发送怎么搞,这个必须解决.论坛是php的.我用Chrome类浏 ...

  9. Devices下设备的进程显示为问号的问题

     adb shell getprop ro.debuggable      返回  ro.debuggable=0     说明这个机子的版本不是userdebug版本,是user版本     只有这 ...

  10. Json的简单介绍和解析

    Json:JavaScript对象表示法(JavaScript Object Noatation) Json是存储和交换文本信息的语法,类似XML.它采用键值对的方式来组织,易于人们阅读和编写,同时也 ...