博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
集训第五周动态规划 H题 回文串统计
阅读量:7031 次
发布时间:2019-06-28

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

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=1010;char aa[maxn];int dp[maxn];bool is_palindrome(int a,int b){    int m=(a+b)>>1;    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+1);      int n=strlen(aa+1);      memset(dp,0,sizeof(dp));      dp[0]=0;    for(int i=1;i<=n+1;i++) dp[i]=n;    for(int i=1;i<=n;i++)    for(int j=1;j<=i;j++)      {        if(is_palindrome(j,i))          //dp[i]=dp[j-1]+1;            dp[i]=min(dp[i],dp[j-1]+1);      }      cout<
<
View Code

 

 

 

转载于:https://www.cnblogs.com/zsyacm666666/p/4725551.html

你可能感兴趣的文章
20145103 《Java程序设计》第3周学习总结
查看>>
ubuntu声音系统
查看>>
哈哈更新资源列表2
查看>>
冲刺第一周第五天
查看>>
Java 接口
查看>>
Android 微信第三方登录
查看>>
硬盘的读写原理
查看>>
实例 centos自动挂载、备份windows共享文件夹,并删除第7日前当天的备份
查看>>
LNMP下动静分离部署phpmyadmin软件包
查看>>
如何写更好的自动化测试用例
查看>>
60再谈指针
查看>>
repost
查看>>
android异步加载AsyncTask
查看>>
GCC Stack-Smashing Protector
查看>>
虚拟机Visualbox安装Ubuntu Server
查看>>
用带余除法可以解决一切部分分式的题目
查看>>
vs 生成事件
查看>>
jmeter 实战项目总结2——微信端
查看>>
php.ini 中文版
查看>>
即时通信客户端流程,
查看>>