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

  1. aabbaabb
  2. computer
  3. abzla
  4. samhita

Sample Output

  1. aabbaa
  2. c
  3. aba
  4. aha
  5.  
  6. 解题:求最长的且字典序最小的回文子序列。把原串逆转,然后与原串求LCSLCS的的前半部分一定要求的回文序列的前半部分,但是后半部分可能不是。
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <vector>
  8. #include <queue>
  9. #include <cstdlib>
  10. #include <string>
  11. #include <set>
  12. #include <stack>
  13. #define LL long long
  14. #define pii pair<int,int>
  15. #define INF 0x3f3f3f3f
  16. using namespace std;
  17. const int maxn = ;
  18. struct DP{
  19. int len;
  20. string str;
  21. };
  22. DP dp[maxn][maxn];
  23. char sa[maxn],sb[maxn];
  24. int main() {
  25. while(gets(sa)){
  26. int len = strlen(sa);
  27. strcpy(sb,sa);
  28. reverse(sb,sb+len);
  29. for(int i = ; i <= len; ++i){
  30. dp[][i].len = ;
  31. dp[][i].str = "";
  32. }
  33. for(int i = ; i <= len; ++i){
  34. for(int j = ; j <= len; ++j){
  35. if(sa[i-] == sb[j-]){
  36. dp[i][j].len = dp[i-][j-].len+;
  37. dp[i][j].str = dp[i-][j-].str + sa[i-];
  38. }else if(dp[i-][j].len > dp[i][j-].len){
  39. dp[i][j].len = dp[i-][j].len;
  40. dp[i][j].str = dp[i-][j].str;
  41. }else if(dp[i-][j].len < dp[i][j-].len){
  42. dp[i][j].len = dp[i][j-].len;
  43. dp[i][j].str = dp[i][j-].str;
  44. }else{
  45. dp[i][j].len = dp[i-][j].len;
  46. dp[i][j].str = min(dp[i-][j].str,dp[i][j-].str);
  47. }
  48. }
  49. }
  50. string ans = dp[len][len].str;
  51. if(dp[len][len].len&){
  52. for(int i = ; i < dp[len][len].len>>; ++i)
  53. putchar(ans[i]);
  54. for(int i = dp[len][len].len>>; i >= ; --i)
  55. putchar(ans[i]);
  56. putchar('\n');
  57. }else{
  58. for(int i = ; i+ < dp[len][len].len>>; ++i)
  59. putchar(ans[i]);
  60. for(int i = dp[len][len].len>>; i >= ; --i)
  61. putchar(ans[i]);
  62. putchar('\n');
  63. }
  64. }
  65. return ;
  66. }

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. 循环时自动打开url

    'systemutil.Run "C:\Program Files (x86)\HP\QuickTest Professional\samples\flight\app\flight4a.e ...

  2. 启用QNX系统,海尔智能冰箱或成业界“宝马”

        智能家电正处于迅猛发展的态势,国内眼下有非常多企业都在积极布局智能家电,当中又以海尔最为典型.作为家电领域的领头羊,海尔近年来在智能家电领域的动作不小.近期有消息透露.海尔也许会在IFA展会上 ...

  3. 关于server和虚拟主机的差别

    文章都是先由本人个人博客,孙占兴:www.teilim.com,先更新,随后CSDN博客才会更新.掌握第一动态请关注本人主站. 原文链接:http://www.teilim.com/guan-yu-y ...

  4. 例说Linux内核链表(三)

    经常使用的linux内核双向链表API介绍 linux link list结构图例如以下: 内核双向链表的在linux内核中的位置:/include/linux/list.h 使用双向链表的过程,主要 ...

  5. unity3d面试题摘选(全)

    ======================================= 数据结构和算法非常重要.图形学也非常重要! 大的游戏公司非常看重个人基础.综合能力.也有的看重你实际工作能力,看你的De ...

  6. js如何计算浮点数

    js中浮点型是如何运算的呢? 例如:var a=0.69; 我想得到6.9 直接这样写 var c=a*10; alert(c);   得到结果是:6.8999999999999995 到网上一搜,有 ...

  7. Ubuntu16.04系统安装搜狗输入法

    前言:正常双击.deb软件包安装搜狗输入法会有bug,需要按照下面操作进行消除错误. 一.下载搜狗输入法Linux版软件包 下载地址为:http://pinyin.sogou.com/linux/ , ...

  8. POJ 1466 最大独立点集

    思路:匈牙利 n-ans/2; // by SiriusRen #include <cstdio> #include <cstring> #define N 505 using ...

  9. zset 有序集合

    zadd key score1 value1 score2 value2 .. 添加元素 redis 127.0.0.1:6379> zadd stu 18 lily 19 hmm 20 lil ...

  10. zeromq-4.1.2在windows下的编译

    作者:朱金灿 来源:http://blog.csdn.net/clever101 zeromq是一个最近比较火的跨平台消息中间件,最近准备研究它,故下载它的源码编译了一下.我是使用VS2008编译的, ...