最长公共子序列(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的最长公共子序列(子序列不要求是连续的). ...
随机推荐
- word和画图
文档和画图收费文档:edu.51cto.com/course/course_id-4992.htmledu.51cto.com/course/course_id-4991.html
- Mysql基本原理和概念
一.引言 随着互联网应用的广泛普及,海量数据的存储和访问成为了系统设计的瓶颈问题.对于一个大型的互联网应用,每天几十亿的PV无疑对数据库造成了相当高的负载.对于系统的稳定性和扩展性造成了极大的问题.通 ...
- Python抽象类(abc模块)
1.抽象类概念 抽象类是一个特殊的类,只能被继承,不能实例化 2.为什么要有抽象类 其实在未接触抽象类概念时,我们可以构造香蕉.苹果.梨之类的类,然后让它们继承水果这个基类,水果的基类包含一个eat函 ...
- 186. [USACO Oct08] 牧场旅行 (第三次考试大整理)
186. [USACO Oct08] 牧场旅行 输入文件:pwalk.in 输出文件:pwalk.out 简单对比 时间限制:1 s 内存限制:128 MB n个被自然地编号为1..n奶牛 ...
- 洛谷P2482 [SDOI2010]猪国杀——题解
猪国杀,模拟题的一颗耀眼的明珠,成长大牛.锻炼码力必写题! 模拟题没什么思维难度.只要按部就班地去做就是.模拟简单在这,难也在这.因为题面巨长,条件巨多,忽疏一点都有可能全盘皆输.故推荐考试时碰见了, ...
- Jmeter性能测试一
用jmeter进行压力测试,在网上看到一个简单的例子.按步骤做,在jmeter中执行时,结果中error一直为100%.通过在代码中加入打印语句,才找出代码中的一处错误.下面po上的代码中已将错误修改 ...
- B. Array K-Coloring
B. Array K-Coloring 给定一个数组 要求对元素进行染色,满足相同元素颜色一定不同,且所有颜色要全部用完 #include<bits/stdc++.h> using nam ...
- 用flask进行web开发
经理管理一个餐厅,推出每天都有特色菜的营销模式.他想根据一周中的每一天有一种特色菜. 客人想知道当天的特色菜是什么.另外再添加一个介绍页面.bios路径下,显示餐厅主人,厨师,服务生的简介. pyth ...
- redis 安装 主从同步 哨兵模式
一.redis 的安装1.先将安装包放到linux的一个文件夹下面 2.解压压缩包如图所示 3.解压后进入解压文件 4.安装: make 出现it.s a good idea to run 'make ...
- selenium报错汇总
selenium报错汇总 报错:[error] Could not connect to Selenium Server. Have you started the Selenium Server y ...