uva 11584 Partitioning by Palindromes
来源:互联网 发布:tensorflow vgg 编辑:程序博客网 时间:2024/05/22 10:29
原题:
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’).
Can you read upside-down?
• ‘aaadbccb’ can be partitioned as (‘aaa’, ‘d’, ‘bccb’).
Input
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.
Output
For each test case, output a line containing the minimum number of groups required to partition the
input into groups of palindromes.Universidad de Valladolid OJ: 11584 – Partitioning by Palindromes
Sample Input
3
racecar
fastcar
aaadbccb
Sample Output
1
7
3
题目大意:
给你一个字符串,问你这个字符串最少能拆分成多少个回文串的组合。
#include <bits/stdc++.h>using namespace std;//fstream in,out;bool mark[1001][1001];//用来记录i到j这段字符串是否是回文int dp[1001];string s;int main(){ ios::sync_with_stdio(false); int n; cin>>n; while(n--) { cin>>s; memset(dp,0,sizeof(dp)); memset(mark,false,sizeof(mark)); int len=s.size(); for(int i=0;i<len;i++) mark[i][i]=true; for(int i=0;i<len-1;i++) if(s[i]==s[i+1]) mark[i+1][i]=mark[i][i+1]=true;//相邻的两个字符相同要先判断 for(int k=3;k<=len;k++) { for(int i=0;i<len-k+1;i++) { int j=k+i-1; if(s[i]==s[j]&&mark[i+1][j-1]) mark[j][i]=mark[i][j]=true; } } dp[0]=1; for(int i=1;i<len;i++) { if(mark[i][0]) { dp[i]=1; continue; } else dp[i]=INT_MAX; for(int j=1;j<i;j++) { if(mark[i][j]) dp[i]=min(dp[j-1]+1,dp[i]); } dp[i]=min(dp[i],dp[i-1]+1); } cout<<dp[len-1]<<endl; } return 0;}
解答:
之前在leetcode上面做过一个最长回文串的题目,没想到在这用上了。用mark[i][j]表示i到j是否是回文串,如果s[i]==s[j]而且mark[i+1][j-1]为真,那么mark[i][]j]肯定为真,也就是i到j是一个回文串。
接下来设置dp[i]表示以字母s[i]为结尾的字符串最少能分解成多少个回文串,那么dp[i]=min(dp[j-1]+1,dp[i]) 当mark[i][j]为真的情况下。
转移方程的意思是如果mark[i][j]为真,那么就把字符串j前面字符能组成最少回文串数加上回文串i到j即可。
- Uva 11584 - Partitioning by Palindromes
- UVa 11584 - Partitioning by Palindromes
- Uva-11584-Partitioning by Palindromes
- UVA 11584 - Partitioning by Palindromes
- UVA 11584 Partitioning by Palindromes
- UVA 11584 Partitioning by Palindromes
- UVA 11584 Partitioning by Palindromes
- UVA - 11584 Partitioning by Palindromes
- UVa:11584 Partitioning by Palindromes
- UVA 11584 Partitioning by Palindromes
- UVA 11584Partitioning by Palindromes
- UVA 11584 - Partitioning by Palindromes
- UVA 11584 Partitioning by Palindromes
- Uva 11584 Partitioning by Palindromes
- UVa 11584 Partitioning by Palindromes
- UVA - 11584 Partitioning by Palindromes
- UVA 11584 Partitioning by Palindromes
- UVA 11584 Partitioning by Palindromes
- js零零散散的积累
- jQuery表单验证插件Validator的使用方法
- Springframework和Hibernate版本对应关系
- Java基础复习系列五
- 手写服务器httpserver_准备_http协议_http工具JAVA197-198
- uva 11584 Partitioning by Palindromes
- 深入浅出ClassLoader
- /* 判断一个对象是不是数组*/
- hadoop 2.7.2开发环境配置
- hdu——1106排序(重定向)
- 绘制模拟Android钟 ####时钟表盘
- Linux命令对应的全称解释
- python学习之常用的内置类(4):python中的XML
- Android 防内存泄露handler