Partitioning by Palindromes UVA - 11584(最小回文串分解DP)

1人阅读 评论(0) 收藏 举报
分类:

    We say a sequence of charactersis a palindrome if itis the same written forwardsand backwards. For example,‘racecar’ is a palindrome, but‘fastcar’ is not.

    A partition of a sequence ofcharacters is a list of one ormore disjoint non-empty groupsof consecutive characters whoseconcatenation yields the initialsequence. For example, (‘race’,‘car’) is a partition of ‘racecar’into two groups.

    Given a sequence of characters,we can always create a partitionof these characters suchthat each group in the partitionis a palindrome! Given this observationit is natural to ask:what is the minimum number ofgroups needed for a given stringsuch that every group is a palindrome?

    For example:• ‘racecar’ is already apalindrome, therefore itcan be partitioned intoone group.• ‘fastcar’ does not containany non-trivial palindromes,so it must be partitionedas (‘f’, ‘a’, ‘s’, ‘t’,‘c’, ‘a’, ‘r’).• ‘aaadbccb’ can be partitionedas (‘aaa’, ‘d’, ‘bccb’).

Input

Input begins with the number n of test cases. Each test case consists of a single line of between 1 and1000 lowercase letters, with no whitespace within.

Output

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

Sample Input

3

racecar

fastcar

aaadbccb

SampleOutput

1

7

3

题目大意&&解题思路:

给定一个字符串,使其分解为若干个回文串,输出最小的分解数。利用动态规划思想,d[i] 表示s[0,i]所能分解成的回文串最小数量,d[0]=1是必然的,意思是从头开始分解,d[i]有两种情况,若存在一个0<j<i使得s[j,i]为回文串,那么s[j-1]后的串s[i,j]就单独构成了一个回文串,d[i]无非是d[i-1]的基础上加1;不然,d[i]=d[i-1]+1,代表开辟一个新的分解小串。

Code:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
string s;
int check(int x,int y){
	while(x<y){
		if(s[x]!=s[y]) return 0;
		x++;
		y--;
	}
	return 1;
}
int d[1005]; 
int main(){
	int t;

	cin>>t;
	getchar();
	while(t--){
		getline(cin,s);
		int len=s.length();
		d[0]=1;
		for(int i=1;i<len;i++){
			d[i]=d[i-1]+1;
			for(int j=i-1;j>=0;j--){
				if(check(j,i))	d[i]=min(d[i],d[j-1]+1);
			}
		}
		cout<<d[len-1]<<endl;
	}
	
} 


查看评论

例题9-7 划分成回文串 UVa11584

1.题目描述:点击打开链接 2.解题思路:本题要求划分回文串,且个数尽可能的少。可以用动态规划解决。先提前判断i~j是否构成回文串,时间复杂度是O(N^2),然后定义d(i)表示0~i-1划分成的回文...
  • u014800748
  • u014800748
  • 2015-02-13 12:56:36
  • 615

uva 划分成回文串 11584 题解

题目大意是求把一字符串划分成尽可能少的回文串,用dp来做 状态转移方程是d[i]=min{d[j]+1}其中j+1到i为回文串 问题的关键在于如果每次转移都要判断j+1到i是不是回文串,那么时间复...
  • u014664226
  • u014664226
  • 2015-05-01 12:42:45
  • 347

UVa 11584 Partitioning by Palindromes(DP 最少对称串)

题意  判断一个串最少可以分解为多少个对称串   一个串从左往后和从右往左是一样的  这个串就称为对沉串 令d[i]表示给定串的前i个字母至少可以分解为多少个对称串  那么对于j=1~i   若(i,...
  • acvay
  • acvay
  • 2014-08-25 15:07:18
  • 710

UVA - 11584 Partitioning by Palindromes

题意:输入一个有小写字母组成的字符串,你的任务是将它划分成尽量少的回文串 思路:dp[i]代表到第i位的最小值,枚举它的前几位求出最小值,为了方便枚举整个长度我们 从str[1]开始输入 #inc...
  • u011345136
  • u011345136
  • 2013-12-20 18:41:36
  • 850

UVa11584 -Partitioningby Palindromes

UVa11584 -Partitioningby Palindromes   题意:给定一个字符串,求该字符串最少能被分为几个回文子串。 分析:很明显的区间dp,问题规模为判断1到n这n...
  • consciousman
  • consciousman
  • 2016-09-27 19:29:33
  • 315

UVA 11584 Partitioning by Palindromes 回文串dp

Partitioning by Palindromes Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %ll...
  • budlele
  • budlele
  • 2013-10-28 19:38:18
  • 505

UVa 11584 - Partitioning by Palindromes 回文串dp

Problem H: Partitioning by Palindromes We say a sequence of characters is a palindrome if it is...
  • cyendra
  • cyendra
  • 2013-04-24 17:14:00
  • 751

uva11584 dp最少回文串划分

此题很简单但是我看到网上O(n^2)的写法居然慢与O(n^3)的写法,难道是数据太水,以至于初始化时间长了? o(n^2)就是处理一遍flag [ i ] [ j ] 意思是i的长度J的开始时是否是...
  • luyuncheng
  • luyuncheng
  • 2012-12-01 22:47:17
  • 2372

UVa 11584 Partitioning by Palindromes(DP)

题目链接:http://acm.hust.edu.cn/vjudge/problem/34398 题意:给出一个字符串,把它划分成尽量少的回文串。 没想到用O(n3)的暴力也能过,...
  • Interstellar_
  • Interstellar_
  • 2016-08-19 18:59:13
  • 134

uva 11584 Partitioning by Palindromes dp

Problem H: Partitioning by Palindromes We say a sequence of characters is a palindrome if it is t...
  • qq_16843991
  • qq_16843991
  • 2014-08-09 18:12:06
  • 304
    个人资料
    持之以恒
    等级:
    访问量: 608
    积分: 241
    排名: 31万+
    文章存档