UVa 11404 Palindromic Subsequence (LCS)
题意:给定一个字符串,问删除一些字符,使得它成为一个最长回访串,如果有多个,输出字典序最小的那个。
析: 我们可以把原字符串反转,然后求两个串的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)的更多相关文章
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- LPS UVA 11404 Palindromic Subsequence
题目传送门 题意:求LPS (Longest Palidromic Subsequence) 最长回文子序列.和回文串不同,子序列是可以不连续的. 分析:1. 推荐->还有一种写法是用了LCS的 ...
- UVA 11404 Palindromic Subsequence
Palindromic Subsequence Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA ...
- 【UVa】Palindromic Subsequence(dp+字典序)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=465&page=s ...
- uva 11404 dp
UVA 11404 - Palindromic Subsequence 求给定字符串的最长回文子序列,长度一样的输出字典序最小的. 对于 [l, r] 区间的最长回文串.他可能是[l+1, r] 和[ ...
- UVA 11404 五 Palindromic Subsequence
Palindromic Subsequence Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu ...
- 【UVA 11404】Palindromic Subsequence
UVA 11404 我用了最暴力的做法:考虑\(dp[i][j]\)表示\(S[i..j]\)的最长回文子序列的长度以及字典序最小的那个. 然后转移的时候如下处理:首先\(dp[i][j]\)要取\( ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- [leetcode-516-Longest Palindromic Subsequence]
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
随机推荐
- NHibernate学习系列一
NHibernate是一个面向.NET环境的对象/关系数据库映射工具.对象/关系数据库映射(object/relational mapping,ORM)这个术语表示一种技术,用来把对象模型表示的对象映 ...
- php验证身份证号码有效性
<?php // 18位身份证校验码有效性检查 // idcard_checksum18('...'); function idcard_checksum18($idcard) { if (st ...
- HTML 学习笔记 JQueryUI(Interactions,Widgets)
Draggable 允许使用鼠标移动元素(拖动) demo <html> <head> <meta charset="UTF-8" ...
- .net 调用SAP RFC的几种方法
转自:http://www.cherpservice.com/pub/newsdetail.asp?Newsid=3613 第一种方式采用SAP.net Connector: 最新版本是3.,不开源, ...
- Mysql 外键级联
如果表A的主关键字是表B中的字段,则该字段称为表B的外键,表A称为主表,表B称为从表.外键是用来实现参照完整性的,不同的外键约束方式将可以使两张表紧密的结合起来,特别是修改或者删除的级联操作将使得日常 ...
- SpringBoot学习笔记(1):配置Mybatis
SpringBoot学习笔记(1):配置Mybatis 反思:如果自己写的笔记自己都看不懂,那就不要拿出来丢人现眼! IDEA插件 Free MyBatis Plugin插件可以让我们的MyBatis ...
- Linux线程的几种结束方式
Linux创建线程使用 int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) ...
- Git core objects
Git core objects Core objects in git blob object tree object commit object Git low level commands gi ...
- [HDU4609] 3-idiots FFT+计数
用FFT再去重计算出两条边加起来为某个值得方案数,然后用总方案数减去不合法方案数即可. #include<iostream> #include<cstdio> #include ...
- hdu 2015校赛1002 Dual horsetail (思维题 )
Dual horsetail Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...