题意:输入一个由小写字母组成的字符串,你的任务是把它划分成尽量少的回文串,字符串长度不超过1000。

分析:

1、dp[i]为字符0~i划分成的最小回文串的个数。

2、dp[j] = Min(dp[j], dp[i - 1] + 1),若i~j是回文串,则更新dp[j]。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){if(fabs(a - b) < eps) return 0;return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
char s[MAXN];
int dp[MAXN];
bool judge(int l, int r){int len = r - l + 1;for(int i = 0; i < len / 2; ++i)if(s[l + i] != s[r - i]) return false;return true;
}
int main(){int T;scanf("%d", &T);while(T--){memset(dp, INT_INF, sizeof dp);scanf("%s", s + 1);int len = strlen(s + 1);dp[0] = 0;for(int i = 1; i <= len; ++i){for(int j = i; j <= len; ++j){if(judge(i, j)){dp[j] = Min(dp[j], dp[i - 1] + 1);}}}printf("%d\n", dp[len]);}return 0;
}

  

转载于:https://www.cnblogs.com/tyty-Somnuspoppy/p/6417523.html

UVA - 11584 Partitioning by Palindromes(划分成回文串)(dp)相关推荐

  1. UVA11584 划分成回文串 Partitioning by Palindromes(线性DP划分+DP判断回文串)

    整理的算法模板合集: ACM模板 依旧是线性DP 我们使用闫氏DP分析法 总体DP转移的时间复杂度为O(n2)O(n^2)O(n2). 但是这里牵扯到判断 i\tt ii 到 j\tt jj 是否为回 ...

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

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

  3. UVA 11584—— Partitioning by Palindromes

    题意:给定一个串,然后问最少可以分割成多少个回文串. 思路:简单dp,dp[i]=min(dp[j]+1,1<j<=i),两次循环扫一遍即可,考察dp的思想. code: #include ...

  4. uva 11584——Partitioning by Palindromes

    题意:给定一个字符串,把该字符串划分为最少的回文串. 思路:dp,到达i点的回文串长度都存起来,那么dp[i]=min(以i为结尾的最短的回文串长度). code: #include <bits ...

  5. 【132】求把字符串分割成回文串的最少切分次数

    给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是回文. 返回符合要求的 最少分割次数 . 示例 1: 输入:s = "aab" 输出:1 解释:只需一次分割就可将 s ...

  6. POJ1159 回文串DP与MTE,滚动数组

    题意: 输入一串字符串,插入一些字符使这个字符串变成回文串,求插入字符个数的最小值 要点: 可以这么看,设字符串为C1,C2,C3--Cn,如果C1==Cn,那么只要再考虑C2是否等于Cn-1即可,如 ...

  7. UVA 11584 Partitioning by Palindromes 动态规划 入门

    这个题目的大意就是,给你一个字符串,然后让你求出最少的回文数.我开始傻逼了,写了一个o(n^3)的算法,结果老超时.然后略看了别人的题解,才知道有个如此的转移方程. f[i+1]=min(f[j]+1 ...

  8. 【回文串5 重点+动态规划】LeetCode 132. Palindrome Partitioning II

    LeetCode 132. Palindrome Partitioning II Solution1:我的答案1 直接模仿131那道题的DFS解法,找其中size最小的.果不其然,因为超时只能部分AC ...

  9. 动态规划之回文串问题

    一.DP求回文串[POJ-1159] A palindrome is a symmetrical string, that is, a string read identically from lef ...

最新文章

  1. Prometheus — 软件架构
  2. OpenCV Python教程系列
  3. excel函数去重_将数字去重,然后按从小到大的顺序连接起来!
  4. WaitForMultipleObject与MsgWaitForMultipleObjects用法
  5. 如何隐晦地表达“滚”?
  6. CNN/RNN TF1.4
  7. Java web后端1 XML URI与URL HTTP Tomcat
  8. 软件工程师你应该知道的100个原则
  9. WAF(NGINX)中502和504的区别
  10. Python 3.5 正式退役,不再受支持
  11. php短网址案例,php 短网址小例子
  12. OpenGL环境配置(超全整合版)
  13. JS生成随机数并排序
  14. 嵌入式linux/鸿蒙开发板(IMX6ULL)开发(九)第一个程序背后的C语言知识
  15. 锐捷DHCPV6配置
  16. Android - Md5加密 工具类
  17. 小程序体验版二维码进入请求超时
  18. python找色_利用python检测色情图片简易实例
  19. 自己搜的算法题2.0
  20. Redis---Redis三种常用数据结构

热门文章

  1. struts2 标签的使用之二 s:iterator
  2. error: jump to label ‘XXXX’ [-fpermissive]
  3. linux 父子进程的关系
  4. 防止IE6出现BUG的十种常见解决方法
  5. C# chart控件基础使用
  6. ios+openflow 问题
  7. 特性(property)
  8. Win7系统安装MySQL5.5.21图解教程
  9. ORACLE查询语句简单实例
  10. 关于使用AIR开发移动APP 的技术解决方案 笔记