题意:给定一个字符串,问删除一些字符,使得它成为一个最长回访串,如果有多个,输出字典序最小的那个。

析: 我们可以把原字符串反转,然后求两个串的LCS,就得到最长回文串,不过要注意一些细节。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#define debug() puts("++++");
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000 + 5;
const int mod = 2000;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Node{
int val;
string str;
bool operator > (const Node &p) const{
if(val != p.val) return val > p.val;
return str < p.str;
}
};
Node dp[maxn][maxn];
char s[maxn]; int main(){
ios::sync_with_stdio(false);
while(cin >> s+1){
n = strlen(s+1);
for(int i = n; i > 0; --i){
dp[i][i].val = 1;
dp[i][i].str = s[i];
for(int j = i+1; j <= n; ++j)
if(s[i] == s[j]) dp[i][j].val = dp[i+1][j-1].val + 2, dp[i][j].str = s[i] + dp[i+1][j-1].str + s[j];
else if(dp[i+1][j] > dp[i][j-1]) dp[i][j] = dp[i+1][j];
else dp[i][j] = dp[i][j-1];
}
cout << dp[1][n].str << endl;
}
return 0;
}

UVa 11404 Palindromic Subsequence (LCS)的更多相关文章

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

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

  2. LPS UVA 11404 Palindromic Subsequence

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

  3. UVA 11404 Palindromic Subsequence

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

  4. 【UVa】Palindromic Subsequence(dp+字典序)

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

  5. uva 11404 dp

    UVA 11404 - Palindromic Subsequence 求给定字符串的最长回文子序列,长度一样的输出字典序最小的. 对于 [l, r] 区间的最长回文串.他可能是[l+1, r] 和[ ...

  6. UVA 11404 五 Palindromic Subsequence

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

  7. 【UVA 11404】Palindromic Subsequence

    UVA 11404 我用了最暴力的做法:考虑\(dp[i][j]\)表示\(S[i..j]\)的最长回文子序列的长度以及字典序最小的那个. 然后转移的时候如下处理:首先\(dp[i][j]\)要取\( ...

  8. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  9. [leetcode-516-Longest Palindromic Subsequence]

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

随机推荐

  1. 基于live555实现的RTSPServer对底层进行性能优化的方法

    在博客<EasyIPCamera高性能摄像机RTSP服务器RTSPServer解决方案>我介绍了基于live555实现的一套RTSPServer功能组件,当时开发者经过几个月的调试,已经将 ...

  2. 【网络与系统安全】20179209 利用metasploit对windows系统的渗透

    这次实验的主角是素有"内网杀手"之称的metasploit.还是少说一些夸赞它的话(因为即使功能再强大,不明白它的原理,不会灵活使用它集成的功能,一样没有用),我们直入主题.简单说 ...

  3. org.apache.catalina.Lifecycle

    org.apache.catalina.Lifecycle start() *  ----------------------------- *  |                         ...

  4. 各种 Spring-Boot-Starters系列 介绍

     分类: JavaWeb后端(6)      目录(?)[+]   原文传送门:Spring Boot application starters ↑传送门里有各Spring-Boot-Starters ...

  5. IC卡、ID卡、M1卡、射频卡的区别是什么【转】

    本文转载自:https://www.cnblogs.com/najifu-jason/p/4122741.html IC卡.ID卡.M1卡.射频卡都是我们常见的一种智能卡,但是很多的顾客还是不清楚IC ...

  6. Linux2.4文件系统中vfsmount、安装点的dentry、设备的dentry之间的关系【转】

    本文转载自:https://blog.csdn.net/mishifangxiangdefeng/article/details/7566575 1.vfsmount.安装点的dentry.设备的de ...

  7. Spring Boot2.0之整合多数据源

    一般公司分两个数据库: 一个放共同配置文件, 一个数据库垂直业务数据库 垂直拆分和水平拆分: 垂直是根据业务划分具体数据库 在一个项目中有多个数据源(不同库jdbc) 无限个的哈~ 根据包名 或者 注 ...

  8. tensorflow实现svm iris二分类——本质上在使用梯度下降法求解线性回归(loss是定制的而已)

    iris二分类 # Linear Support Vector Machine: Soft Margin # ---------------------------------- # # This f ...

  9. 时间序列数据库概览——基于文件(RRD)、K/V数据库(influxDB)、关系型数据库

    一般人们谈论时间序列数据库的时候指代的就是这一类存储.按照底层技术不同可以划分为三类. 直接基于文件的简单存储:RRD Tool,Graphite Whisper.这类工具附属于监控告警工具,底层没有 ...

  10. Java微信公众平台开发_05_微信网页授权

    GitHub源码:https://github.com/shirayner/weixin_gz 一.本节要点 1.网页授权回调域名 登录微信公众平台后台, 开发 - 接口权限 - 网页服务 - 网页帐 ...