UVA 11584
Problem H: Partitioning by Palindromes
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
3
racecar
fastcar
aaadbccb
Sample Output
1
7
3
Kevin Waugh
这道题倒是没什么可说的,简单的DP入门题 , 但我却在题以外的地方上WA了好久
谁能告诉我ios::sync_with_stdio(false);再用cin ,cout 会判错!!!求科普!
悬赏解答该问题!
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set> using namespace std;
const int MAXN = 1010 + 50;
const int maxw = 100 + 20;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e10-8
#define mod 1000000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D; //#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a)) #define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
char inp[MAXN];
int state[MAXN];///记录从头到i的最少回文串数
bool isPalindrome(int s , int t)
{
while(s < t)
{
if(inp[s] != inp[t])return false;
s++;
t--;
}
return true;
}
int main()
{
//ios::sync_with_stdio(false);
#ifdef Online_Judge
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // Online_Judge
int T;
cin >> T;
while(T--)
{
scanf("%s",inp);
int len = strlen(inp);
FOR(i , 0 , len)state[i] = INF;
state[0] = 1;
FOR(i , 1 , len)FORR(j ,0 , i)
{
if(isPalindrome(j , i))
{
if(j == 0)state[i] = min(state[i] , 1);
else state[i] = min(state[j - 1] + 1 , state[i]);
}
}
printf("%d\n" , state[len - 1]);
}
return 0;
}
UVA 11584的更多相关文章
- UVA - 11584 Partitioning by Palindromes[序列DP]
UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...
- UVA 11584 一 Partitioning by Palindromes
Partitioning by Palindromes Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
- uva 11584 Partitioning by Palindromes 线性dp
// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...
- UVA - 11584 划分字符串的回文串子串; 简单dp
/** 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34398 UVA - 11584 划分字符串的回文串子串: 简单 ...
- 区间DP UVA 11584 Partitioning by Palindromes
题目传送门 /* 题意:给一个字符串,划分成尽量少的回文串 区间DP:状态转移方程:dp[i] = min (dp[i], dp[j-1] + 1); dp[i] 表示前i个字符划分的最少回文串, 如 ...
- Uva 11584,划分成回文串
题目链接:https://uva.onlinejudge.org/external/115/11584.pdf 题意: 一个字符串,将它划分一下,使得每个串都是回文串,求最少的回文串个数. 分析: d ...
- UVA 11584 Partitioning by Palindromes (字符串区间dp)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVa 11584 - Partitioning by Palindromes(线性DP + 预处理)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 11584 - Partitioning by Palindromes DP
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa 11584 Partitioning by Palindromes
题意: 给出一个字符串,求最少能划分成多少个回文子串. 分析: d[i] = min{d[j] + 1 | s[j+1]...s[i]是回文串} d[i]表示前 i 个字符最少能分割的回文子串的个数 ...
随机推荐
- android插件化-apkplug框架启动-02
本文章基于apkplug v1.6.7 版本号编写,最新方式以官网最新消息为准 一 apkplug框架所须要的库文件(宿主) 可从http://git.oschina.net/plug/apkplug ...
- 基于 Android 的 3D 视频示例代码
笔者:Mark Liu 下载样本代码 简单介绍 在Android 中,创建一个可以播放视频剪辑的应用很easy:创建一个採用 3D 图形平面的游戏应用也很easy.可是,创建一个可以在 3D 图形对象 ...
- 【Android 应用开发】 FastJson 使用具体解释
博客地址 :http://blog.csdn.net/shulianghan/article/details/41011605 fastjson 源代码地址 : -- GitHub : https:/ ...
- Java Drp项目实战——Drp知多少
是什么 Drp是Distribution Resource Planning的缩写,意思是分销资源计划.它是用来管理企业的执行于Internet上的分销网络的系统,是以商业流程优化为基础,它的核心是销 ...
- [转载] RaspberryPi B+ WiringPi 引脚对应图
Pin Numbering - Raspberry Pi Model B+ Numbering Scheme Expansion Header J8 Pinout (40-pin Header) Ad ...
- top 查看资源使用
top:动态观察程序的变化 ? [root@linux ~]# top [-d] | top [-bnp] 参数: -d :后面可以接秒数,就是整个程序画面更新的秒数.预设是 5 秒: -b :以批次 ...
- bash组织成树数据结构
君子也非独占,善假于物!bash也因此.昨天晚上,今天早上世界杯很精彩.晚上醒来看到不断地居住的电话.早上没有喝的水开始赞赏在英国和意大利的对决.也TM精彩,最后生下了罗马文化.意大利伊特鲁里亚文化获 ...
- 2048 Puzzle游戏攻略
2048 Puzzle这是目前手机游戏的很火. 在地铁上经常看到的人玩这个游戏. 首先,简介2048 Puzzle游戏. 游戏界面是4X4广场格,每一方格可以放置在数字. 有四种移动数字的方法,向左. ...
- 开源 自由 java CMS - FreeCMS1.9 分纪录
项目地址:http://www.freeteam.cn/ 2.4.1 积分记录 查看系统全部会员积分记录. 从左側管理菜单点击积分记录进入. 版权声明:本文博客原创文章,博客,未经同意,不得转载.
- [Unity3D] 有关公告板实现的误区
最直接实现一个公告板,我认为我们应该这样做: usingUnityEngine; publicclassBillboard :MonoBehaviour { voidUpdate() { transf ...