http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=465&page=show_problem&problem=2399

最长的很简单,将串翻转过来后求两个串的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+字典序)的更多相关文章

  1. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  2. UVA 11404 Palindromic Subsequence

    Palindromic Subsequence Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA ...

  3. UVA 11404 五 Palindromic Subsequence

     Palindromic Subsequence Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu ...

  4. LPS UVA 11404 Palindromic Subsequence

    题目传送门 题意:求LPS (Longest Palidromic Subsequence) 最长回文子序列.和回文串不同,子序列是可以不连续的. 分析:1. 推荐->还有一种写法是用了LCS的 ...

  5. [leetcode-516-Longest Palindromic Subsequence]

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  6. [LeetCode] Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  7. [Swift]LeetCode516. 最长回文子序列 | Longest Palindromic Subsequence

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  8. 516. Longest Palindromic Subsequence最长的不连续回文串的长度

    [抄题]: Given a string s, find the longest palindromic subsequence's length in s. You may assume that ...

  9. 516. Longest Palindromic Subsequence

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  10. [Leetcode] Longest Palindromic Subsequence

    Longest Palindromic Subsequence 题解 题目来源:https://leetcode.com/problems/longest-palindromic-subsequenc ...

随机推荐

  1. Mac安装Myeclipse2015开发环境

    1.下载Myeclipse2015 链接: http://pan.baidu.com/s/1jHe8mFk 密码: qgeb 下载下来后,在安装的时候需要自己设置下安装目标,不然在破解的时候不是太好找 ...

  2. DBCP(一)数据源配置文件

    DBCP是Apache开发的数据源API,使用的话需要导入dbcp jar包.collections jar包.pool jar包. 其数据源匹配的配置文件格式如下:   #连接设置 driverCl ...

  3. centos6.5下使用yum完美搭建LNMP环境(php5.6,mysql5.5,nginx1.10)

    准备工作 配置防火墙,开启80端口.3306端口 不用执行这句:rm -rf /etc/sysconfig/iptables 直接进入修改:vi /etc/sysconfig/iptables 添加8 ...

  4. 【LeetCode】77. Combinations (2 solutions)

    Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ...  ...

  5. 前端异步文件上传组件 Uploader

    Uploader是非常强大的异步文件上传组件,支持ajax.iframe.flash三套方案,实现浏览器的全兼容,调用非常简单,内置多套主题支持 和常用插件,比如验证.图片预览.进度条等,广泛应用于淘 ...

  6. python 并发和线程

    并发和线程 基本概念 - 并行.并发 并行, parallel 互不干扰的在同一时刻做多件事; 如,同一时刻,同时有多辆车在多条车道上跑,即同时发生的概念. 并发, concurrency 同时做某些 ...

  7. 使用meta属性禁用手机浏览器的缩放功能

    在移动设备浏览器上,通过为视口(viewport)设置 meta 属性为 user-scalable=no 可以禁用其缩放(zooming)功能.这样禁用缩放功能后,用户只能滚动屏幕,就能让你的网站看 ...

  8. hdu5293 Tree chain problem 树形dp+线段树

    题目:pid=5293">http://acm.hdu.edu.cn/showproblem.php?pid=5293 在一棵树中,给出若干条链和链的权值.求选取不相交的链使得权值和最 ...

  9. script的延迟执行

    看这段代码: <body> <script src="deffer.js"></script> content </body> de ...

  10. NPM 模块恩仇录

    vue-clickoutside 点击元素以外的东西时会触发的事件.好东西.其实可以利用全局event来判断当前点击的对象来判断也一样.但这个显然更舒服 传送门:https://www.npmjs.c ...