博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA 11584 Partitioning by Palindromes (回文DP,4级)
阅读量:5327 次
发布时间:2019-06-14

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

A - Partitioning by Palindromes
Crawling in process...
Crawling failed
Time Limit:1000MS    Memory Limit:0KB     64bit IO Format:%lld & %llu
Appoint description: System Crawler (2013-05-30)

Description

Problem H: Partitioning by Palindromes

Can you read upside-down?

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 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.

For each test case, output a line containing the minimum number of groups required to partition the input into groups of palindromes.

Sample Input

3racecarfastcaraaadbccb

Sample Output

173

Kevin Waugh
 
一个串最少分成几个回文串
思路:先标记回文串,然后扫一遍DP
   
#include
#include
#include
#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))using namespace std;const int mm=1009;char s[mm];bool isp[mm][mm];int dp[mm];int main(){ int cas; while(~scanf("%d",&cas)) { while(cas--) { clr(isp,0); scanf("%s",s); int len=strlen(s); FOR(i,0,len)isp[i+1][i]=isp[i+2][i]=1; for(int i=len;i>0;--i) FOR(j,i,len) if(isp[i+1][j-1]&&s[i-1]==s[j-1])isp[i][j]=true; dp[0]=0; FOR(i,1,len) { dp[i]=mm; FOR(j,1,i) if(isp[j][i]) dp[i]=min(dp[i],dp[j-1]+1); } printf("%d\n",dp[len]); } } return 0;}

转载于:https://www.cnblogs.com/nealgavin/p/3797529.html

你可能感兴趣的文章
HAL层三类函数及其作用
查看>>
Odoo 去掉 恼人的 "上午"和"下午"
查看>>
web@h,c小总结
查看>>
USACO 3.2 msquare 裸BFS
查看>>
Naive and Silly Muggles (计算几何)
查看>>
java编程思想笔记(一)——面向对象导论
查看>>
nginx 出现504 Gateway Time-out的解决方法
查看>>
(HDU)1089 --A+B for Input-Output Practice (I)(输入输出练习(I))
查看>>
SQL Server 备份和还原
查看>>
Data Structure 基本概念
查看>>
微信内置浏览器不支持 onclick 如何解决?(原因是因为内面中的内容或者标签大部分是动态生成的)...
查看>>
Ubuntu改坏sudoers后无法使用sudo的解决办法
查看>>
记字符编码与转义符的纠缠
查看>>
NEYC 2017 游记
查看>>
【BZOJ 3669】 [Noi2014]魔法森林 LCT维护动态最小生成树
查看>>
[搬运] 写给 C# 开发人员的函数式编程
查看>>
对Python中yield的理解
查看>>
NailTech 公司网站制作思路
查看>>
Shiro权限控制框架
查看>>
java第六次作业
查看>>