集训第五周动态规划 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 ...
随机推荐
- CAD中的相对坐标和绝对坐标
绝对坐标就是你作图的整个界限的原点,也就是CAD系统默认的原点坐标. 相对坐标就是相对于当前的点的坐标. 这两种坐标都有,可以根据习惯和需要自己看使用哪种. 一.绝对坐标 ①笛卡尔坐标(X,Y,Z) ...
- 通过IDEA制作包含Java应程序的Docker镜像
IDEA官网在IDEA中把Java App制作成Docker镜像并启动一个容器运行 在idea上使用docker作为java的开发环境[][] ubuntu+docker+docker-compose ...
- shell脚本从入门到精通
阿里云大学 shell脚本从入门到精通 第1 章 : shell脚本编程-变量-算术表达式-判断语句-if分支语句 第2 章 : case-for-While-双括号-循环嵌套-break-conti ...
- 贪心 Codeforces Round #109 (Div. 2) B. Combination
题目传送门 /* 贪心:按照能选的个数和点数降序排序,当条件不符合就break,水题啊! */ #include <cstdio> #include <algorithm> # ...
- SSRS域账号下 User 'XXX' does not have required permissions的处理方法
SSRS安装完成后,点击Report Manager URL,提示:User 'XXX' does not have required permissions. Verify that suffici ...
- Android开发-下载网络图片并显示到本地
Android下载网络图片的流程是: 发送网络请求->将图片以流的形式下载下来->将流转换为Bitmap并赋给ImageView控件. 注意点 最新的Android系统不可以在主线程上请求 ...
- iOS-UI控件之UIImageView
contentMode属性 带有scale单词的:图片有可能会拉伸 UIViewContentModeScaleToFill 将图片拉伸至填充整个imageView 图片显示的尺寸跟imageView ...
- js中cookie的操作
JavaScript中的另一个机制:cookie,则可以达到真正全局变量的要求. cookie是浏览器 提供的一种机制,它将document 对象的cookie属性提供给JavaScript.可以由J ...
- js模块化方案以及前端打包工具
图片来自知乎
- 一些常用的meta标签及其作用
声明文档使用的字符编码 <meta charset='utf-8'>优先使用 IE 最新版本和 Chrome <meta http-equiv="X-UA-Compat ...