集训第五周动态规划 H题 回文串统计
Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A sequence of characters is a palindrome if it is the same written forwards and backwards. For example, 'abeba' is a palindrome, but 'abcd' 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 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.Each test case consists of a single line of between 1 and 1000 lowercase letters, with no whitespace within.For each test case, output a line containing the minimum number of groups required to partition the input into groups of palindromes
racecar
fastcar
aaadbccb
1
7
3
使用dp(i)表示从数组起始位置到i位置回文串的个数
动态规划方程为
dp(i)=min{dp(j-1)+1,dp[i]} //if(子串j~i是回文串)
初始给dp赋值为一个大数,代表这个区间的回文串个数未知
#include"iostream"
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int maxn=;
char aa[maxn];
int dp[maxn]; bool is_palindrome(int a,int b)
{
int m=(a+b)>>;
for(int i=a; i<=m; i++)
if(aa[i]!=aa[b-i+a]) return false;
return true;
} int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%s",aa+);
int n=strlen(aa+);
memset(dp,,sizeof(dp));
dp[]=;
for(int i=;i<=n+;i++) dp[i]=n;
for(int i=;i<=n;i++)
for(int j=;j<=i;j++)
{
if(is_palindrome(j,i))
//dp[i]=dp[j-1]+1;
dp[i]=min(dp[i],dp[j-]+);
}
cout<<dp[n]<<endl;
}
return ;
}
集训第五周动态规划 H题 回文串统计的更多相关文章
- 集训第五周动态规划 G题 回文串
Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...
- 动态规划——H 最少回文串
We say a sequence of characters is a palindrome if it is the same written forwards and backwards. Fo ...
- 集训第五周 动态规划 B题LIS
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Des ...
- 集训第五周动态规划 I题 记忆化搜索
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- 集训第五周动态规划 D题 LCS
Description In a few months the European Currency Union will become a reality. However, to join the ...
- 集训第五周动态规划 F题 最大子矩阵和
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...
- 集训第五周动态规划 C题 编辑距离
Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...
- 集训第五周 动态规划 K题 背包
K - 背包 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- 集训第五周动态规划 J题 括号匹配
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
随机推荐
- JS 自写datapage.js 通用分页
var Page = function () { }; Page.prototype = { Loading: "<img src='/Content/Scripts/Data ...
- BIOS 和UEFI的区别
BIOS先要对CPU初始化,然后跳转到BIOS启动处进行POST自检,此过程如有严重错误,则电脑会用不同的报警声音提醒,接下来采用读中断的方式加载各种硬件,完成硬件初始化后进入操作系统启动过程:而UE ...
- virtualenv杂记
Linux复制命令: cp sourcedir destdir (如果是复制文件夹,增加参数 -a) Linux重命名命令:mv 旧的名称 新的名称 通过xshell查看虚拟机的配置:通过命令 ...
- hihoOffer收割练习20题目2
题目2 : SCI表示法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 每一个正整数 N 都能表示成若干个连续正整数的和,例如10可以表示成1+2+3+4,15可以表示 ...
- PHP获取今天内的时间 今天开始和结束的时间戳
$t = time(); $start = mktime(0,0,0,date("m",$t),date("d",$t),date("Y", ...
- 题解报告:hdu 1754 I Hate It(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某 ...
- .NET下集中实现AOP编程的框架
一.Castle 使用这个框架呢,首先是需要安装NuGet包. 先建立一个控制台项目,然后在NuGet中搜索Castle.Windsor,不出意外的话应该能找到如下的包 然后安装,会自动的安装包Cas ...
- 12c debug 转 12C 连接CDB和PDB
来源:David Dai -- Focus on Oracle 连接到CDB 12c debug 和普通实例一样的连接. 指定ORACLE_SID 以后可以使用OS认证,也可以使用密码进行连接. [o ...
- Suricata是什么?
不多说,直接上干货! 见Suricata的官网 https://suricata.readthedocs.io/en/latest/what-is-suricata.html snort.suirca ...
- 转】RDD与DataFrame的转换
原博文出自于: http://www.cnblogs.com/namhwik/p/5967910.html RDD与DataFrame转换1. 通过反射的方式来推断RDD元素中的元数据.因为RDD本身 ...