博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
动态规划——H 最少回文串
阅读量:4948 次
发布时间:2019-06-11

本文共 1661 字,大约阅读时间需要 5 分钟。

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是否是回文串,如果是单个的字母,则也单独组成一个回文串

程序代码:

1 #include
2 #include
3 using namespace std; 4 #define MAXN 1010 5 char a[MAXN]; 6 int d[MAXN]; 7 int min(int x,int y) 8 { 9 return x
View Code

 

转载于:https://www.cnblogs.com/www-cnxcy-com/p/4722177.html

你可能感兴趣的文章
ABP开发框架前后端开发系列---(7)系统审计日志和登录日志的管理
查看>>
Jmeter参数的AES加密使用
查看>>
hdu 2594 Simpsons’ Hidden Talents【kmp】
查看>>
numpy两列数据合并的方法
查看>>
多机联合产生负载
查看>>
kubelet Pod status的状态分析
查看>>
常见错误: 创建 WCF RIA Services 类库后, 访问数据库出错
查看>>
对Javascript面向对象的理解
查看>>
已知一个日期和天数, 求多少天后的日期(是那个超时代码的AC版)
查看>>
CAS 逻辑流程图
查看>>
hyperopt中文文档:Scipy2013
查看>>
testng+IEDriverServer+yum+tomcat下载软件包
查看>>
perl6 Net::HTTP 不能发送https请求
查看>>
python 远程 部署和运行
查看>>
hihocoder 第一周 最长回文字串
查看>>
MongoDB学习day02--数据库增删改查
查看>>
spoj705 后缀数组求不同子串的个数
查看>>
KMP算法的一点学习笔记
查看>>
分页查询信息(使用jdbc连接mysql数据库实现分页查询任务)
查看>>
localStorage 杂记
查看>>