UVA 11404 Palindromic Subsequence
Palindromic Subsequence
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的更多相关文章
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- LPS UVA 11404 Palindromic Subsequence
题目传送门 题意:求LPS (Longest Palidromic Subsequence) 最长回文子序列.和回文串不同,子序列是可以不连续的. 分析:1. 推荐->还有一种写法是用了LCS的 ...
- UVa 11404 Palindromic Subsequence (LCS)
题意:给定一个字符串,问删除一些字符,使得它成为一个最长回访串,如果有多个,输出字典序最小的那个. 析: 我们可以把原字符串反转,然后求两个串的LCS,就得到最长回文串,不过要注意一些细节. 代码如下 ...
- 【UVa】Palindromic Subsequence(dp+字典序)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=465&page=s ...
- uva 11404 dp
UVA 11404 - Palindromic Subsequence 求给定字符串的最长回文子序列,长度一样的输出字典序最小的. 对于 [l, r] 区间的最长回文串.他可能是[l+1, r] 和[ ...
- UVA 11404 五 Palindromic Subsequence
Palindromic Subsequence Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu ...
- 【UVA 11404】Palindromic Subsequence
UVA 11404 我用了最暴力的做法:考虑\(dp[i][j]\)表示\(S[i..j]\)的最长回文子序列的长度以及字典序最小的那个. 然后转移的时候如下处理:首先\(dp[i][j]\)要取\( ...
- [leetcode-516-Longest Palindromic Subsequence]
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- [LeetCode] Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
随机推荐
- 循环时自动打开url
'systemutil.Run "C:\Program Files (x86)\HP\QuickTest Professional\samples\flight\app\flight4a.e ...
- 启用QNX系统,海尔智能冰箱或成业界“宝马”
智能家电正处于迅猛发展的态势,国内眼下有非常多企业都在积极布局智能家电,当中又以海尔最为典型.作为家电领域的领头羊,海尔近年来在智能家电领域的动作不小.近期有消息透露.海尔也许会在IFA展会上 ...
- 关于server和虚拟主机的差别
文章都是先由本人个人博客,孙占兴:www.teilim.com,先更新,随后CSDN博客才会更新.掌握第一动态请关注本人主站. 原文链接:http://www.teilim.com/guan-yu-y ...
- 例说Linux内核链表(三)
经常使用的linux内核双向链表API介绍 linux link list结构图例如以下: 内核双向链表的在linux内核中的位置:/include/linux/list.h 使用双向链表的过程,主要 ...
- unity3d面试题摘选(全)
======================================= 数据结构和算法非常重要.图形学也非常重要! 大的游戏公司非常看重个人基础.综合能力.也有的看重你实际工作能力,看你的De ...
- js如何计算浮点数
js中浮点型是如何运算的呢? 例如:var a=0.69; 我想得到6.9 直接这样写 var c=a*10; alert(c); 得到结果是:6.8999999999999995 到网上一搜,有 ...
- Ubuntu16.04系统安装搜狗输入法
前言:正常双击.deb软件包安装搜狗输入法会有bug,需要按照下面操作进行消除错误. 一.下载搜狗输入法Linux版软件包 下载地址为:http://pinyin.sogou.com/linux/ , ...
- POJ 1466 最大独立点集
思路:匈牙利 n-ans/2; // by SiriusRen #include <cstdio> #include <cstring> #define N 505 using ...
- zset 有序集合
zadd key score1 value1 score2 value2 .. 添加元素 redis 127.0.0.1:6379> zadd stu 18 lily 19 hmm 20 lil ...
- zeromq-4.1.2在windows下的编译
作者:朱金灿 来源:http://blog.csdn.net/clever101 zeromq是一个最近比较火的跨平台消息中间件,最近准备研究它,故下载它的源码编译了一下.我是使用VS2008编译的, ...