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. redis 零散知识

    1.单线程 2.默认 16 个库.0~15 3.select :切换数据库 4.DBsize :查看当前数据库的数量 5.keys * :查看当前库的所有 key 6.keys k? :问号是占位符 ...

  2. cogs 106. [NOIP2003] 加分二叉树(区间DP)

    106. [NOIP2003] 加分二叉树 ★☆   输入文件:jfecs.in   输出文件:jfecs.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] 设 一个 n ...

  3. [Linux]第四部分-Linux用户管理

    登陆过程:1.从etc/passwd中查找账号,没有则退出,然后在etc/shadow中读出uid与密码表passwd中内容格式 用户名:密码:UID:GID:用户信息说明:家目录:用户所用Shell ...

  4. FATAL ERROR in native method: JDWP No transports initialized, jvmtiError=AGENT_ERROR_TRANSPORT_INIT

    FATAL ERROR in native method:JDWP No transports initialized,jvmtiError=AGENT_ERROR_TRANSPORT_INIT(19 ...

  5. sql查询语句中的乱码 -- 前面加N

    直接运行sql出出现乱码,在中文字符前加N就能够正常显示了.N的含义就是用nvarchar格式显示.

  6. Cocos2d-x 动手实现游戏主循环

    因为Cocos2d-x封装的非常好,所以对于非常多新手,他们仅仅知道先new一个场景,在场景上加入布景或精灵,然后用Director的runWithScene便能够执行游戏了.假设给一个精灵加个动作, ...

  7. mysql基础综述(四)

    1.数据库的简单介绍 1.1 数据库,就是一个文件系统,使用标准sql对数据库进行操作 1.2 常见的数据库 oracle  是oracle公司的数据库,是一个收费的大型的数据库 DB2,是IBM公司 ...

  8. Android SQLiteDatabase分析

    Android中的数据存储使用的小巧的SQLite数据库. 为了方便java层使用SQLite,android做了大量的封装.提供了一些列的类和API.本文章就揭露这些封装背后的类图关系. 老规矩,首 ...

  9. PHP 上传文件到其他服务器

    PHP 上传文件到其他服务器 标签(空格分隔): 安装Guzzle类库 **guzzle** 是发送网络请求的类库 composer安装:**composer require guzzlehttp/g ...

  10. 1.matlab基础准备及入门

    1.1 Command Window(命令行窗口)运用入门 1 计算器的用法 2 数值变量与表达式 3. 计算结果的图形表示 代码及注释 function [ output_args ] = Unti ...