UVA 11404 Plalidromic Subsquence (回文子序列,LCS)
最长回文子序列可以用求解原串s和反转串rv的LCS来得到,因为要求回文串分奇偶,dp[i][j]保存长度,
要求字典序最小,dp[i][j]应该表示回文子序列的端点,所以边界为单个字符,即i+j=len+1。
这题最麻烦的地方在于字典序,我是写了个比较函数,有点暴力(常数大)。
也可以反着定义,这时结点就要保存那个状态的字符串了(这样定义比较字典序的时候常数小)
#include<bits/stdc++.h>
using namespace std; #define MP make_pair
#define fi first
#define se second const int LEN = 1e3+;
char s[LEN],rv[LEN];
int dp[LEN][LEN];
pair<int,int> pre[LEN][LEN];
char val[LEN][LEN]; inline void updata(int i,int j,int v,char c,const pair<int,int> &prv)
{
dp[i][j] = v;
pre[i][j] = prv;
val[i][j] = c;
} const auto nil = MP(,); #define dim(x) [x.fi][x.se]
bool cmpLex(pair<int,int> a,pair<int,int> b)
{
//
while(a != nil && val[a.fi][a.se] == val[b.fi][b.se]){
a = pre dim(a); b = pre dim(b);
}
return val dim(a) < val dim(b);
} //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif while(gets(s)){
int len = strlen(s);
for(int i = ; i < len; i++){
rv[len--i] = s[i];
}
int hd1,hd2,vl = ;
for(int i = ; i <= len; i++){
int j = len+-i;
dp[i][j] = ; val[i][j] = s[i-]; pre[i][j] = nil;
if(dp[i][j] > vl || ( dp[i][j] == vl && cmpLex( MP(i,j), MP(hd1,hd2) ) ) ){//
vl = dp[i][j];
hd1 = i; hd2 = j;
}
for(int k = ; k < j; k++) dp[i][k] = ;
for(j++; j <= len; j++){
if(s[i-] == rv[j-]){
updata(i,j,dp[i-][j-]+,s[i-],make_pair(i-,j-));
if(dp[i][j] > vl || (dp[i][j] == vl && cmpLex(MP(i,j),MP(hd1,hd2)) ) ){//
vl = dp[i][j];
hd1 = i; hd2 = j;
}
}else {
if(dp[i-][j] > dp[i][j-] || (dp[i-][j] == dp[i][j-] && cmpLex(MP(i-,j),MP(i,j-)) ) ){//
updata(i,j,dp[i-][j],val[i-][j],pre[i-][j]);
}else {
updata(i,j,dp[i][j-],val[i][j-],pre[i][j-]);
}
} }
}
int pv = (vl+)>>,ln = vl; auto u = MP(hd1,hd2);
for(int i = ; i < pv; i++){
s[i] = val[u.fi][u.se];
u = pre[u.fi][u.se];
} s[ln] = '\0';
for(int i = pv; i < ln; i++){
s[i] = s[ln--i];
}
puts(s);
}
return ;
}
UVA 11404 Plalidromic Subsquence (回文子序列,LCS)的更多相关文章
- 最长回文子序列LCS,最长递增子序列LIS及相互联系
最长公共子序列LCS Lintcode 77. 最长公共子序列 LCS问题是求两个字符串的最长公共子序列 \[ dp[i][j] = \left\{\begin{matrix} & max(d ...
- 动态规划求一个序列的最长回文子序列(Longest Palindromic Substring )
1.问题描述 给定一个字符串(序列),求该序列的最长的回文子序列. 2.分析 需要理解的几个概念: ---回文 ---子序列 ---子串 http://www.cnblogs.com/LCCRNblo ...
- 51NOD 1092 回文字符串 LCS
Q:给定一个串,问需要插入多少字符才能使其成为回文串,也就是左右对称的串. 经典求LCS题,即最长公共子序列,不用连续的序列.考虑O(n^2^)解法,求LCS起码得有两个串,题中才给了一个串,另一个需 ...
- NOIP2016提高组初赛(2)四、读程序写结果3、求最长回文子序列
#include <iostream> using namespace std; int lps(string seq, int i, int j) { int len1, len2; i ...
- 最长回文子序列(LPS)
问题描述: 回文是正序与逆序相同的非空字符串,例如"civic"."racecar"都是回文串.任意单个字符的回文是其本身. 求最长回文子序列要求在给定的字符串 ...
- [LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- [LeetCode] Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- [Swift]LeetCode516. 最长回文子序列 | Longest Palindromic Subsequence
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- hdu-4632 Palindrome subsequence (回文子序列计数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632 问题要求回答一串字符串中回文子序列的数量,例如acbca就有 a,c,b,c,a,cc,aa,a ...
随机推荐
- Spark BlockManager 概述
Application 启动的时候: 1. 会在 SparkEnv 中实例化 BlockManagerMaster 和 MapOutputTracker,其中 (a) BlockManagerMast ...
- 洛谷P3147 [USACO16OPEN]262144
P3147 [USACO16OPEN]262144 题目描述 Bessie likes downloading games to play on her cell phone, even though ...
- Claris’ Contest # 2 Day 2 Problem C. Dash Speed(分治+可持久化并查集+树剖)
题面 题解 \(std\)爆栈了→_→ 我们先考虑一个简化的问题,如果只有加边的情况下如何动态维护直径 合并两棵树时,设\(a,b\)为\(A\)的直径的两个端点,\(c,d\)为\(B\)的直径的两 ...
- NoSuchMethodError idea解决jar包冲突
报NoSuchMethodError(使用spring boot框架idea)一般是jar包冲突 Exception in thread"main" java.lang.NoSuc ...
- IDEA开发Spark的漫漫摸索(二)
1 新建Maven项目 特别提醒,Maven项目中有GropId和ArtifactId.GroupId是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构.一般Gru ...
- JIRA reference
Workflow https://confluence.atlassian.com/adminjiracloud/configuring-workflow-schemes-776636598.html ...
- CF447B DZY Loves Strings 贪心
DZY loves collecting special strings which only contain lowercase letters. For each lowercase letter ...
- sql 存储过程-proc
创建 create proc 存储过程名 (参数列表) as sql 语句 go 执行 exec 存储过程名 参数1,参数2 删除 drop procedure 存储过程名 注: 存储过程名最好以_ ...
- input和button不同高 和 rem
rem的使用 浏览器中默认的字体大小是 16px,此时为 100%.当我们在使用 rem 的时候,常常给 html设置为 html{font-size:62.5;},这样的好处是 1rem 刚好为 1 ...
- 4、python数据类型之列表(list)
列表列表常见操作1.索引取值 name_list = ['wang','zhou','li','hu','wu','zhao'] print(name_list[0]) print(name_list ...