Atcoder F - LCS (DP-最长公共子序列,输出字符串)
F - LCS
Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 100100 points
Problem Statement
You are given strings ss and tt. Find one longest string that is a subsequence of both ss and tt.
Notes
A subsequence of a string xx is the string obtained by removing zero or more characters from xx and concatenating the remaining characters without changing the order.
Constraints
- ss and tt are strings consisting of lowercase English letters.
- 1≤|s|,|t|≤30001≤|s|,|t|≤3000
Input
Input is given from Standard Input in the following format:
ss
tt
Output
Print one longest string that is a subsequence of both ss and tt. If there are multiple such strings, any of them will be accepted.
Sample Input 1 Copy
axyb
abyxb
Sample Output 1 Copy
axb
The answer is axb
or ayb
; either will be accepted.
Sample Input 2 Copy
aa
xayaz
Sample Output 2 Copy
aa
Sample Input 3 Copy
a
z
Sample Output 3 Copy
The answer is (an empty string).
Sample Input 4 Copy
abracadabra
avadakedavra
Sample Output 4 Copy
aaadara 题意:给定两个字符串s和t,让你求出这两个字符串的最长公共子序列,并输出最长公共子序列。
思路:先通过DP求出LCS的DP信息,然后再根据DP信息输出对应的字符。
裸题主要看思路。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int dp[][];
char a[maxn];
char b[maxn];
int n,m;
int c[maxn];
int pre[maxn];
int lis[maxn];
int main()
{
scanf("%s",a);
scanf("%s",b);
n=strlen(a);
m=strlen(b);
for(int i=n-;i>=;i--)
{
for(int j=m-;j>=;j--)
{
if(a[i]==b[j])
{
dp[i][j]=dp[i+][j+]+;
}else
{
dp[i][j]=max(dp[i+][j],dp[i][j+]);
}
}
}
// cout<<dp[n-1][m-1]<<endl;
int i=;
int j=;
while(i<n&&j<m)
{
if(a[i]==b[j])
{
putchar(a[i]);
i++;
j++;
}else if(dp[i][j]==dp[i+][j])
{
i++;
}else
{
j++;
}
} return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Atcoder F - LCS (DP-最长公共子序列,输出字符串)的更多相关文章
- 基于DP的LCS(最长公共子序列)问题
最长公共子序列,即给出两个序列,给出最长的公共序列,例如: 序列1 understand 序列2 underground 最长公共序列undernd,长度为7 一般这类问题很适合使用动态规划,其动态规 ...
- POJ 1458 Common Subsequence (DP+LCS,最长公共子序列)
题意:给定两个字符串,让你找出它们之间最长公共子序列(LCS)的长度. 析:很明显是个DP,就是LCS,一点都没变.设两个序列分别为,A1,A2,...和B1,B2..,d(i, j)表示两个字符串L ...
- HDU 1159 Common Subsequence:LCS(最长公共子序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 题意: 求最长公共子序列. 题解: (LCS模板题) 表示状态: dp[i][j] = max ...
- NYOJ 36 LCS(最长公共子序列)
题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=36 最长公共子序列 时间限制:3000 ms | 内存限制:65535 KB ...
- POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56150 Accepted: 19398 Desc ...
- 经典dp 最长公共子序列
首先,说明一下子序列的定义…… 一个序列A={a1,a2,a3,...,an},从中删除任意若干项,剩余的序列叫A的一个子序列. 很明显(并不明显……),子序列……并不需要元素是连续的……(一开始的时 ...
- LCS(最长公共子序列)问题
例题见挑战程序设计竞赛P56 解释:子序列是从原序列中按顺序(可以跳着)抽取出来的,序列是不连续的,这是其和子串最大的区别: 我们可以定义dp数组为dp[i][j],表示的是s1-si和t1-ti对应 ...
- hdu1159 dp(最长公共子序列)
题意:给两个字符串,求这两个字符串的最长公共子序列的长度 因为之前集训的时候做过,所以现在即使会做也并不是什么稀奇的事,依旧为了自己的浅薄感到羞愧啊``` 解法就是通过两个字符串的每个字符互相比较,根 ...
- LCS(最长公共子序列)动规算法正确性证明
今天在看代码源文件求diff的原理的时候看到了LCS算法.这个算法应该不陌生,动规的经典算法.具体算法做啥了我就不说了,不知道的可以直接看<算法导论>动态规划那一章.既然看到了就想回忆下, ...
- 2016级算法第四次上机-F.AlvinZH的最“长”公共子序列
940 AlvinZH的最"长"公共子序列 思路 DP,难题. \(dp[i][j]\) :记录A的前i个字符与B的前j个字符变成相同需要的最小操作数. 初始化:dp[i][0] ...
随机推荐
- JS一些实用的方法
1.首次为变量赋值时务必使用var关键字 变量没有声明而直接赋值得话,默认会作为一个新的全局变量,要尽量避免使用全局变量. 2.使用===取代== ==和!=操作符会在需要的情况下自动转换数据类型.但 ...
- June 9. 2018, Week 23rd, Saturday
I know nothing except the fact of my ignorance. 除了自己的无知,我一无所知. Believe it or not, true wisdom exists ...
- Ubuntu 12.04上安装MySQL并运行
Ubuntu 12.04上安装MySQL并运行 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 安装MySQL数据库 sudo apt-get upda ...
- IE和其他浏览器内核
1.qq急速 2.qq的IE兼容模式 3.Edge 4.IE11 5.chrome js获取浏览器内核 <script language="JavaScript" type= ...
- (转)Spring Boot(二十):使用 spring-boot-admin 对 Spring Boot 服务进行监控
http://www.ityouknow.com/springboot/2018/02/11/spring-boot-admin.html 上一篇文章<Spring Boot(十九):使用 Sp ...
- 强化学习(三)—— 时序差分法(SARSA和Q-Learning)
1.时序差分法基本概念 虽然蒙特卡洛方法可以在不知道状态转移概率矩阵的前提下,灵活地求解强化学习问题,但是蒙特卡洛方法需要所有的采样序列都是完整的状态序列.如果我们没有完整的状态序列就无法用蒙特卡洛方 ...
- Nginx缓存服务
Nginx缓存服务 1.缓存常见类型 2.缓存配置语法 3.缓存配置实践 4.缓存清理实践 5.部分页面不缓存 6.缓存日志记录统计 通常情况下缓存是用来减少后端压力, 将压力尽可能的往前推, 减少后 ...
- 003_python学习之 字符串前'r'的用法
在打开文件的时候open(r'c:\....') 加r和不加''r是有区别的 'r'是防止字符转义的 如果路径中出现'\t'的话 不加r的话\t就会被转义 而加了'r'之后'\t'就能保留原有的样子 ...
- Kali-linux使用Metasploit基础
Metasploit是一款开源的安全漏洞检测工具.它可以帮助用户识别安全问题,验证漏洞的缓解措施,并对某些软件进行安全性评估,提供真正的安全风险情报.当用户第一次接触Metasploit渗透测试框架软 ...
- scipy 安装错误及解决
pip 安装 scipy 时,因为是编译安装,所以如果缺少一些编译库,会报很多错误,以下总结可能缺失的安装包: sudo apt-get install gfortran sudo apt-get i ...