UVA 11404 - Palindromic Subsequence

求给定字符串的最长回文子序列,长度一样的输出字典序最小的。

对于 [l, r] 区间的最长回文串。他可能是[l+1, r] 和[l, r-1]两个区间的结果。

或者当s[l] == s[r]时,区间[l+1, r-1]的结果再加上以s[l], s[r]为首尾的子序列。

dp[l][r] = ans(dp[l][r-1], dp[l+1][r], s[l] + dp[l+1][r-1] + s[r]) 

dp存的是子序列,即一个string。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int MAXN = 1000 + 5; string s;
string dp[MAXN][MAXN]; void init() {
for (int i=0; i<MAXN; i++) {
for (int j=0; j<MAXN; j++) {
dp[i][j] = "#";
}
}
} string DP(int l, int r) {
if (dp[l][r] != "#") return dp[l][r];
if (l > r) return dp[l][r] = "";
if (l == r) return dp[l][r] = string(1, s[l]); string a, b, c, res; a = DP(l+1, r);
b = DP(l, r-1);
c = DP(l+1, r-1); if (a.size() < b.size()) res = b;
else if (a.size() > b.size()) res = a;
else if (a < b) res = a;
else res = b; if (s[l] == s[r]) {
c = string(1, s[l]) + c + string(1, s[r]);
}
if (res.size() == c.size()) {
if (res > c) res = c;
} else if (res.size() < c.size()) res = c; return dp[l][r] = res;
} int main () {
for (; cin >> s; ) { init(); string ans = DP(0, s.size()-1); cout << ans << endl;
}
return 0;
}

uva 11404 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:0KB     64bit IO Format:%lld & %llu ...

  3. 【UVA 11404】Palindromic Subsequence

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

  4. LPS UVA 11404 Palindromic Subsequence

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

  5. UVA 11404 简单LCS模型DP 字典序比较

    这个题目求某个字符串中含的最长的回文子串. 就是一个很简单的LCS模型吗,而且我不明白为什么网上这么多人都说仿照某写法把字符串先逆序一下,然后求LCS,我只想问一下,有必要吗? 直接按LCS的套路来就 ...

  6. UVA 11404 Palindromic Subsequence

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

  7. uva 1401 dp+Trie

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

  8. uva 11552 dp

    UVA 11552 - Fewest Flops 一个字符串,字符串每 k 个当作一组,组中的字符顺序能够重组.问经过重组后改字符串能够编程最少由多少块字符组成.连续的一段字符被称为块. dp[i][ ...

  9. uva 10271 (dp)

    题意:有n个数据,给定k,要从中选出k+8个三元组(x,y,z,其中x<=y<=z),每选一次的代价为(x-y)^2,求最小代价和. [解题方法] 将筷子按长度从大到小排序 排序原因: 由 ...

随机推荐

  1. luogu 3406 海底高铁 前缀和

    题目链接 题意 给定一个数轴上的若干城市\(1,2,3,...,n\),在第\(i\)到\(i+1\)\((1\leq i\lt n)\)个城市间有铁路,通行方式可为 \(1.\)每次买票(花费\(a ...

  2. Android与H5互调

    前言 微信,微博,微商,QQ空间,大量的软件使用内嵌了H5,这个时候就需要了解Android如何更H5交互的了:有些外包公司,为了节约成本,采用Android内嵌H5模式开发,便于在IOS上直接复用页 ...

  3. Codeforces 707C. Pythagorean Triples-推公式的数学题

    两道C题题解,能推出来公式简直是无敌. http://codeforces.com/problemset/problem/707/C codeforces707C. Pythagorean Tripl ...

  4. Http头 Range、Content-Range

    HTTP头中一般断点下载时才用到Range和Content-Range实体头,Range用户请求头中,指定第一个字节的位置和最后一个字节的位置,如(Range:200-300)Content-Rang ...

  5. The web application [/struts2_0100] created a ThreadLocal with key of type

    引用: 严重: The web application [/struts2_0100] created a ThreadLocal with key of type [com.opensymphony ...

  6. ALBB 找公共最长连续字母序列的长度

    问题描写叙述 给定一个 query 和一个 text .均由小写字母组成.要求在 text 中找出以相同的顺序连续出如今 query 中的最长连续字母序列的长度. 比如, query为"ac ...

  7. Linux内核——内存管理

    内存管理 页 内核把物理页作为内存管理的基本单位.内存管理单元(MMU,管理内存并把虚拟地址转换为物理地址)通常以页为单位进行处理.MMU以页大小为单位来管理系统中的页表. 从虚拟内存的角度看,页就是 ...

  8. maven setting.xml 配置(有效)

    <?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://mav ...

  9. TSRC挑战赛:WAF之SQL注入绕过挑战实录

    转自腾讯 博文作者:TSRC白帽子 发布日期:2014-09-03 阅读次数:1338 博文内容: 博文作者:lol [TSRC 白帽子] 第二作者:Conqu3r.花开若相惜 来自团队:[Pax.M ...

  10. 新版本号的tlplayer for android ,TigerLeapMC for windows公布了

    tlplayer for android 新版本号修正了图像倾斜等等问题,添加了动态水印功能. 支持hls(m3u8),http,rtsp,mms,rtmp等网络协议. 声明tlplayer 上的变速 ...