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 个字符最少能分割的回文子串的个数 ...
 
随机推荐
- MyEclipse中“擅自乱改”项目名导致项目报错的处理
			
最近几天培训的过程中,经常有同学手一抖,默默的修改了本来配置部署好的项目名,导致项目报错…… 遇到这种事情,我一般会做的处理就是重新新建项目,然后把包和各种文件ctrl+c ctrl+v,遇到项目小还 ...
 - android集成apk对一些问题经常遇到系统
			
1.集成APK必须确认是否release版本号,否则会导致CTS测试失败. 途径:反编译apk,视图manifest.xml文件,看<application>在那里debug属性:andr ...
 - 修改easyui datebox默认日期格式
			
问题描述: 根据jquery easyui datebox demo中给的示例,导入和使用datebox, 发现日期格式为: 6/22/2011, 其他的今天和关闭也是 Today, Close, 对 ...
 - enumerateObjectsUsingBlock、enumerateObjectsWithOptions、enumerateObjectsAtIndexes、makeObjectsPerfor使用
			
OC至 NSArray它提供了一个方便的遍历block,以下具体说明 第一.enumerateObjectsUsingBlock NSArray *array=@[@"aa",@& ...
 - win7/win8通过媒体流(DLNA技术)共享音乐照片和视频
			
http://www.jb51.net/os/windows/79421.html 工具/原料 Windows 7/8/10家庭高级版以上版本 家庭WiFi局域网(无须连接互联网) 支持DLNA的手机 ...
 - JTree demo
			
JFrame居中方法一: setLocationRelativeTo(null); 注意:必须在整个frame初始化完成后再加上此语句,否则将显示在屏幕右下角 方法二: private Dimen ...
 - hdu1052 Tian Ji -- The Horse Racing 馋
			
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1052">http://acm.hdu.edu.cn/showproblem.php ...
 - [MSSQL]最小公约数
			
[摘要]一个朋友在展BOM的时候有这种需求,两列字段(数值):A ,B A=用量,B=底数,组成用量=用量/底数.A/B,若能被整除,显示整除的结果,若不能整除显示分数形式A/B(分数形式要是约分 ...
 - Cocos发展Visual Studio下一个libcurl图书馆开发环境的搭建
			
我们解释win32在Visual Studio下一个libcurl图书馆开发环境的搭建.Cocos2d-x发动机实际上与Win32在访问libcurl库.Cocos2d-x 3.x在libcurl库文 ...
 - 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)
			
原文:乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) 作者:weba ...