UVa 11583 Partitioning by Palindromes



Description

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求回文串,最后求最小个数。

AC代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.*;
public class Main{

	public static void main(String[] args) throws IOException {
		//Scanner scan=new Scanner(System.in);
		StreamTokenizer st = new StreamTokenizer(new BufferedReader(
				new InputStreamReader(System.in)));
		st.nextToken();
		int n=(int)st.nval;
		//scan.nextLine();
		for(int i=0;i<n;i++){
			st.nextToken();
			String s=(String)st.sval;
			int len=s.length();
			char a[]=new char[len];
			a=s.toCharArray();
			int dp[][]=new int[1010][1010];
			for(int j=0;j<len;j++){
				dp[j][j]=1;
				dp[j+1][j]=1;
			}
			for(int j=len-1;j>=0;j--){
				for(int k=j+1;k<len;k++){
					dp[j][k]=Math.min(dp[j+1][k]+1, dp[j][k-1]+1);
					if(a[j]==a[k]&&dp[j+1][k-1]==1){
						dp[j][k]=Math.min(dp[j][k], 1);
					}
				}
			}
			for(int j=0;j<len;j++){
				for(int k=0;k<j;k++){
					dp[0][j]=Math.min(dp[0][j], dp[0][k]+dp[k+1][j]);
				}
			}
			System.out.println(dp[0][len-1]);
		}
	}

}

UVa 11583 Partitioning by Palindromes,布布扣,bubuko.com

时间: 2024-10-21 15:55:53

UVa 11583 Partitioning by Palindromes的相关文章

uva 11584 Partitioning by Palindromes 线性dp

// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串的数目 // // f[i] = min(f[i],f[j-1] + 1(j到i是回文串)) // // 这道题还是挺简单的,继续练 #include <algorithm> #include <bitset> #include <cassert> #include <

UVa 11584 Partitioning by Palindromes

/*---UVa 11584 Partitioning by Palindromes --用dp[i]表示前i个字母划分成最小回文串的个数,则有dp[i]=min{dp[j]+1}s[j+1...i]是一个回文串,状态O(n)个 每次有O(n)个决策,而判断是否是回文串复杂度O(n),这样总的复杂度O(n^3).如果事先预处理标记一下s[i...j]是否构成 回文串,这样总的复杂度O(n^2).标记s[i...j]是否构成回文串,用vis[i][j](i<=j)来标记,if s[i]!=s[j]

UVA - 11584 Partitioning by Palindromes[序列DP]

UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters 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 lis

区间DP UVA 11584 Partitioning by Palindromes

题目传送门 1 /* 2 题意:给一个字符串,划分成尽量少的回文串 3 区间DP:状态转移方程:dp[i] = min (dp[i], dp[j-1] + 1); dp[i] 表示前i个字符划分的最少回文串, 4 如果s[j] 到 s[i]是回文串,那么可以从dp[j-1] + 1递推过来 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <algorithm> 9 #include <cmath&g

Uva 11584 - Partitioning by Palindromes dp

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 o

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

题意  判断一个串最少可以分解为多少个对称串   一个串从左往后和从右往左是一样的  这个串就称为对沉串 令d[i]表示给定串的前i个字母至少可以分解为多少个对称串  那么对于j=1~i   若(i,j)是一个对称串  那么有  d[i]=min(d[i],d[j-1]+1)   然后就得到答案了 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N

【UVa】Partitioning by Palindromes(dp)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=27&page=show_problem&problem=2549 设w[i,j]为i-j能分割成的最少回文串 f[i]为前i个字符能够分成的最少回文串 w[i,j]=1 当w[i+1,j-1]==1 && s[i]==s[j] 或 i==j-1 && s[i]==s[j] w[i,j]=

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

链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2631 题意: 输入一个由小写字母组成的字符串(长度不超过1000),你的任务是把它划分成尽量少的回文串.例如,racecar本身就是回文串:fastcar只能分成7个单字母的回文串,aaadbccb最少分成3个回文串:aaa, d, bccb. 分析: 设d[i]为字符0-i划分成

uva 11584 Partitioning by Palindromes(dp)

题目链接 题意:给定一个字符串,分解成多个子串,每个子串都是回文串,问最少能分成多少个子串. 题解: dp[i]表示前i个字符串分割成最少回文子串的数量: 0<=j<=i;如果字符串从j到i是回文串,那么dp[i]=min(dp[i],dp[j-1]+1); #include <iostream> using namespace std; int dp[1005]; string s; bool ok(int j,int i) { while(j<=i) { if(s[j]!