动态规划——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 ...
随机推荐
- Lambda表达式转SQL语句类库
/* 作者:道法自然 * 个人邮件:myyangbin@sina.cn * 2014-10-1 */ Lambda表达式转SQL语句类库源码下载:http://download.csdn.net/ ...
- ORA-01653:表空间扩展失败的问题
以下内容来源于ORA-01653:表空间扩展失败的问题 今天发现,原来设备的数据表空间只有5M,已经满了,上网去找发现要进行扩展空间. 一.脚本修改方式: ----查询表空间使用情况---使用DB ...
- vs编译和运行的区
编译: 是把代码转变成一系列指令(把源代码翻译为计算机能够识别的语言),产生目标代码,并不限于EXE(EXE只是WINDOWS的东西),这样才能装入内存; 运行: 是运行目标代码(运行EXE),就是执 ...
- oracle数据表误删恢复
1.查看回收站中的表: select object_name,original_name,partition_name,type,ts_name,createtime,droptime from re ...
- 非注解SpringMVC
<!-- SpringMVC前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> < ...
- Cocos2dx开发(1)——Win8.1下 NDK r10 环境搭建
内容简要:仅讲述NDK在Windows环境下搭建方法,至于NDK何物一概不属于本文内容,老鸟或已有环境的跳过. 笔者已安装的环境: vs2013企业版.谷歌官网adt 22.3.0(推荐)省得自己ec ...
- SQL Server的事务处理与高级查询
6.高级查询与脚本 6.1子查询 位于SELECT查询中的SELECT查询. 6.11 标量表达式 select id,val,val-(select avg(val) from tbltest) f ...
- contos 安装jdk1.8
JDK安装配置 查看centos系统32位还是64位, 使用命令uname -a;x86是386,586系列的统称,主要是指指令集合.X64才是cpu对64位计算的支持版本. 1. 下载jdk,本例使 ...
- Entity Framework 级联删除
为一对主从表增加级联删除功能 protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.E ...
- 利用WebRequest类上传文件
说明:1.WebRequest类是一个抽象类,所以上传类实际使用的是其子类 2.打开Fiddler软件,监视正常网页的文件上传,可以看到http协议的请求和响应信息,简略说明 (第一行:请求说明 PO ...