POJ2250:Compromise(LCS)
Description
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
题意:给出两段文字,求出最长的公共单词串
思路:LCS问题,只需要开个二维来记录就好了
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; char a[35][105],b[35][105],c[35][105];
int dp[105][105],mark[105][105],len1,len2,cnt; void LCS()
{
int i,j;
memset(dp,0,sizeof(dp));
memset(mark,0,sizeof(mark));
for(i = 0;i<=len1;i++)
mark[i][0] = 1;
for(i = 0;i<=len2;i++)
mark[0][i] = -1;
for(i = 1; i<=len1; i++)
{
for(j = 1; j<=len2; j++)
{
if(!strcmp(a[i-1],b[j-1]))
{
dp[i][j] = dp[i-1][j-1]+1;
mark[i][j] = 0;
}
else if(dp[i-1][j]>=dp[i][j-1])
{
dp[i][j] = dp[i-1][j];
mark[i][j] = 1;
}
else
{
dp[i][j] = dp[i][j-1];
mark[i][j] = -1;
}
}
}
} void PrintLCS(int i,int j)
{
if(!i&&!j)
return ;
if(mark[i][j]==0)
{
PrintLCS(i-1,j-1);
strcpy(c[cnt++],a[i-1]);
}
else if(mark[i][j]==1)
{
PrintLCS(i-1,j);
}
else
{
PrintLCS(i,j-1);
}
} int main()
{
int i;
while(~scanf("%s",a[0]))
{
len1 = 1;
while(strcmp(a[len1-1],"#"))
scanf("%s",a[len1++]);
len1-=1;
scanf("%s",b[0]);
len2 = 1;
while(strcmp(b[len2-1],"#"))
scanf("%s",b[len2++]);
LCS();
cnt = 0;
PrintLCS(len1,len2);
printf("%s",c[0]);
for(i = 1; i<cnt; i++)
{
printf(" %s",c[i]);
}
printf("\n");
} return 0;
}
POJ2250:Compromise(LCS)的更多相关文章
- POJ 2250 Compromise(LCS)
POJ 2250 Compromise(LCS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#proble ...
- POJ2250 - Compromise(LCS+打印路径)
题目大意 给定两段文本,问公共单词有多少个 题解 裸LCS... 代码: #include<iostream> #include<string> using namespace ...
- 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring
LCS - Longest Common Substring no tags A string is finite sequence of characters over a non-empty f ...
- 我的第一篇博客----LCS学习笔记
LCS引论 在这篇博文中,博主要给大家讲一个算法----最长公共子序列(LCS)算法.我最初接触这个算法是在高中学信息学竞赛的时候.那时候花了好长时间理解这个算法.老师经常说,这种算法是母算法,即从这 ...
- 51nod 1006 最长公共子序列Lcs(经典动态规划)
传送门 Description 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdkscab ab是两个串的子序列,abc也是 ...
- 整理的一些模版LCS(连续和非连续)
对于连续的最大串,我们称之为子串....非连续的称之为公共序列.. 代码: 非连续连续 int LCS(char a[],char b[],char sav[]){ int lena=strlen(a ...
- 算法导论-动态规划(最长公共子序列问题LCS)-C++实现
首先定义一个给定序列的子序列,就是将给定序列中零个或多个元素去掉之后得到的结果,其形式化定义如下:给定一个序列X = <x1,x2 ,..., xm>,另一个序列Z =<z1,z2 ...
- 最长公共子序列(LCS问题)
先简单介绍下什么是最长公共子序列问题,其实问题很直白,假设两个序列X,Y,X的值是ACBDDCB,Y的值是BBDC,那么XY的最长公共子序列就是BDC.这里解决的问题就是需要一种算法可以快速的计算出这 ...
- 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列
出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...
随机推荐
- Fedora上配置一个安全FTP
现在流行的FTP服务器,比较著名的有WU-FTP(Washington University FTP)和VSFTP(Very Secure FTP 非常安全的FTP)以及Proftp,pureftp等 ...
- javascript 设为首页 | 加入收藏夹 JS代码
我们介绍一个可兼容所有浏览器的加入收藏代码代码,大概原理是这样的我们根据获取用户navigator.userAgent.toLowerCase()信息来判断浏览器,根据浏览器是否支持加入收藏js命令, ...
- django-orm-standalone
django-orm-standalone script via:https://github.com/masnun/django-orm-standalone/ # Django specific ...
- rpmbuild构建包时的宏定义的赋值
rpmbuild -bb SPECS/git.spec --define="_topdir `pwd`" rpmbuild --rebuild SRPMS/git.src.rp ...
- Gunicorn快速入门
Gunicorn (独角兽)是一个高效的Python WSGI Server,通常用它来运行 wsgi application(由我们自己编写遵循WSGI application的编写规范) 或者 w ...
- Unity的Profiler性能分析
1. CPU Usage A. WaitForTargetFPS: Vsync(垂直同步)功能所,即显示当前帧的CPU等待时间 B. Overhead: Profiler总体时间-所有单项的记录时间总 ...
- WEB工程数据库相关安装脚本写作
1. 数据库oracle安装 2. 数据库用户创建,表空间创建,表创建 #!/bin/bash current_path=`pwd` create_tablespace=${current_path} ...
- BZOJ 3106 棋盘游戏
Description 一个\(n \times n(n \le 2)\)棋盘上有黑白棋子各一枚.游戏者A和B轮流移动棋子,A先走. A的移动规则:只能移动白棋子.可以往上下左右四个方向之一移动一格. ...
- 非主窗体在任务栏显示按钮(简单好用)good
非主窗体在任务栏显示按钮 type TForm2 = class(TForm) private { Private declarations } public { Public declaration ...
- 【HDOJ】2268 How To Use The Car
数学题.设步行速度a,车速b,距离c.Teddy步行时间为T1,WhereIsHeroFrom步行时间T2,总时间T.若b>a:aT1 + b(T-T1) = c (1)aT2 + b(T-T2 ...