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.

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的更多相关文章

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

  2. UVA 11584 一 Partitioning by Palindromes

    Partitioning by Palindromes Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

  3. uva 11584 Partitioning by Palindromes 线性dp

    // uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...

  4. UVA - 11584 划分字符串的回文串子串; 简单dp

    /** 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34398 UVA - 11584 划分字符串的回文串子串: 简单 ...

  5. 区间DP UVA 11584 Partitioning by Palindromes

    题目传送门 /* 题意:给一个字符串,划分成尽量少的回文串 区间DP:状态转移方程:dp[i] = min (dp[i], dp[j-1] + 1); dp[i] 表示前i个字符划分的最少回文串, 如 ...

  6. Uva 11584,划分成回文串

    题目链接:https://uva.onlinejudge.org/external/115/11584.pdf 题意: 一个字符串,将它划分一下,使得每个串都是回文串,求最少的回文串个数. 分析: d ...

  7. UVA 11584 Partitioning by Palindromes (字符串区间dp)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  8. UVa 11584 - Partitioning by Palindromes(线性DP + 预处理)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. UVA 11584 - Partitioning by Palindromes DP

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  10. UVa 11584 Partitioning by Palindromes

    题意: 给出一个字符串,求最少能划分成多少个回文子串. 分析: d[i] = min{d[j] + 1 | s[j+1]...s[i]是回文串} d[i]表示前 i 个字符最少能分割的回文子串的个数 ...

随机推荐

  1. zigbee学习:示例程序SampleApp中通讯流程

    zigbee学习:示例程序SampleApp中通讯流程 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 参考链接: http://wjf88223.bl ...

  2. android笔记6——intent的使用

    今天挑出一节专门来说一下使用intent和intentfilter进行通信. 场景:一个Activity启动还有一个Activity. 前面已经讲了Fragment的切换,Fragment顾名思义是基 ...

  3. 0x00000000该内存不能为read

    0X000000存储器不能read解决方案 有这种现象方面,首先,在硬件,这有问题的内存,二,软件,其中有许多问题. 一:先说说硬件: 一般来说,电脑硬件不easy生病.内存故障的可能性并不大(非你的 ...

  4. 【Android先进】查看手机记忆库状态和应用方法

    一世 我们知道.android程序存储器通常被限制16M.当然,24M的,和android程序存储器分为2部分:native和dalvik.dalvik 就是我们寻常说的java堆.我们创建的对象是在 ...

  5. 数据结构 - trie

    #include <cstring> #include <iostream> #include <map> #include <cstdio> usin ...

  6. 7、Cocos2dx 3.0游戏开发找小三之3.0版本号的代码风格

    重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27691337 Cocos2d-x代码风格 前面我们已 ...

  7. TCP和UDP的差别

    简单的差别: TCP提供面向连接的.可靠的数据流传输,而UDP提供的是非面向连接的.不可靠的数据流传输. TCP传输单位称为TCP报文段,UDP传输单位称为用户数据报. TCP注重数据安全性,UDP传 ...

  8. hibernate的通配符比拼接sql到底好在哪?

    Hibernate对于刚接触的人来说,通配符只是提供了另一种组合sql的方式.接触的久了,熟悉之后,才能够真正理解通配符在Hibernate中起到的作用 主要作用有两点: 1,避免sql注入 hibe ...

  9. Hibernate学习笔记(1)Hibernate构造

    一 准备工作 首先,我们将创建一个简单的基于控制台(console-based)Hibernate应用. 我们所做的第一件事就是创建我们的开发文件夹.并把所有需要用到的Java件放进去.解压缩从Hib ...

  10. C++结构体之统计最高最低分

    [Submit][Status][Web Board] Description 输入学生的姓名和成绩,统计出最高分的学生和最低分的学生. Input 输入5个学生的姓名和分数,用结构体完成 Outpu ...