UVa 11404 - Palindromic Subsequence(最大回文串,区间DP)
2015-04-29 14:53
531 查看
UVa11404Palindromic Subsequence(最大回文串,区间DP)
Description
A Subsequence is a sequence obtained by deleting zero or more characters in a string. A Palindrome is a string which when read from left to right, reads same as when read from right to left. Given a string, find the longest palindromic
subsequence. If there are many answers to it, print the one that comes lexicographically earliest.
Constraints
Maximum length of string is 1000.
Each string has characters `a' to `z' only.
题意:
给定一个字符串s,对s进行删除操作,使得剩下的子串是回文字符串,输出最长的字符串,当有多个相同长度的就输出字典序最小的。
思路:
由于要输出字符串,所以在状态转移过程中要保存下字符串,用C++的string就方便很多,然后就是和找最长回文的方法一样了。
定义结构体保存长度,以及字符串,分别用len, s 表示
状态的转移方程为,如果头尾相同,dp[i][j].len = dp[i + 1][j - 1].len + 2(长度加上首尾,所以增加2);如果首尾不同,那么回文长度不增加 dp[i][j].len = max(dp[i + 1][j].len, dp[i][j - 1].len);
如果长度相同 dp[i + 1][j].len, == dp[i][j - 1].len,那么就要比较子串的字典序,取字典序小的,
也就是判断 dp[i + 1][j].s, dp[i][j - 1].s。
Description
A Subsequence is a sequence obtained by deleting zero or more characters in a string. A Palindrome is a string which when read from left to right, reads same as when read from right to left. Given a string, find the longest palindromic
subsequence. If there are many answers to it, print the one that comes lexicographically earliest.
Constraints
Maximum length of string is 1000.
Each string has characters `a' to `z' only.
Input
Input consists of several strings, each in a separate line. Input is terminated by EOF.Output
For each line in the input, print the output in a single line.Sample Input
aabbaabb computer abzla samhita
Sample Output
aabbaa c aba aha
题意:
给定一个字符串s,对s进行删除操作,使得剩下的子串是回文字符串,输出最长的字符串,当有多个相同长度的就输出字典序最小的。
思路:
由于要输出字符串,所以在状态转移过程中要保存下字符串,用C++的string就方便很多,然后就是和找最长回文的方法一样了。
定义结构体保存长度,以及字符串,分别用len, s 表示
状态的转移方程为,如果头尾相同,dp[i][j].len = dp[i + 1][j - 1].len + 2(长度加上首尾,所以增加2);如果首尾不同,那么回文长度不增加 dp[i][j].len = max(dp[i + 1][j].len, dp[i][j - 1].len);
如果长度相同 dp[i + 1][j].len, == dp[i][j - 1].len,那么就要比较子串的字典序,取字典序小的,
也就是判断 dp[i + 1][j].s, dp[i][j - 1].s。
<span style="font-size:18px;">#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <string> #include <algorithm> #include <queue> #include <stack> using namespace std; const double PI = acos(-1.0); const double e = 2.718281828459; const double eps = 1e-8; const int MAXN = 1010; struct str { int len; string s; } dp[MAXN][MAXN]; char s[MAXN]; int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); while(scanf("%s", s+1) != EOF) { int len = strlen(s+1); for(int i = 1; i <= len; i++) { //初始化 dp[i][i].len = 1; dp[i][i].s = s[i]; } for(int k = 2; k <= len; k++) //控制区间大小 { for(int i = 1, j = k; j <= len; i++, j++) //正推 //for(int i = len-k+1, j = len; i >= 1; i--, j--) //逆推 { if(s[i] == s[j]) { dp[i][j].len = dp[i+1][j-1].len+2; dp[i][j].s = s[i]+dp[i+1][j-1].s+s[j]; } else { if(dp[i][j-1].len > dp[i+1][j].len || (dp[i][j-1].len==dp[i+1][j].len&&dp[i][j-1].s<dp[i+1][j].s)) { //当 [i, j-1] 的长度大于 [i+1, j] 的长度,或者二者长度相等并且 //[i, j-1] 的字典序小于 [i+1, j] 的字典序,则选择 [i, j-1],否则选择后者 dp[i][j].len = dp[i][j-1].len; dp[i][j].s = dp[i][j-1].s; } else { dp[i][j].len = dp[i+1][j].len; dp[i][j].s = dp[i+1][j].s; } } } } cout<<dp[1][len].s<<endl; } return 0; } </span>
相关文章推荐
- UVA-1331 Minimax Triangulation 区间dp 计算几何 三角剖分 最大三角形最小化
- UVA 1331 Minimax Triangulation 最大面积最小的三角剖分(区间dp--记忆化搜索)
- UVA 10453 Make Palindrome(区间dp-补全回文串+打印结果)
- UVa 10739 String to Palindrome(经典回文串区间DP)
- UVa 10617 Again Palindrome(经典回文串区间DP)
- hdu4745(区间dp,求最大回文串的长度)
- UVa 10617 Again Palindrome(回文串区间DP)
- UVA-11584-Partitioning by Palindromes(区间DP 最小回文串数量)
- UVA - 10891 Game of Sum(区间dp)
- Uva 1331 - Minimax Triangulation(最优三角剖分 区间DP)
- UVA-1630 Folding (KMP、区间dp)
- UVA 3938 线段树(连续最大值区间)
- 区间DP UVA 11584 Partitioning by Palindromes
- UVa 11167 Monkeys in the Emei Mountain 最大流,区间模型,输出方案
- UVA 1626 括号序列(区间dp)
- UVa 10891 Game of Sum(经典博弈区间DP)
- POJ 2955 Brackets 区间DP 最大括号匹配
- UVa 10003 - Cutting Sticks 区间dp
- UVA 1626 Brackets sequence(括号匹配 + 区间DP)
- 区间DP(括号序列,uva1626)