最长公共子序列(LCS) Medium1
Therefore the German government requires a program for the following task:
Two politicians each enter their proposal of what to do. The computer then outputs the longest common subsequence of words that occurs in both proposals. As you can see, this is a totally fair compromise (after all, a common sequence of words is something what both people have in mind).
Your country needs this program, so your job is to write it for us.
Input
Each test case consists of two texts. Each text is given as a sequence of lower-case words, separated by whitespace, but with no punctuation. Words will be less than 30 characters long. Both texts will contain less than 100 words and will be terminated by a line containing a single '#'.
Input is terminated by end of file.
Output
Sample Input
die einkommen der landwirte
sind fuer die abgeordneten ein buch mit sieben siegeln
um dem abzuhelfen
muessen dringend alle subventionsgesetze verbessert werden
#
die steuern auf vermoegen und einkommen
sollten nach meinung der abgeordneten
nachdruecklich erhoben werden
dazu muessen die kontrollbefugnisse der finanzbehoerden
dringend verbessert werden
#
Sample Output
die einkommen der abgeordneten muessen dringend verbessert werden
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<map> using namespace std;
const int N = + ;
int dp[N][N], Mark[N][N];
char ch[];
string a[N], b[N];
void Work(int len_1, int len_2){
memset(Mark, , sizeof(Mark));
memset(dp, , sizeof(dp));
for(int i = ; i <= len_1; i++)
for(int j = ; j <= len_2; j++)
if(a[i] == b[j]){ dp[i][j] = dp[i-][j-] + ; Mark[i][j] = ;}
else if(dp[i-][j] >= dp[i][j-]) {dp[i][j] = dp[i-][j]; Mark[i][j] = ;}
else dp[i][j] = dp[i][j-],Mark[i][j] = ;
}
bool flag;
void Solve(int i, int j){
if(i == || j == ) return;
if(Mark[i][j] == ){
Solve(i-, j-);
if(flag) printf(" ");
flag = true;
printf("%s",a[i].c_str());
}else if(Mark[i][j] == ){
Solve(i-, j);
}else if(Mark[i][j] == )
Solve(i, j - );
}
int main(){
while(cin >> ch){
int len_1 = ,len_2 = ;
flag = false;
a[len_1++] = ch;
while(cin >> ch &&ch[] != '#'){
a[len_1++] = ch;
}
while(cin >> ch && ch[] != '#'){
b[len_2++] = ch;
}
Work(len_1, len_2);
Solve(len_1, len_2);
printf("\n");
}
}
最长公共子序列(LCS) Medium1的更多相关文章
- 1006 最长公共子序列Lcs
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdks ...
- 动态规划之最长公共子序列LCS(Longest Common Subsequence)
一.问题描述 由于最长公共子序列LCS是一个比较经典的问题,主要是采用动态规划(DP)算法去实现,理论方面的讲述也非常详尽,本文重点是程序的实现部分,所以理论方面的解释主要看这篇博客:http://b ...
- 编程算法 - 最长公共子序列(LCS) 代码(C)
最长公共子序列(LCS) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 给定两个字符串s,t, 求出这两个字符串最长的公共子序列的长度. 字符 ...
- C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解
版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...
- POJ 1458 Common Subsequence(最长公共子序列LCS)
POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...
- 51Nod 1006:最长公共子序列Lcs(打印LCS)
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...
- 51nod 1006 最长公共子序列Lcs 【LCS/打印path】
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...
- 每日一题-——最长公共子序列(LCS)与最长公共子串
最长公共子序列(LCS) 思路: 代码: def LCS(string1,string2): len1 = len(string1) len2 = len(string2) res = [[0 for ...
- 51nod 1006:最长公共子序列Lcs
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...
随机推荐
- 3. ClustrixDB 操作
测试数据库 一. 测试分片,存储信息 测试前: clxm@p2cn1uclx101m_10.248.100.241 /data]$ clx statCluster Name: cle69e350c2c ...
- linux-history-ps1-1
1.串行端口终端(/dev/ttySn) 串行端口终端(Serial Port Terminal)是使用计算机串行端口连接的终端设备.计算机把每个串行端口都看作是一个字符设备.有段时间这些串行端口设备 ...
- Mysql5.7.26解压版(免安装版)简单快速配置步骤,5分钟搞定(win10-64位系统)
第一次安装mysql环境的时候,总会遇到各种各样的坑,在尝试了安装版和解压版的数据库之后,感觉mysql的解压版更加的简单方便,省去好多时间做专业的事情 我这里选择的是5.7.26版本,解压版下载地址 ...
- IdentityServer4
序言 IdentityServer4能做什么 Identity Server 4(以下简称ID4)是一个基于oauth2和OpenID的身份认证组件,基于这个组件可以快速开发自己的身份认证网站,支持单 ...
- DevExpress.XtraGrid.Views.Grid.GridView
private void SetView() { GridView gridView = (GridView)this.DefaultView; if (gridView != null) { gri ...
- ZOJ 1610 Count the Colors (线段树成段更新)
题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...
- Halcon链接库
halcon.libhalconc.libhalconcpp.libhalconcppxl.libhalconcxl.libhalconx.libhalconxl.libhalconxxl.libhd ...
- java jdk原生的http请求工具类
package com.base; import java.io.IOException; import java.io.InputStream; import java.io.InputStream ...
- [CSP-S模拟测试]:666(模拟)
题目描述 不忘初心. 小$\pi$假期在家无聊,打开了某弹幕直播网站. 突然,有一个精彩的镜头. 小$\pi$看到了满屏的$6$,其中,有$666$.也有$666666$.也有$6666666666. ...
- vue 表格组件分享
分享一款自己写的table组件 用起来还算简单好用 (先介绍使用方法(ts版本的)) 引入组件不多说 import jTable from '../comp/comp/table/table.v ...