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 ...
随机推荐
- android 编程小技巧(持续中)
first: Intent跳转一般存用于Activity类,可是若要在非activity类里跳转的话,解决方法是在startActivity(intent)前加mContext即上下文,终于为 ...
- 每日五题(Spring)
1使用Spring框架的优点是什么? 控制反转: Spring通过控制反转实现了松散耦合,对象们给出它们的依赖,而不是创建或查找依赖的对象们. 面向切面的编程(AOP): Spring支持面向切面的编 ...
- Statelessness Provide credentials with the request. Each request MUST stand alone and should not be affected from previous conversation happened from same client in past.
The server never relies on information from previous requests. Statelessness As per the REST (REpres ...
- YARN commands are invoked by the bin/yarn script.
Apache Hadoop 2.9.0 – YARN Commands http://hadoop.apache.org/docs/stable/hadoop-yarn/hadoop-yarn-sit ...
- org.eclipse.swt.SWTError: No more handles的解决办法
今天装了JBoss Tools 3.1 插件后,eclipse 打开jsp文件老是报错,或者要我关闭: org.eclipse.swt.SWTError: No more handles 网上找了两个 ...
- React中Transition的作用
/** * `Transaction` creates a black box that is able to wrap any method such that * certain invarian ...
- codeforcfes Codeforces Round #287 (Div. 2) B. Amr and Pins
B. Amr and Pins time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- ZOJ3469 Food Delivery —— 区间DP
题目链接:https://vjudge.net/problem/ZOJ-3469 Food Delivery Time Limit: 2 Seconds Memory Limit: 6553 ...
- 【HDU 4819】Mosaic
[题目链接] 点击打开链接 [算法] 二维线段树(树套树) [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 8 ...
- TCP连接、释放及HTTPS连接流程
一.建立连接是三次握手 为什么三次握手?前两次握手为了确认服务端能正常收到客户端的请求并愿意应答,后两次握手是为了确认客户端能正常收到服务端的请求并愿意应答.三次握手可以避免意外建立错误连接而导致浪费 ...