最长公共子序列(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的最长公共子序列(子序列不要求是连续的). ...
随机推荐
- 015:URLs分层模块化
URLs分层模块化: 经过上面的14节课程,大伙有没有发现一个问题:那就是随着的项目功能模块越来越多,所有url匹配都写在一个urls.py文件中,其结果是:文件长,看着心累——需要分门别类:因此能不 ...
- jquery animated选择器 语法
jquery animated选择器 语法 作用::animated 选择器选取当前的所有动画元素.直线电机参数 语法:$(":animated") jquery animated ...
- java把含小数点的数字字符串转换为int类型
String num ="1.00"; int abc =Double.valueOf(num).intValue();//转换为Int类型
- vue相关基础知识
参考文章: Vue 项目里戳中你痛点的问题及解决办法(更新) vue中前进刷新.后退缓存用户浏览数据和浏览位置的实践
- js基本数据类型+判断数据类型方法
摘要:不管是什么类型的,Object.prototype.toString.call();都可以判断出其具体的类型,简单基本类型(String.Number.Boolean.Null.Undefine ...
- Maven构建生命周期
以下引用官方的生命周期解释https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html: 一.构建生命 ...
- pymysql(一)检索、增加、更新、删除数据
(一) SELECT 检索数据 代码如下: import pymysql '''pymysql使用指南host = '127.0.0.1'回送地址,指本地机port = 3306MySQL的默认端口 ...
- Linux小记 -- apt-get install build-essential和yum groupinstall "Development Tools"
Ubuntu的apt install build-essential 作用:配置Debian系统编译环境,就是下载安装支持编译Debian包的依赖/包,比如gcc等. 直接执行 #apt instal ...
- linux上的常用的系统自带命令
wikipedia 发现的分类,发现还有好多没用过.. Unix command-line interface programs and shell builtins File system ca ...
- IO负载高来源定位pt-ioprofile
1.使用top -d 1 查看%wa是否有等待IO完成的cpu时间,简单理解就是指cpu等待磁盘写入完成的时间:IO等待所占用的cpu时间的百分比,高过30%时IO压力高: 2.使用iostat -d ...