LPS UVA 11404 Palindromic Subsequence
题意:求LPS (Longest Palidromic Subsequence) 最长回文子序列。和回文串不同,子序列是可以不连续的。
分析:1. 推荐->还有一种写法是用了LCS的思想,dp[i][j]表示i到j的最长回文串长度,状态转移方程:
1. dp[j][j+i-1] = dp[j+1][j+i-2] + 2; (str[j] == str[j+i-1])
2. dp[j][j+i-1] = max (dp[j+1][j+i-1], dp[j][j+i-2]); (str[j] != str[j+i-1])
2. 转化为LCS问题,将字符串逆序,然后和本串求LCS就是LPS的长度,但是前一半是LPS的一半,可以补全
代码1:
/************************************************
* Author :Running_Time
* Created Time :2015-8-7 14:26:22
* File Name :UVA_11404.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 1e3 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
string ans[MAXN][MAXN];
int dp[MAXN][MAXN];
char str[MAXN]; void LPS(void) {
int len = strlen (str + 1);
memset (dp, 0, sizeof (dp));
for (int i=1; i<=len; ++i) dp[i][i] = 1;
for (int i=1; i<=len; ++i) ans[i][i] = str[i]; for (int i=2; i<=len; ++i) { //区间长度
for (int j=1; j+i-1<=len; ++j) { //[j, j+i-1]
if (str[j] == str[j+i-1]) {
if (i == 2) {
dp[j][j+i-1] = 2;
ans[j][j+i-1] = ans[j][j] + ans[j+i-1][j+i-1]; continue;
}
dp[j][j+i-1] = dp[j+1][j+i-2] + 2;
ans[j][j+i-1] = str[j] + ans[j+1][j+i-2] + str[j+i-1];
}
else if (dp[j+1][j+i-1] > dp[j][j+i-2]) {
dp[j][j+i-1] = dp[j+1][j+i-1];
ans[j][j+i-1] = ans[j+1][j+i-1];
}
else if (dp[j][j+i-2] > dp[j+1][j+i-1]) {
dp[j][j+i-1] = dp[j][j+i-2];
ans[j][j+i-1] = ans[j][j+i-2];
}
else {
dp[j][j+i-1] = dp[j+1][j+i-1];
ans[j][j+i-1] = min (ans[j+1][j+i-1], ans[j][j+i-2]);
}
}
}
int mlen = dp[1][len];
for (int i=0; i<mlen; ++i) {
printf ("%c", ans[1][len][i]);
}
puts ("");
} int main(void) { //UVA 11404 Palindromic Subsequence
while (scanf ("%s", str + 1) == 1) {
LPS ();
} return 0;
}
代码2:
/************************************************
* Author :Running_Time
* Created Time :2015-8-7 14:26:22
* File Name :UVA_11404.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 1e3 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
struct Ans {
int len;
string s;
}dp[MAXN][MAXN];
char str[MAXN], rstr[MAXN]; int main(void) {
while (scanf ("%s", str + 1) == 1) {
int len = strlen (str + 1);
for (int i=1; i<=len; ++i) {
rstr[len-i+1] = str[i];
}
for (int i=0; i<=len; ++i) {
dp[0][i].len = 0, dp[0][i].s = "";
}
for (int i=1; i<=len; ++i) {
for (int j=1; j<=len; ++j) {
if (str[i] == rstr[j]) {
dp[i][j].len = dp[i-1][j-1].len + 1;
dp[i][j].s = dp[i-1][j-1].s + str[i];
}
else if (dp[i][j-1].len > dp[i-1][j].len) {
dp[i][j].len = dp[i][j-1].len;
dp[i][j].s = dp[i][j-1].s;
}
else if (dp[i-1][j].len > dp[i][j-1].len) {
dp[i][j].len = dp[i-1][j].len;
dp[i][j].s = dp[i-1][j].s;
}
else {
dp[i][j].len = dp[i-1][j].len;
dp[i][j].s = min (dp[i-1][j].s, dp[i][j-1].s);
}
}
}
int mlen = dp[len][len].len;
string ans = dp[len][len].s;
for (int i=0; i<mlen/2; ++i) printf ("%c", ans[i]);
int j;
if (mlen & 1) j = mlen / 2;
else j = mlen / 2 - 1;
for (; j>=0; --j) printf ("%c", ans[j]);
puts ("");
} return 0;
}
LPS UVA 11404 Palindromic Subsequence的更多相关文章
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA 11404 Palindromic Subsequence
Palindromic Subsequence Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA ...
- UVa 11404 Palindromic Subsequence (LCS)
题意:给定一个字符串,问删除一些字符,使得它成为一个最长回访串,如果有多个,输出字典序最小的那个. 析: 我们可以把原字符串反转,然后求两个串的LCS,就得到最长回文串,不过要注意一些细节. 代码如下 ...
- 【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]\)要取\( ...
- [leetcode-516-Longest Palindromic Subsequence]
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- [LeetCode] Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
随机推荐
- Sharepoint2013 列表的NewForm 页面加入一个 保存新建 button
昨天一同事问我怎样在sharepoint2013的NewForm.aspx页面上加入一个 save and new的button.实现save 和new的功能.save的功能和默认的save按钮效果一 ...
- 三分钟教你学Git(十四) 之 线下传输仓库
有时候还有一个人不能从远程直接clone仓库或者说由于非常大,clone非常慢或其他原因.我们能够使用bundle命令将Git仓库打包,然后通过U盘或者是其他介质拷贝给他,这样他拿到打包好的仓库后能够 ...
- JVM的CPU资源占用过高问题的排查
互联网后端架构 https://mp.weixin.qq.com/s/LiqAy2DikbmZzqogb5XRdA JVM的CPU资源占用过高问题的排查 互联网后端架构 今天 上午线上某应用的一台J ...
- Get Luffy Out (poj 2723 二分+2-SAT)
Language: Default Get Luffy Out Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7969 ...
- Netty实现时间服务演示样例
相关知识点: [1] ChannelGroup是一个容纳打开的通道实例的线程安全的集合,方便我们统一施加操作.所以在使用的过程中能够将一些相关的Channel归类为一个有意义的集合.关闭的通道会自己主 ...
- Zookeeper原理和应用
ZooKeeper基本原理 数据模型 如上图所示,ZooKeeper数据模型的结构与Unix文件系统很类似,整体上可以看作是一棵树,每个节点称做一个ZNode.每个ZNode都可以通过其路径唯一标识, ...
- eureka-注册中心使用密码验证
spring cloud 1.1 版本之后可以使用 配置文件: bootstrap.yml server.port: 9000 spring.application.name: registry eu ...
- HDU2594 Simpsons’ Hidden Talents —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-2594 Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Oth ...
- EhCache+Redis实现分布式缓存
Ehcache集群模式 由于 EhCache 是进程中的缓存系统,一旦将应用部署在集群环境中,每一个节点维护各自的缓存数据,当某个节点对缓存数据进行更新,这些更新的数据无法在其它节点中共享,这不仅会降 ...
- Python作业之购物商城
作业:购物商场 1.商品展示,价格 2.银行卡余额 3.付账 程序流程图如下: 代码如下: ShopDisplay = {'} print(ShopDisplay) ShoppingCartPrice ...