【UVa】Palindromic Subsequence(dp+字典序)
最长的很简单,将串翻转过来后求两个串的lcs就是答案。。
主要是字典序那里。。。
还是开string来比较吧。。
注意最后输出方案时用前半段推出后半段。(因为可能lcs时会重合。。。)
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mkpii make_pair<int, int>
#define pdi pair<double, int>
#define mkpdi make_pair<double, int>
#define pli pair<ll, int>
#define mkpli make_pair<ll, int>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } const int N=1005;
char s1[N], s2[N];
int f[N][N], mn[N][N];
string p[N][N];
int main() {
while(~scanf("%s", s1+1)) {
CC(mn, 0x3f);
int n=strlen(s1+1);
for1(i, 1, n) s2[n-i+1]=s1[i];
for1(i, 1, n) for1(j, 1, n) {
if(s1[i]==s2[j]) f[i][j]=f[i-1][j-1]+1, p[i][j]=p[i-1][j-1]+s1[i];
else {
f[i][j]=max(f[i-1][j], f[i][j-1]);
if(f[i-1][j]==f[i][j-1]) p[i][j]=min(p[i-1][j], p[i][j-1]);
else if(f[i-1][j]>f[i][j-1]) p[i][j]=p[i-1][j];
else p[i][j]=p[i][j-1];
}
}
int len=(f[n][n]+1)>>1;
string ans1(p[n][n].begin(), p[n][n].begin()+len);
string ans2(ans1.rbegin()+(f[n][n]&1), ans1.rend());
printf("%s\n", (ans1+ans2).c_str());
}
return 0;
}
【UVa】Palindromic Subsequence(dp+字典序)的更多相关文章
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA 11404 Palindromic Subsequence
Palindromic Subsequence Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA ...
- UVA 11404 五 Palindromic Subsequence
Palindromic Subsequence Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu ...
- LPS UVA 11404 Palindromic Subsequence
题目传送门 题意:求LPS (Longest Palidromic Subsequence) 最长回文子序列.和回文串不同,子序列是可以不连续的. 分析:1. 推荐->还有一种写法是用了LCS的 ...
- [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 ...
- [Swift]LeetCode516. 最长回文子序列 | Longest Palindromic Subsequence
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- 516. Longest Palindromic Subsequence最长的不连续回文串的长度
[抄题]: Given a string s, find the longest palindromic subsequence's length in s. You may assume that ...
- 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
Longest Palindromic Subsequence 题解 题目来源:https://leetcode.com/problems/longest-palindromic-subsequenc ...
随机推荐
- 【PM】关于系统数据库和服务现场升级的一些看法
工作快满一年了,立即着手准备第二次出差去升级我们的系统,可是突然想到一件事情,让我颇有感触,是关于系统现场升级的. 我们迭代开发的系统隔一段时间就会须要到用户的现场去为其进行系统升级,当中升级包含cl ...
- QtGui.QFileDialog
The QtGui.QFileDialog is a dialog that allows users to select files or directories. The files can be ...
- Decorator Pattern (装饰者模式)
装饰者模式( Decorator Pattern ) 意图 : 动态的给一个对象添加一些额外的功能,IO这块内容体现出了装饰模式,Decorator模式相比生成子类更为灵活. 角色 : 1)抽象构件角 ...
- 【CODEFORCES】 A. Keyboard
A. Keyboard time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- java接口的高级应用
直接上菜 /*接口类*/ public interface MsgListener{ public void afterMsgRecived(String msgData); } /*工具类*/ pu ...
- Java Tread多线程(1)实现Runnable接口
作者 : 卿笃军 原文地址:http://blog.csdn.net/qingdujun/article/details/39347245 本文演示,Tread多线程实现Runnable接口,以及简单 ...
- FFmpeg命令添加视频字幕
FFmpeg添加字幕 首先需要科普下.vob,mkv等格式文件以流的形式存储字幕,而mp4不支持这种方式.如果希望生成带字幕的mp4文件,只能将字幕“烧录”到视频中. 也就是说我们需要将字幕流与视频流 ...
- FFMPEG,将字幕“烧进”MP4视频中
原文地址:http://blog.csdn.net/ufocode/article/details/75475539 由于mp4容器,不像MKV等容器有自己的字幕流. MKV这种容器的视频格式中,会带 ...
- 工作总结 @Html 辅助方法 为 生成的 标签设置元素属性 htmlAttributes 一个对象,其中包含要为该元素设置的 HTML 特性。
@Html.RadioButtonFor(m => m.IsJianChe, true, new { @style = "width: 18px; height: 18px;" ...
- 三维模型 DAE 导出格式结合 OpenGLES 要素浅析
三维模型 DAE 导出格式结合 OpenGLES 要素浅析 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致&quo ...