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. GameCenter 使用指南

    原地址:http://www.cocoachina.com/gamedev/misc/2010/1022/2229.html GameCenter 为单机游戏为主的 iPhone 游戏平台引入了社会化 ...

  2. Jquery重新学习之九[Ajax运用总结C]

    前两篇文章主要介绍Jquery如何利用Ajax进行操作数据,主要介绍调用的方法:其中Jquery.ajax()是Jquery中最底层的方法:Jquery还定义的一个方法跟几个事件为Jquery.aja ...

  3. 安装 Percona XtraBackup 2.3

    Installing Percona XtraBackup from Percona yum repository Install the Percona repository You can ins ...

  4. sphinx相关文章

    sphinx配置文件详解http://yanue.net/post-129.html Sphinx+Scws 搭建千万级准实时搜索&应用场景详解 http://blog.csdn.net/pi ...

  5. ubuntu下apache和mysql的命令

    // Apache //Task: Start Apache 2 Server /启动apache服务 # /etc/init.d/apache2 start //or $ sudo /etc/ini ...

  6. MySQL学习总结(二)数据库以及表的基本操作

    上一节中详细的介绍了关于MySQL数据库的安装过程,接下来我们就该对数据库以及表进行一些基本的操作了. 1.数据类型 MySQL数据库中提供了整数类型.浮点数类型.定点数类型.日期和时间类型.字符串类 ...

  7. C#:列表视图操作类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  8. java线程同步方法,方法块差别

    先说同步方法.它究竟是锁定的当前对象,还是当前类 代码块1 package com.ssss; public class Thread1 implements Runnable { //public ...

  9. AntShares区块链的节点部署与搭建私有链

    近期陆续体验常见的区块链开源项目,比如小蚁OnChain出的AntShares.AntShares跟其它开源区块链项目不一样,他们家是使用C#开发,基于.NET Core执行在Linux,期待未来出G ...

  10. 在ubuntu下安装free pascal

    1.ubuntu16.04版本已经集成了free pascal最新的3.0版,只需要安装就可以了. sudo apt install fp-compiler 在ubuntu里面,fp-compiler ...