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 ...
随机推荐
- “王者对战”之 MySQL 8 vs PostgreSQL 10
既然 MySQL 8 和 PostgreSQL 10 已经发布了,现在是时候回顾一下这两大开源关系型数据库是如何彼此竞争的. 在这些版本之前,人们普遍认为,Postgres 在功能集表现更出色,也因其 ...
- ASP.NET CORE--WIN10上无法单步调试解决方法
参考这篇文章 http://www.cnblogs.com/artech/p/debug-in-vs-code.html In order to be able to debug cross-plat ...
- HDU 4352
#include <iostream> #include <cstdio> #include <cmath> #include <string.h> # ...
- 【Android 初学】13、Broadcast Receiver
Broadcast Receiver Android广播机制包括三个基本要素:广播(Broadcast) - 用于发送广播.广播接收器(BroadcastReceiver) - 用于接收广播:意图内容 ...
- How to improve Java's I/O performance( 提升 java i/o 性能)
原文:http://www.javaworld.com/article/2077523/build-ci-sdlc/java-tip-26--how-to-improve-java-s-i-o-per ...
- UVA 1541 - To Bet or Not To Bet 记忆化DP概率
Alexander Charles McMillan loves to gamble, and during his last trip to the casino he ran across a n ...
- 使用CNN做文本分类——将图像2维卷积换成1维
使用CNN做文本分类 from __future__ import division, print_function, absolute_import import tensorflow as tf ...
- CXF WebService中传递复杂对象(List、Map、Array)
转自:https://wenku.baidu.com/view/047ce58ed0d233d4b14e69eb.html 现在开始介绍传递复杂类型的对象.如JavaBean.Array.List.M ...
- asp.net 后台给前台控件添加及设置属性
txtTopImgBox.Attributes.Add("title", "12312121"); Text1.Style["background-c ...
- ubuntu16.04 安装 docker-compose
下载安装 docker-composecurl -L https://github.com/docker/compose/releases/download/1.15.0/docker-compose ...