uva 11404 dp
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的更多相关文章
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- 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]\)要取\( ...
- LPS UVA 11404 Palindromic Subsequence
题目传送门 题意:求LPS (Longest Palidromic Subsequence) 最长回文子序列.和回文串不同,子序列是可以不连续的. 分析:1. 推荐->还有一种写法是用了LCS的 ...
- UVA 11404 简单LCS模型DP 字典序比较
这个题目求某个字符串中含的最长的回文子串. 就是一个很简单的LCS模型吗,而且我不明白为什么网上这么多人都说仿照某写法把字符串先逆序一下,然后求LCS,我只想问一下,有必要吗? 直接按LCS的套路来就 ...
- UVA 11404 Palindromic Subsequence
Palindromic Subsequence Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA ...
- uva 1401 dp+Trie
http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva 11552 dp
UVA 11552 - Fewest Flops 一个字符串,字符串每 k 个当作一组,组中的字符顺序能够重组.问经过重组后改字符串能够编程最少由多少块字符组成.连续的一段字符被称为块. dp[i][ ...
- uva 10271 (dp)
题意:有n个数据,给定k,要从中选出k+8个三元组(x,y,z,其中x<=y<=z),每选一次的代价为(x-y)^2,求最小代价和. [解题方法] 将筷子按长度从大到小排序 排序原因: 由 ...
随机推荐
- 【HDOJ5558】Alice's Classified Message(后缀数组)
题意:给定一个长度为n的下标从0开始的小写字母字符串,每次对于当前的i寻找一个j使得后缀i与后缀j的lcp最大,(j<i)若lcp相同则取较小的 若lcp为0则输出0与当前字符,i自增1,否则输 ...
- 【原创】datalist repeater 控件的行鼠标单击 以及 滑过特效
原文发布时间为:2009-05-06 -- 来源于本人的百度文章 [由搬家工具导入] 调用方法如:<tr id="<%# Container.ItemIndex+1 %>& ...
- 【Linux】自主实现my_sleep【转】
转自:http://blog.csdn.net/scenlyf/article/details/52068522 版权声明:本文为博主原创文章,未经博主允许不得转载. 首先说一下信号相关的内容. ** ...
- Codeforces 716C. Plus and Square Root-推公式的数学题
http://codeforces.com/problemset/problem/716/C codeforces716C. Plus and Square Root 这个题就是推,会推出来规律,发现 ...
- QUICK START GIT
install git at you laptop https://git-scm.com/downloads config git at you laptop git config --global ...
- MySQL 如何优化cpu消耗
目录 谁在消耗cpu? 祸首是谁? 用户 IO等待 产生影响 如何减少CPU消耗? 减少等待 减少计算 升级cpu 谁在消耗cpu? 用户+系统+IO等待+软硬中断+空闲 祸首是谁? 用户 用户空间C ...
- Codeforces Round #298 (Div. 2) A、B、C题
题目链接:Codeforces Round #298 (Div. 2) A. Exam An exam for n students will take place in a long and nar ...
- php的session与免登陆问题
Session 与 Session的GC 由于PHP的工作机制,它并没有一个daemon线程来定期的扫描Session 信息并判断其是否失效,当一个有效的请求发生时,PHP 会根据全局变量 sessi ...
- 【java】Map、Set、List不同数据结构的各种不同循环迭代的效率对比,使用场景
Map.Set.List不同数据结构的各种不同循环迭代的效率对比,使用场景 引申一个地址:Map迭代的使用keySet和entitySet的效率
- 【spring cloud】【spring boot】项目启动报错:Cannot determine embedded database driver class for database type NONE
解决参考文章:https://blog.csdn.net/hengyunabc/article/details/78762097 spring boot启动报错如下: Error starting A ...