UVA - 11584 Partitioning by Palindromes(划分成回文串)(dp)
题意:输入一个由小写字母组成的字符串,你的任务是把它划分成尽量少的回文串,字符串长度不超过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;
}
UVA - 11584 Partitioning by Palindromes(划分成回文串)(dp)的更多相关文章
- UVA 11584 Partitioning by Palindromes 划分回文串 (Manacher算法)
d[i]表示前面i个字符划分成的最小回文串个数, 转移:当第i字符加进来和前面区间j构成回文串,那么d[i] = d[j]+1. 要判断前面的字符j+1到i是不是回文串,可以用Manacher算法预处 ...
- Uva 11584,划分成回文串
题目链接:https://uva.onlinejudge.org/external/115/11584.pdf 题意: 一个字符串,将它划分一下,使得每个串都是回文串,求最少的回文串个数. 分析: d ...
- UVa 11584 划分成回文串
https://vjudge.net/problem/UVA-11584 题意: 给出一串字符,把它划分成尽量少的回文串. 思路: 用d[i]表示划分到i时所能划分的最小个数,转移方程为d[i]=mi ...
- UVA 11584 Paritioning by Palindromes(动态规划 回文)
题目大意:输入一个由小写字母组成的字符串,你的任务是把它划分成尽量少的回文串.比如racecar本身就是回文串:fastcar只能分成7个单字母的回文串:aaadbccb最少可分成3个回文串:aaa. ...
- UVA11584 划分成回文串
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/B 紫书275 题意:输入一个字符,最少能划分几个回文串 分析 ...
- 随手练——Uva-11584 划分成回文串(区间DP)
思路:dp[i]代表到第i位的最小值,枚举它的前几位,求出最小值. 转移方程:dp[ i ] = min(dp[ i ], dp[ j - 1 ] + 1 ) ; 本来觉得,代码加深部分可以提前bre ...
- uva 11584 Partitioning by Palindromes 线性dp
// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...
- 区间DP UVA 11584 Partitioning by Palindromes
题目传送门 /* 题意:给一个字符串,划分成尽量少的回文串 区间DP:状态转移方程:dp[i] = min (dp[i], dp[j-1] + 1); dp[i] 表示前i个字符划分的最少回文串, 如 ...
- 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 ...
随机推荐
- Mybatis-问题总结
1.在mybatis里面注释语句的时候,一定用 <!- -需要注释的内容–>.用快捷键注释一行代码显示是/**/,但是实际执行起来报错.
- 剑指offer 二叉树的后序遍历序列
题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 思路:这题目有点特殊,主要在于序列为空的时候,不是 ...
- [Codeforces #615 div3]1294E Obtain a Permutation
Before the Beginniing 本文为 Clouder 原创文章,原文链接为Click,转载时请将本段放在文章开头显眼处.如进行了二次创作,请明确标明. 由本人转载于博客园. 题意分析 C ...
- 常见加密解密 -- pycryptodomex库
安装 windows pip install pycryptodomex ubuntu pip install pycryptodome 加密方式 单向加密:MD5 只能对数据进行加密,而不能解密 对 ...
- ThreadLocal的原理和在框架中的应用
ThreadLocal的原理和在框架中的应用 博客分类: java基础 框架多线程SpringthreadDAO 概述 我们知道Spring通过各种DAO模板类降低了开发者使用各种数据持久 ...
- 0. GC 前置知识
阅读<垃圾回收的算法与实现>时记录的一些笔记. 对象 在GC的世界中,对象表示的是"通过应用程序利用的数据的集合" 头 我们将对象中保存对象本身信息的部分称为头.头主要 ...
- 二十二 动态代理&解决网站的字符集编码问题
设计模式: 软件开发过程中,遇到相似问题,将问题的解决方式抽取模型(套路) 单例,工厂,装饰者,适配器,动态代理 谷歌汽车场景: 谷歌汽车场景Car Interface Icar{ start r ...
- NO22 Linux正则表达式--grep命令常用参数
Linux正则表达式: 一.基础正则第一波字符说明: 示例: 二.基础正则第二波字符说明: 三.基础正则第二波字符说明: 示例: grep:一般常用参数 示例:+和* ?和.: |: () ...
- is application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem
最近试着了解 c++,接触到了QT,写了一个测试程序,在开发环境下正常后移到非开发环境,报错 网上找资料说是少了platforms文件夹中的dll,把里面所有的dll复制到执行程序目录,还是提示,继续 ...
- ubuntu18.04下neo4j的安装
参考CSDN博客 安装jdk8方式与博客中有不同,按照博客中方法没有成功 以下方法配置环境变量成功 进入配置文件 [root@cuierdan java]# vim /etc/profile在文件的后 ...