/*
LCS
*/
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000;
int dp[maxn][maxn], c[maxn][maxn];
int str1[maxn],str2[maxn];
int k;
void dfs(int i,int j){ //打印路径
if(i == 0 || j == 0) return;
if(c[i][j] == 1){
dfs(i-1,j-1);
k--;
printf("%d%c",str1[i],k > 0 ? ' ':'\n');
}else if(c[i][j] == 2){
dfs(i-1,j);
}else{
dfs(i,j-1);
}
}
int main(){
int n,m;
while(scanf("%d",&n)!=EOF){
for(int i = 1; i <= n; i++){
scanf("%d",&str1[i]);
}
scanf("%d",&m);
for(int i = 1; i <= m; i++){
scanf("%d",&str2[i]);
}
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= m; j++){
if(str1[i] == str2[j]){
dp[i][j] = dp[i-1][j-1] + 1;
c[i][j] = 1;
}else if(dp[i-1][j] > dp[i][j-1]){
dp[i][j] = dp[i-1][j];
c[i][j] = 2;
}else{
dp[i][j] = dp[i][j-1];
c[i][j] = 3;
}
}
}
printf("%d\n",dp[n][m]);
k = dp[n][m];
dfs(n,m);
}
return 0;
} /*
7
1 2 3 2 4 1 2
6
2 4 3 1 2 1
*/

  

LCS与打印路径的更多相关文章

  1. LCS(打印路径) POJ 2250 Compromise

    题目传送门 题意:求单词的最长公共子序列,并要求打印路径 分析:LCS 将单词看成一个点,dp[i][j] = dp[i-1][j-1] + 1 (s1[i] == s2[j]), dp[i][j] ...

  2. UVA 531 - Compromise(dp + LCS打印路径)

      Compromise  In a few months the European Currency Union will become a reality. However, to join th ...

  3. 最长公共子序列Lcs(打印路径)

    给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的).   比如两个串为:   abcicba abdkscab   ab是两个串的子序列,abc也是,abca也是,其中abca是这 ...

  4. POJ 2250 Compromise【LCS】+输出路径

    题目链接:https://vjudge.net/problem/POJ-2250 题目大意:给出n组case,每组case由两部分组成,分别包含若干个单词,都以“#”当结束标志,要求输出最长子序列. ...

  5. UVA 624 (0 1背包 + 打印路径)

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #i ...

  6. UVA 10054 The Necklace(欧拉回路,打印路径)

    题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  7. zoj 3088 Easter Holidays(最长路+最短路+打印路径)

    Scandinavians often make vacation during the Easter holidays in the largest ski resort Are. Are prov ...

  8. AOE网上的关键路径(最长路径 + 打印路径)

    题目描述 一个无环的有向图称为无环图(Directed Acyclic Graph),简称DAG图.     AOE(Activity On Edge)网:顾名思义,用边表示活动的网,当然它也是DAG ...

  9. POJ 3414 Pots ( BFS , 打印路径 )

    题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...

随机推荐

  1. Unity串口通信

    一.串口简介 串行接口(串口)通常指COM接口,是采用串行通信方式的扩展接口.串口按位(bit)发送和接收字节.尽管比按字节(byte)的并行通信慢,但是串口可以在使用一根线发送数据的同时用另一根线接 ...

  2. Apache Spark

    1. 用Apache Spark进行大数据处理——第一部分:入门介绍 2.

  3. JQuery全局篇

    学到JavaScript的时候,感觉这个东西很神奇,没想到学到JQuery的时候,发现BS的世界,真的很微妙,不经意的一个方法就可以给人焕然一新的感觉,很喜欢这个阶段学的东西,但是还是感觉少于代码的训 ...

  4. 【bzoj3813】: 奇数国 数论-线段树-欧拉函数

    [bzoj3813]: 奇数国 题意:给定一个序列,每个元素可以分解为最小的60个素数的形式.(x=p1^k1*p2^k2*......p60^k60)(p1=2,p2=3,…,p60=281) 支持 ...

  5. jpa batch批量操作save和persist比较

    1.网上最常见的JPA----entityManager批量操作方法 private EntityManager em; @PersistenceContext(name = "Entity ...

  6. javascript拖拽事件

    <!DOCTYPE html> <html> <head> <title></title> <style type="tex ...

  7. [USACO08FEB]酒店Hotel 线段树 BZOJ 1593

    题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...

  8. LUNA16数据集的百度云链接

    可能需要会员链接:https://pan.baidu.com/s/1KTjoGKfLB_1Y-BQzerhGgg 提取码:g901

  9. spring 配置 junit

    package cn.hefen.mall.app; import cn.hefen.mall.app.model.ResultMap; import cn.hefen.mall.app.model. ...

  10. nginx FastCGI模块配置

    这个模块允许nginx同FastCGI协同工作,并且控制哪些参数将被安全传递. location / { fastcgi_pass localhost:9000;# 或者http://ip:9000; ...