动态规划——H 最少回文串
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! 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’).
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.
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
题目大意:一串字符串中,找出最少组成字符串
解题思路:
用枚举法枚举起点到终点是否是回文串,即判断j-i是否是回文串,如果是单个的字母,则也单独组成一个回文串
程序代码:
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 1010
char a[MAXN];
int d[MAXN];
int min(int x,int y)
{
return x<y?x:y;
}
bool level(int l,int r)
{
int m=(l+r)/;
for(int i=l; i<=m; i++)
if(a[i]!=a[r-i+l]) return false;
return true;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
scanf("%s",a+);
int m=strlen(a+);
d[]=;
for(int i=; i<=m+; i++)
d[i]=;
for(int i=; i<=m; i++)
for(int j=; j<=i; j++)
if(level(j,i))
d[i]=min(d[i],d[j-]+);
printf("%d\n",d[m]);
}
return ;
}
动态规划——H 最少回文串的更多相关文章
- 集训第五周动态规划 H题  回文串统计
		
Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A ...
 - Minimum Palindromic Factorization(最少回文串分割)
		
Minimum Palindromic Factorization(最少回文串分割) 以下内容大部分(可以说除了关于回文树的部分)来自论文A Subquadratic Algorithm for Mi ...
 - 51Nod - 1154 回文串划分(最少回文串dp)
		
回文串划分 有一个字符串S,求S最少可以被划分为多少个回文串. 例如:abbaabaa,有多种划分方式. a|bb|aabaa - 3 个回文串 a|bb|a|aba|a - 5 个回文串 a|b ...
 - 集训第五周动态规划 G题  回文串
		
Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...
 - 【CPLUSOJ】【动态规划】最短回文串
		
题目链接 [问题描述] 如果一个字符串正过来读和倒过来读是一样的,那么这个字符串就被称作回文串.例如abcdcba,abcddbca就是回文串,而abcdabcd不是. 你要解决的问题是:对于任意一个 ...
 - UVA-11584 Partitioning by Palindromes 动态规划 回文串的最少个数
		
题目链接:https://cn.vjudge.net/problem/UVA-11584 题意 给一个字符串序列,问回文串的最少个数. 例:aaadbccb 分为aaa, d, bccb三份 n< ...
 - NYOJ 1023 还是回文(DP,花最少费用形成回文串)
		
/* 题意:给出一串字符(全部是小写字母),添加或删除一个字符,都会产生一定的花费. 那么,将字符串变成回文串的最小花费是多少呢? 思路:如果一个字符串增加一个字符 x可以形成一个回文串,那么从这个字 ...
 - hdu 1159 Palindrome(回文串)  动态规划
		
题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串) 分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解 状态转化方程: ...
 - 动态规划——G 回文串
		
G - 回文串 Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Stat ...
 
随机推荐
- SimpleDateFormat使用详解
			
http://blog.csdn.net/gubaohua/article/details/575488 public class SimpleDateFormat extends DateForma ...
 - java 进制.
			
/* 整数的'3'种表现形式: 1,十进制. 2,八进制. 3,十六进制. */ public class IntegerDemo { public static void main(String[] ...
 - 用Ueditor存入数据库带HTML标签的文本,从数据库取出来后,anjular用ng-bind-html处理带HTML标签的文本
			
ng.module('index-filters', []) .filter('trustHtml', function ($sce) { return function (input) { retu ...
 - PHP android ios相互兼容的AES加密算法
			
APP项目用户密码传输一直没有用HTTPS,考虑到用户的隐私暂时先用AES对密码加密,以后也可以用于手机端与服务端加密交互. PHP的免费版phpAES项目,手机端解码各种不对. 好不容易找了PHP ...
 - action 关联
			
<act_window context="{'product_id': active_id}" id="act_stock_product_location_ope ...
 - BAE 环境下配置 struts2 + spring + hibernate(SSH)(一)准备
			
1.首先选择版本控制 SVN 或者 Git ,但是由于Git在windows下需要环境,所以优先选择SVN. 2.安装一个SVN客户端 windows下使用TortoiseSVN:立即下载 注意:BA ...
 - G - A+B for Input-Output Practice (VI)
			
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description You ...
 - iOS常用的加密方式--备用
			
MD5 iOS代码加密 创建MD5类,代码如下 #import <Foundation/Foundation.h> @interface CJMD5 : NSObject +(NSStri ...
 - RHEL/CentOS 6.x 系统服务详解
			
PS:RHEL/CentOS 6.x的系统服务比5.x系列的要多了很多新面孔,估计很多童鞋不甚理解,网上这方面资料也很少.理解这个对运维人员是必要的,因为开启不必要的服务越 多,系统就相对越不安全.不 ...
 - 理解Java(StringBuffer和StringBuilder)
			
StringBuffer可实现同步,StringBuilder线程不安全. 翻译自Java API英文文档 本质 StringBuffer 和 StringBuilder 均表示一个可变字符序列 这个 ...