Palindromic Subsequence

Time Limit: 3000ms
Memory Limit: 131072KB

This problem will be judged on UVA. Original ID: 11404
64-bit integer IO format: %lld      Java class name: Main

 

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 解题:求最长的且字典序最小的回文子序列。把原串逆转,然后与原串求LCS。LCS的的前半部分一定要求的回文序列的前半部分,但是后半部分可能不是。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct DP{
int len;
string str;
};
DP dp[maxn][maxn];
char sa[maxn],sb[maxn];
int main() {
while(gets(sa)){
int len = strlen(sa);
strcpy(sb,sa);
reverse(sb,sb+len);
for(int i = ; i <= len; ++i){
dp[][i].len = ;
dp[][i].str = "";
}
for(int i = ; i <= len; ++i){
for(int j = ; j <= len; ++j){
if(sa[i-] == sb[j-]){
dp[i][j].len = dp[i-][j-].len+;
dp[i][j].str = dp[i-][j-].str + sa[i-];
}else if(dp[i-][j].len > dp[i][j-].len){
dp[i][j].len = dp[i-][j].len;
dp[i][j].str = dp[i-][j].str;
}else if(dp[i-][j].len < dp[i][j-].len){
dp[i][j].len = dp[i][j-].len;
dp[i][j].str = dp[i][j-].str;
}else{
dp[i][j].len = dp[i-][j].len;
dp[i][j].str = min(dp[i-][j].str,dp[i][j-].str);
}
}
}
string ans = dp[len][len].str;
if(dp[len][len].len&){
for(int i = ; i < dp[len][len].len>>; ++i)
putchar(ans[i]);
for(int i = dp[len][len].len>>; i >= ; --i)
putchar(ans[i]);
putchar('\n');
}else{
for(int i = ; i+ < dp[len][len].len>>; ++i)
putchar(ans[i]);
for(int i = dp[len][len].len>>; i >= ; --i)
putchar(ans[i]);
putchar('\n');
}
}
return ;
}

UVA 11404 Palindromic Subsequence的更多相关文章

  1. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  2. LPS UVA 11404 Palindromic Subsequence

    题目传送门 题意:求LPS (Longest Palidromic Subsequence) 最长回文子序列.和回文串不同,子序列是可以不连续的. 分析:1. 推荐->还有一种写法是用了LCS的 ...

  3. UVa 11404 Palindromic Subsequence (LCS)

    题意:给定一个字符串,问删除一些字符,使得它成为一个最长回访串,如果有多个,输出字典序最小的那个. 析: 我们可以把原字符串反转,然后求两个串的LCS,就得到最长回文串,不过要注意一些细节. 代码如下 ...

  4. 【UVa】Palindromic Subsequence(dp+字典序)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=465&page=s ...

  5. uva 11404 dp

    UVA 11404 - Palindromic Subsequence 求给定字符串的最长回文子序列,长度一样的输出字典序最小的. 对于 [l, r] 区间的最长回文串.他可能是[l+1, r] 和[ ...

  6. UVA 11404 五 Palindromic Subsequence

     Palindromic Subsequence Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu ...

  7. 【UVA 11404】Palindromic Subsequence

    UVA 11404 我用了最暴力的做法:考虑\(dp[i][j]\)表示\(S[i..j]\)的最长回文子序列的长度以及字典序最小的那个. 然后转移的时候如下处理:首先\(dp[i][j]\)要取\( ...

  8. [leetcode-516-Longest Palindromic Subsequence]

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  9. [LeetCode] Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

随机推荐

  1. JSON 基础学习1

    http://www.360doc.com/content/10/0809/22/2633_44873063.shtml JSON转字符串: json.stringify(jsonobj); 字符串转 ...

  2. [using_microsoft_infopath_2010]Chapter8 使用InfoPath表单web部件

    本章概要: 1.配置web part 2.创建web part连接 3.创建表单参数 4.利用其它浏览器表单参数

  3. 王立平--split字符串切割

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzQyNTUyNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  4. [HTML5] Why ARIA?

    For some reason, you build a custom checkbox component, if without ARIA in mind, basiclly this site ...

  5. UVA 11000- Bee 递推

    In Africa there is a very special species of bee. Every year, the female bees of such species give b ...

  6. c7---函数

    // // main.c // 函数练习 // // Created by xiaomage on 15/6/7. // Copyright (c) 2015年 xiaomage. All right ...

  7. jquery分页点击后页面置顶

    前台: <a href="#" ><span id='top'></span></a> js中: 放在分页事件后,数据加载完成后 j ...

  8. TFS源代码管理工具:

    源代码管理: 先获取最新版本,再签入.如发现错误,可以点击--源代码管理--获取特定版本撤回修改 1.签入:(要备注,测试通过后签入) 敏捷开发:(小步快跑):小部分功能开发完成测试通过后就签入 全部 ...

  9. Python3字符串 详解

    Python3 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可. Python 访问字符串中的值 P ...

  10. Python爬虫:爬取某网站关键词对应商品ID,且存入DB2数据库

    公司研发不给力,我就自己写了一个,专门爬关键词对应的商品ID. 其中还学会了用Python操作DB2数据库.Python发送邮件.写日志文件.处理浏览器访问限制. #!/usr/bin/python# ...