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题 回文串统计的更多相关文章

  1. 集训第五周动态规划 G题 回文串

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  2. 动态规划——H 最少回文串

    We say a sequence of characters is a palindrome if it is the same written forwards and backwards. Fo ...

  3. 集训第五周 动态规划 B题LIS

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Des ...

  4. 集训第五周动态规划 I题 记忆化搜索

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  5. 集训第五周动态规划 D题 LCS

    Description In a few months the European Currency Union will become a reality. However, to join the ...

  6. 集训第五周动态规划 F题 最大子矩阵和

    Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...

  7. 集训第五周动态规划 C题 编辑距离

    Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...

  8. 集训第五周 动态规划 K题 背包

    K - 背包 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  9. 集训第五周动态规划 J题 括号匹配

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

随机推荐

  1. 【react native】rn踩坑实践——从输入框“们”开始

    因为团队技术栈变更为react native,所以开始写起了rn的代码,虽然rn与react份数同源,但是由于有很多native有关的交互和变动,实际使用还是碰到蛮多问题的,于是便有了这个系列,本来第 ...

  2. JAVA启动参数三:非Stable参数

    前面我们提到用-XX作为前缀的参数列表在jvm中可能是不健壮的,SUN也不推荐使用,后续可能会在没有通知的情况下就直接取消了:但是由于这些参数中的确有很多是对我们很有用的,比如我们经常会见到的-XX: ...

  3. 人工智能-深度学习(2)TensorFlow安装及基本使用(学习笔记)

    一.TensorFlow 简介 TensorFlow 是 Google 开源的一款人工智能学习系统.为什么叫这个名字呢? Tensor 的意思是张量,代表 N 维数组:Flow 的意思是流,代表基于数 ...

  4. Spring配置文件中未引入dubbo命名空间头部配置而引起的错误案例

    问题描述: Spring配置文件中未引入dubbo命名空间的头部配置而引起项目启动时报出如下错误信息: org.springframework.beans.factory.xml.XmlBeanDef ...

  5. Service官方教程(9)绑定服务时的注意事项

    Binding to a Service Application components (clients) can bind to a service by calling bindService() ...

  6. joda日期格式转换

    public static String parseDateTime(String date,String formatFrom,String formatTo){ DateTimeFormatter ...

  7. 请大家帮我找一找bug —— 一个MySQL解析程序(JAVA实现)

    周末两天我写了一个MySQLParser.写这个东西的目的是:公司的一个项目中需要对数据打版本号(每个表的每条记录要有一个版本号字段,这个字段需要由框架自动打上去,而不是由程序员来做). 所以,我写的 ...

  8. MVC学习(一)

    http://www.cnblogs.com/QLeelulu/archive/2008/09/30/1302462.html

  9. CentOS 7 挂载ntfs磁盘格式的U盘

    因为CentOS 默认不识别NTFS的磁盘格式,所以我们要借助另外一个软件来挂载,那就是ntfs-3g了 自带的yum源没有这个软件,要用第三方的软件源,这里我用的是阿里的epel. 1. 切换到系统 ...

  10. (转)淘淘商城系列——SSM框架整合之逆向工程

    http://blog.csdn.net/yerenyuan_pku/article/details/72758590 我们知道在开发中有些工作是非常耗时但是又没有什么技术含量的,比如创建mapper ...