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. Cocos2d-x 3.2 Lua演示样例 AssetsManagerTest(资源管理器)

    Cocos2d-x 3.2 Lua演示样例 AssetsManagerTest(资源管理器) 本篇博客介绍Cocos2d-x 为我们提供的一个类--AssetsManager在Lua中的使用样例,效果 ...

  2. 使用JavaMelody监控tomcat以及jvm

    JavaMelody用于对Java应用或者应用服务器的QA以及开发环境的监控.它并不是一个模拟请求类似JMeter的压力测试工具,而是一个衡量并且计算在应用上的操作信息的工具,也就是说,它只负责对行为 ...

  3. Object.defineProperty 监听对象属性变化

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  4. CSerialPort串口类最新修正版(解决关闭死锁问题)2014-01-11

    这是一份优秀的类文件,好多的地方值得我们学习,具体在多线程,事件,自定义消息,类的封装方面等等.Remon提供的串口类网址为:http://codeguru.earthweb.com/network/ ...

  5. Android应用开发揭秘之优化技术

    2013-06-28 第15章 优化技术   不管用什么语言进行开发,所有的优秀代码都会展示出共有的经典品质: 简练,可读性强,模块化,层次性,设计良好,高效,优雅,清晰等.   Java程序员能够依 ...

  6. 【Redis】redis 五种数据结构详解(string,list,set,zset,hash)

    redis 五种数据结构详解(string,list,set,zset,hash) Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存 ...

  7. Python-try except else finally有return时执行顺序探究

    学习python或者其他有异常控制的编程语 言, 大家很有可能说try except finally(try catch finally)的执行很简单,无非就是有异常的话执行except, final ...

  8. [bug]未能从程序集“System.ServiceModel, Version=3.0.0.0问题解决

    在Windows Server 2008中的IIS服务器中部署WCF服务程序时,通过浏览器访问报出如下错误: 未能从程序集“System.ServiceModel, Version=3.0.0.0, ...

  9. Python 爬虫实例(1)—— 爬取百度图片

    爬取百度图片  在Python 2.7上运行 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author: loveNight import jso ...

  10. unity, access material

    MeshRenderer meshRenderer=gameObject.GetComponent<MeshRenderer>();                if(meshRende ...