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. 与之类似但不同的 ...
随机推荐
- MVC Unit Testing学习笔记
MVC Unit Testing 参考文档: 1.http://www.asp.net/mvc/overview/testing 2.http://www.asp.net/mvc/tutorials/ ...
- PAT - 基础 - 最大公约数和最小公倍数
题目: 本题要求两个给定正整数的最大公约数和最小公倍数. 输入格式: 输入在一行中给出2个正整数M和N(<=1000). 输出格式: 在一行中顺序输出M和N的最大公约数和最小公倍数,两数字间以1 ...
- 学习opencv 第六章 习题十三
用傅里叶变换加速卷积,直接上代码,Mat版是Copy他人的. CvMat版 #include "stdafx.h" #include "cv.h" #inclu ...
- Thinking In Java 学习笔记 1-5 章
第1章 对象导论 本章主要讲OOP的思想及一些OOP基本概念 1.抽象过程:万物都是对象,对象具有状态.行为和标识.对象拥有属性和方法,以及在内存中的唯一地址. 2.每个对象都有一个接口:通过接口给对 ...
- BZOJ 1025 游戏
Description windy学会了一种游戏.对于1到N这N个数字,都有唯一且不同的1到N的数字与之对应.最开始windy把数字按顺序1,2,3,……,N写一排在纸上.然后再在这一排下面写上它们对 ...
- Unity NGUI 网络斗地主 -发牌 脚本交互
Unity NGUI 网络斗地主 -发牌 脚本交互 @By 灰太龙 Unity4.2.1f4 NGUI 3.0.4 本篇说的问题是脚本与控件的交互! 现在对界面进行了改进,先看副图! 1.制作发牌效果 ...
- Zabbix的集中式监控
相对于传统的ZABBIX硬件系统级监控(CPU,内存,硬盘,网卡),应用级的监控就显得有些复杂了. 如果对不同的应该来不同的应用,配置会很多的. 如果我们能在一个指定的AGENT上监控所有的APACH ...
- Annikken Andee–Arduino与Android间的简易连接
一个Arduino的兼容板,允许你显示并控制来自Android设备的Arduino应用.无需Anroid APP开发. 点击:观看视频 什么是Annikken Andee? Annikken Ande ...
- 用c写99乘法表
int main(int argc,char **argv){ int a; for(a=1;a<=9;a++){ int b; for(b=1;b<=a;b++){ printf(&qu ...
- C# Hashtable中存入数组、List
哈希表中存入数组示例代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; ...