UVA-11584:Partitioning by Palindromes(基础DP)
今天带来一个简单的线性结构上的DP,与上次的照明系统(UVA11400)是同一种类型题,便于大家类比、总结、理解,但难度上降低了。
We say a sequence of characters is a palindrome if it is the same written forwards and backwards. For example, ‘racecar’ is a palindrome, but ‘fastcar’ is not. A partition of a sequence of characters is a list of one or more disjoint non-empty groups of consecutive characters whose concatenation yields the initial sequence. For example, (‘race’, ‘car’) is a partition of ‘racecar’ into two groups. Given a sequence of characters, we can always create a partition of these characters such that each group in the partition is a palindrome!
正序倒序写都相同的字符串我们称之为回文串。例如,‘racecar’就是回文的,‘fastcar’就不是。对一个字符序列的划分即:分成一堆(至少一个)非空不相交的连续字符串,使它们连起来就是原来的字符序列。例如,‘race’,‘car’就是把‘racecar’划分成两组。给定一个字符串,我们总能找到一种划分使得每个子串都是回文串!(大不了一个字母算一个子串)
Given this observation it is natural to ask: what is the minimum number of groups needed for a given string such that every group is a palindrome?
For example:
• ‘racecar’ is already a palindrome, therefore it can be partitioned into one group.
• ‘fastcar’ does not contain any non-trivial palindromes, so it must be partitioned as (‘f’, ‘a’, ‘s’, ‘t’, ‘c’, ‘a’, ‘r’).
• ‘aaadbccb’ can be partitioned as (‘aaa’, ‘d’, ‘bccb’).
求:使得每个子串都是回文串的最小划分组数。
例如,‘racecar’本身就是个回文串所以它的答案是1组;‘fastcar’不含回文子串,只能一个字母一个字母地分,答案为7组;‘aaadbccb’最优可以分成‘aaa’,‘d’,‘bccb’3组。
Input
Input begins with the number n of test cases. Each test case consists of a single line of between 1 and 1000 lowercase letters, with no whitespace within..
最先输入测试组数n。每组给出一个长度1~1000的小写字母串,中间没有空格。
Output
For each test case, output a line containing the minimum number of groups required to partition the input into groups of palindromes.
对于每组测试,输出可划分的最少组数。
Sample Input
3
racecar
fastcar
aaadbccb
Sample Output
1
7
3
思路:
假如我遍历一遍字符串 ----> 强如‘fastcar’的话只能一个字母一个字母地苦逼+1,那么有回文子串时,差异是如何产生的呢? ----> 就说racecar吧。走到race的时候还是+1模式,再走一步到c的时候发现跟前面的ce能凑个cec ----> 我们用dp数组表示结果,dp[racec]本来等于dp[race]+1,由于找到了回文子串cec,所以变成了min( dp[race]+1, dp[ra]+1 ) ----> 由于我们不知道当前字母最早可以伸展到哪里去跟别人结合为回文子串,所以可以暴力扫一遍前面的 ----> 至于回文串,一边扫一遍判断也可以,预处理也可以,关键是复杂度。预处理可以枚举回文串中心然后向左右伸展得到(j,i)是不是回文串,可以以n²的复杂度求解,这样dp的过程也是n²。一边dp一边判断大概是n³的复杂度,我不知道怎么就过了我复杂度算错了?……
最开始瞎写的代码1:20ms
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; int T;
int dp[];
char str[]; bool ispalindrome(int start, int end)
{
for (int i = start; i < (start+end+)/; i++)
if (str[i] != str[start+end-i])
return false;
return true;
} int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%s", str+); int len = strlen(str+);
for (int i = ; i <= len; i++)
{
dp[i] = dp[i-] + ;
for (int j = ; j <= i-; j++)
if (ispalindrome(j, i))//[j,i]是不是回文
dp[i] = min(dp[i], dp[j-] + );
} printf("%d\n", dp[len]);
}
}
按照上面瞎改的代码2:20ms
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; int T;
int dp[];
char str[];
bool ispalindrome[][]; int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%s", str+); int len = strlen(str+);
memset(ispalindrome, false, sizeof(ispalindrome));
memset(dp, 0x3f, sizeof(dp)); for (int i = ; i <= len; i++)
{
for (int l = i, r = i; str[l] == str[r] && l >= && r <= len; l--, r++)
ispalindrome[l][r] = true;
for (int l = i, r = i+; str[l] == str[r] && l >= && r <= len; l--, r++)
ispalindrome[l][r] = true;
} dp[] = ;
for (int i = ; i <= len; i++)
for (int j = ; j <= i; j++)
if (ispalindrome[j][i])//[j,i]是不是回文
dp[i] = min(dp[i], dp[j-] + ); printf("%d\n", dp[len]);
}
}
UVA-11584:Partitioning by Palindromes(基础DP)的更多相关文章
- uva 11584 Partitioning by Palindromes 线性dp
// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[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 ...
- UVa 11584 Partitioning by Palindromes【DP】
题意:给出一个字符串,问最少能够划分成多少个回文串 dp[i]表示以第i个字母结束最少能够划分成的回文串的个数 dp[i]=min(dp[i],dp[j]+1)(如果从第j个字母到第i个字母是回文串) ...
- UVa 11584 Partitioning by Palindromes (简单DP)
题意:给定一个字符串,求出它最少可分成几个回文串. 析:dp[i] 表示前 i 个字符最少可分成几个回文串,dp[i] = min{ 1 + dp[j-1] | j-i是回文}. 代码如下: #pra ...
- UVA 11584 "Partitioning by Palindromes"(DP+Manacher)
传送门 •题意 •思路一 定义 dp[i] 表示 0~i 的最少划分数: 首先,用马拉车算法求解出回文半径数组: 对于第 i 个字符 si,遍历 j (0 ≤ j < i),判断以 j 为回文中 ...
- 区间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
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA 11584 Partitioning by Palindromes (字符串区间dp)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVa 11584 - Partitioning by Palindromes(线性DP + 预处理)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA - 11584 Partitioning by Palindromes(划分成回文串)(dp)
题意:输入一个由小写字母组成的字符串,你的任务是把它划分成尽量少的回文串,字符串长度不超过1000. 分析: 1.dp[i]为字符0~i划分成的最小回文串的个数. 2.dp[j] = Min(dp[j ...
随机推荐
- [HAOI2016]找相同子串
这题感觉有点坑啊. 题目还是不难想的,先对一个字符串建后缀自动机,然后拿另一个字符串在上面跑. 假设当前跑到了p点,匹配长度为len. 那么当前会对答案产生贡献的串是哪些呢? 显然当前会对p及p到根的 ...
- platform_set_drvdata和platform_get_drvdata用法【转】
本文转载自:http://www.cnblogs.com/wangxianzhen/archive/2013/04/09/3009530.html 在用到Linux设备驱动的platform框架时,常 ...
- Ubuntu安装基础教程
作者:TeliuTe 来源:基础教程网 二十三.安装Ubuntu14.04 返回目录 下一课 14.04 版安装与前面版本类似,学习中遇到不清楚的地方,可以参考一下前面的内容,操作中注意细心,下面来看 ...
- mysql的navicat注册码生成
首先下载安装Navicat在Navicat关闭的情况下运行注册机在注册机界面点击patch,选择Navicat安装目录下的Navicat.exe打补丁弹出破解成功后拔掉网线断网products选择my ...
- hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)
题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 基于aspectj实现AOP操作的两种方式——xml配置
1. 要导入的 jar 包: 常用的aspectj表达式: 权限修饰符可以省略,以下表示:返回值类型为任意,com.chy.service包以及其子包下的.任意类的.参数任意的.任意方法 execut ...
- MongoDB:搭建三节点 Replica Set 环境
今天学习了搭建 MongDB 复制环境,实验环境是在虚拟机上同一系统,并搭建三节点 Replica Set,根据文档上的描述,mongodb 复制配置简单,并且能够自动 failover,这些高级特性 ...
- AngularJS系统学习之Factory,Service, Provider(工厂,服务,供应者)
本文转自:http://blog.csdn.net/zcl_love_wx/article/details/51404390 我看过敲过代码之后, 有了很深的理解, 这三个东西其实都是用来返回对象的. ...
- 使用 StoryBoard 制作一个能够删除cell的TableView
本篇博客方便自己检索使用.资源链接 下面是制作效果图,点击删除按钮,就能将该cell删除: 下面是主要的代码: #define KSUPER_TAG 20000 #define KDEFAU_TAG ...
- powerdesigner设置主键为自增字段,设置非主键为唯一键并作为表的外键
转自:https://www.cnblogs.com/CoffeeHome/archive/2014/06/04/3767501.html 这里powerdesigner连接的数据库是以mysql为例 ...