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 ...
随机推荐
- wpf获取目录路径
AppDomain.CurrentDomain.BaseDirectory +文件名即可,简单吧? //获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称. string str5=Appl ...
- 2014.04.17,转帖,关于FFT的结果为什么要除以N
http://www.chinavib.com/forum/viewthread.php?tid=23665&highlight= 关于这个问题,我看到的书好像都没有进行解释,这里我试着解释下 ...
- ”危险“的RESTRICT与GCC的编译优化(编程者对编译器所做的一个“承诺”:使用restrict修饰过的指针,它所指向的内容只能经由该指针修改)
restrict是C99标准中新添加的关键字,对于从C89标准开始起步学习C语言的同学来说(包括我),第一次看到restrict还是相当陌生的.Wikipedia给出的解释如下: In the C p ...
- Android中添加自己的模块 【转】
本文转载自:http://wallage.blog.163.com/blog/static/17389624201021791333695/ 转:http://blog.csdn.net/yili_x ...
- UESTC--1269--ZhangYu Speech(模拟)
ZhangYu Speech Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format: %lld & %llu Submit ...
- [POJ 2279] Mr. Young's Picture Permutations
[题目链接] http://poj.org/problem?id=2279 [算法] 杨氏矩阵与勾长公式 [代码] #include <algorithm> #include <bi ...
- Linux就该这么学 20181010(第十四章DHCP)
参考链接:https://www.linuxprobe.com DHCP动态地址分配协议 作用域:定义一个很大的网段地址池:真正为用户去分配的地址地址池要小于等于作用域排除范围:作用域-地址池租约-默 ...
- c++面向对象程序设计 谭浩强 第一章答案
c++面向对象程序设计 谭浩强 答案 第一章 目录: c++面向对象程序设计 谭浩强 答案 第一章 c++面向对象程序设计 谭浩强 答案 第二章 c++面向对象程序设计 谭浩强 答案 第三章 c++面 ...
- ROS-SLAM-自主导航
前言:无. 前提:已下载并编译了相关功能包集,如还未下载,可通过git下载:https://github.com/huchunxu/ros_exploring.git 一.启动仿真环境 cd ~/ca ...
- 关于 Visual Studio 中文提示的问题
[转自 https://blog.csdn.net/iloli/article/details/51055299] [转自 http://www.cnblogs.com/hellowin/p/6383 ...