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

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

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #include <unordered_map>
  17. #include <unordered_set>
  18. #define debug() puts("++++");
  19. #define freopenr freopen("in.txt", "r", stdin)
  20. #define freopenw freopen("out.txt", "w", stdout)
  21. using namespace std;
  22.  
  23. typedef long long LL;
  24. typedef pair<int, int> P;
  25. const int INF = 0x3f3f3f3f;
  26. const double inf = 0x3f3f3f3f3f3f;
  27. const double PI = acos(-1.0);
  28. const double eps = 1e-8;
  29. const int maxn = 1000 + 5;
  30. const int mod = 2000;
  31. const int dr[] = {-1, 1, 0, 0};
  32. const int dc[] = {0, 0, 1, -1};
  33. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  34. int n, m;
  35. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  36. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  37. inline bool is_in(int r, int c){
  38. return r >= 0 && r < n && c >= 0 && c < m;
  39. }
  40. struct Node{
  41. int val;
  42. string str;
  43. bool operator > (const Node &p) const{
  44. if(val != p.val) return val > p.val;
  45. return str < p.str;
  46. }
  47. };
  48. Node dp[maxn][maxn];
  49. char s[maxn];
  50.  
  51. int main(){
  52. ios::sync_with_stdio(false);
  53. while(cin >> s+1){
  54. n = strlen(s+1);
  55. for(int i = n; i > 0; --i){
  56. dp[i][i].val = 1;
  57. dp[i][i].str = s[i];
  58. for(int j = i+1; j <= n; ++j)
  59. 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];
  60. else if(dp[i+1][j] > dp[i][j-1]) dp[i][j] = dp[i+1][j];
  61. else dp[i][j] = dp[i][j-1];
  62. }
  63. cout << dp[1][n].str << endl;
  64. }
  65. return 0;
  66. }

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. NHibernate学习系列一

    NHibernate是一个面向.NET环境的对象/关系数据库映射工具.对象/关系数据库映射(object/relational mapping,ORM)这个术语表示一种技术,用来把对象模型表示的对象映 ...

  2. php验证身份证号码有效性

    <?php // 18位身份证校验码有效性检查 // idcard_checksum18('...'); function idcard_checksum18($idcard) { if (st ...

  3. HTML 学习笔记 JQueryUI(Interactions,Widgets)

    Draggable 允许使用鼠标移动元素(拖动) demo <html>    <head>        <meta charset="UTF-8" ...

  4. .net 调用SAP RFC的几种方法

    转自:http://www.cherpservice.com/pub/newsdetail.asp?Newsid=3613 第一种方式采用SAP.net Connector: 最新版本是3.,不开源, ...

  5. Mysql 外键级联

    如果表A的主关键字是表B中的字段,则该字段称为表B的外键,表A称为主表,表B称为从表.外键是用来实现参照完整性的,不同的外键约束方式将可以使两张表紧密的结合起来,特别是修改或者删除的级联操作将使得日常 ...

  6. SpringBoot学习笔记(1):配置Mybatis

    SpringBoot学习笔记(1):配置Mybatis 反思:如果自己写的笔记自己都看不懂,那就不要拿出来丢人现眼! IDEA插件 Free MyBatis Plugin插件可以让我们的MyBatis ...

  7. Linux线程的几种结束方式

    Linux创建线程使用 int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) ...

  8. Git core objects

    Git core objects Core objects in git blob object tree object commit object Git low level commands gi ...

  9. [HDU4609] 3-idiots FFT+计数

    用FFT再去重计算出两条边加起来为某个值得方案数,然后用总方案数减去不合法方案数即可. #include<iostream> #include<cstdio> #include ...

  10. hdu 2015校赛1002 Dual horsetail (思维题 )

    Dual horsetail Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...