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 ...
随机推荐
- 「Luogu P3820 小D的地下温泉」
这道题的考点比较多. 前置芝士 BFS(DFS),这两种算法在这道题中并没有什么特别突出的地方,基本就是自己看心情写(本文以DFS为准,所以我心情是好是坏呢?) 连通块,可以将每一个温泉看作一个连通块 ...
- SSM-Maven配置
全配置 新建项目 新建文件夹 - src - main - java - resources - webapp - WEB-INF - index.jsp - pom.xml <?xml ver ...
- Day3-E-New Year Snowmen CodeForces140C
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. ...
- SSH框架系列:Spring AOP应用记录日志Demo
分类: [java]2013-12-10 18:53 724人阅读 评论(0) 收藏 举报 1.简介 Spring 中的AOP为Aspect Oriented Programming的缩写,面向切面编 ...
- NULL判斷符
Null 传导运算符 编程实务中,如果读取对象内部的某个属性,往往需要判断一下该对象是否存在.比如,要读取message.body.user.firstName,安全的写法是写成下面这样. const ...
- NPM概述及使用简介
什么是 NPM npm之于Node,就像pip之于Python,gem之于Ruby,composer之于PHP. npm是Node官方提供的包管理工具,他已经成了Node包的标准发布平台,用于Node ...
- Deepctr框架代码阅读
DeepCtr是一个简易的CTR模型框架,集成了深度学习流行的所有模型,适合学推荐系统模型的人参考. 我在参加比赛中用到了这个框架,但是效果一般,为了搞清楚原因从算法和框架两方面入手.在读代码的过程中 ...
- (十)微信小程序---上传图片chooseImage
官方文档 示例一 wxml <view bindtap="uploadImage">请上传图片</view> <image wx:for=" ...
- ElasticSearch入门了解
什么是Elasticsearch: Elasticsearch,分布式,高性能,高可用,可伸缩的搜索和分析系统 1.什么是搜索? 搜索,就是在任何场景下,找寻你想要的信息,这个时候,会输入一段你要搜索 ...
- docker安装mysql中注意事项
前言 怎么安装docker和拉mysql镜像不是本文的重点,在这里我主要讲我安装mysql容器的三个注意事项:启动容器, 修改密码,远程登录 run容器 docker run -di --name f ...