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

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

Copy
axyb
abyxb

Sample Output 1 Copy

Copy
axb

The answer is axb or ayb; either will be accepted.


Sample Input 2 Copy

Copy
aa
xayaz

Sample Output 2 Copy

Copy
aa

Sample Input 3 Copy

Copy
a
z

Sample Output 3 Copy

Copy

The answer is  (an empty string).


Sample Input 4 Copy

Copy
abracadabra
avadakedavra

Sample Output 4 Copy

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-最长公共子序列,输出字符串)的更多相关文章

  1. 基于DP的LCS(最长公共子序列)问题

    最长公共子序列,即给出两个序列,给出最长的公共序列,例如: 序列1 understand 序列2 underground 最长公共序列undernd,长度为7 一般这类问题很适合使用动态规划,其动态规 ...

  2. POJ 1458 Common Subsequence (DP+LCS,最长公共子序列)

    题意:给定两个字符串,让你找出它们之间最长公共子序列(LCS)的长度. 析:很明显是个DP,就是LCS,一点都没变.设两个序列分别为,A1,A2,...和B1,B2..,d(i, j)表示两个字符串L ...

  3. HDU 1159 Common Subsequence:LCS(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 题意: 求最长公共子序列. 题解: (LCS模板题) 表示状态: dp[i][j] = max ...

  4. NYOJ 36 LCS(最长公共子序列)

    题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=36 最长公共子序列 时间限制:3000 ms  |  内存限制:65535 KB ...

  5. POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56150   Accepted: 19398 Desc ...

  6. 经典dp 最长公共子序列

    首先,说明一下子序列的定义…… 一个序列A={a1,a2,a3,...,an},从中删除任意若干项,剩余的序列叫A的一个子序列. 很明显(并不明显……),子序列……并不需要元素是连续的……(一开始的时 ...

  7. LCS(最长公共子序列)问题

    例题见挑战程序设计竞赛P56 解释:子序列是从原序列中按顺序(可以跳着)抽取出来的,序列是不连续的,这是其和子串最大的区别: 我们可以定义dp数组为dp[i][j],表示的是s1-si和t1-ti对应 ...

  8. hdu1159 dp(最长公共子序列)

    题意:给两个字符串,求这两个字符串的最长公共子序列的长度 因为之前集训的时候做过,所以现在即使会做也并不是什么稀奇的事,依旧为了自己的浅薄感到羞愧啊``` 解法就是通过两个字符串的每个字符互相比较,根 ...

  9. LCS(最长公共子序列)动规算法正确性证明

    今天在看代码源文件求diff的原理的时候看到了LCS算法.这个算法应该不陌生,动规的经典算法.具体算法做啥了我就不说了,不知道的可以直接看<算法导论>动态规划那一章.既然看到了就想回忆下, ...

  10. 2016级算法第四次上机-F.AlvinZH的最“长”公共子序列

    940 AlvinZH的最"长"公共子序列 思路 DP,难题. \(dp[i][j]\) :记录A的前i个字符与B的前j个字符变成相同需要的最小操作数. 初始化:dp[i][0] ...

随机推荐

  1. c/c++ 类模板初探

    类模板 1,模板类里的函数都是模板函数 2,模板类里的函数,在类外面实现的时候,要用模板函数(方法:push_back)的方式实现,在类内部实现时,不需要用模板函数(方法:show)方式实现. 3,用 ...

  2. Python 标示符和关键字

    标示符 开发人员在程序中自定义的一些符号和名称.标示符是自己定义的,如变量名 .函数名等 标示符的规则 标示符由字母.下划线和数字组成,且数字不能开头   注:python中的标识符是区分大小写的 命 ...

  3. Amazon onsite behavior question

    https://www.1point3acres.com/bbs/thread-307462-1-1.html http://kraftshala.com/how-to-raise-the-bar-i ...

  4. SSH File Transfer遇到错误"too many authentication failures for root".A protocol error was detected......

    在SSH  Secure Shell 连接Linux centos的时候,遇到F-Secure SSH File Transfer错误"too many authentication fai ...

  5. Win 10 和 Linux 双系统,从硬盘删除Linux分区,Win 10引导修复

    由于安装双系统后,Linux 用的比较少.因此,从Win 10 磁盘管理中删除了linux 占用的磁盘空间,重启后无法进入win 10 ,出现如下情况: 有人提出,此时需要重装系统,并不用如此麻烦,通 ...

  6. C语言 统计一串字符中空格键、Tab键、回车键、字母、数字及其他字符的个数(Ctrl+Z终止输入)

    //凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ #include<stdio.h> void main(){ , num=, blank=, ...

  7. spring的基于XML方式的属性注入

    1.掌握spring的属性注入的方法: 1.1构造方法注入普通值---------<constructor-arg>标签的使用 首先新建一个类 package spring.day1.de ...

  8. 【Teradata】并行操作工具

    1.psh并行shell //单机模式 psh date psh pdestate -a psh verify_pdisks //交互模式 psh psh.>help psh.>selec ...

  9. 【Linux常见问题】Centos7的网络配置问题

    在配置Centos7网络的时候,可能出出现虚拟机.本地以及外网三者之间ping不通的问题,可以从以下的几个方面排查: 1.确定需要管理员权限才能修改配置网络,如下图: 需要点下更改设置,然后出现下面的 ...

  10. n2

    "express-ws": "^3.0.0", "devDependencies": {    "socket.io": ...