题目链接:

http://poj.org/problem?id=2250

Compromise
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9284   Accepted: 3972   Special Judge

Description

In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is not a trivial task for the countries (maybe except for Luxembourg). To enforce that Germany will fulfill the criteria, our government has so many wonderful options (raise taxes, sell stocks, revalue the gold reserves,...) that it is really hard to choose what to do.

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

The input will contain several test cases.

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

For each test case, print the longest common subsequence of words occuring in the two texts. If there is more than one such sequence, any one is acceptable. Separate the words by one blank. After the last word, output a newline character.

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

Source

分析:
经典的LCS问题,重点是采用DFS输出此LCS序列中的一个
注意输入两个序列的方式,学习了
注意输入序列的下标从1开始
注意DFS要先递归到最后再输出序列
注意dp的初始化,直接memset就可以
代码如下:
 
代码如下;

#include<cstring>
#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
#define max_v 1005
using namespace std;
string x[max_v],y[max_v];
int dp[max_v][max_v];
int l1,l2;
int dfs(int i,int j)
{
if(i==||j==)
return ;
if(x[i]==y[j])//来自左上角
{
dfs(i-,j-);
cout<<x[i]<<" ";//先递归到最后再输出,,这样就是顺序的
}
else
{
if(dp[i-][j]>dp[i][j-])//来自上面
{
dfs(i-,j);
}
else//来自左边
{
dfs(i,j-);
}
}
return ;
}
int main()
{
string s;
while(cin>>s)
{
l1=l2=;
if(s!="#")
{
x[++l1]=s;
while(cin>>s&&s!="#")
{
x[++l1]=s;
}
}
while(cin>>s&&s!="#")
{
y[++l2]=s;
}
memset(dp,,sizeof(dp));
for(int i=; i<=l1; i++)
{
for(int j=; j<=l2; j++)
{
if(x[i]==y[j])
{
dp[i][j]=dp[i-][j-]+;
}
else
{
dp[i][j]=max(dp[i-][j],dp[i][j-]);
}
}
}
dfs(l1,l2);
cout<<endl;
}
return ;
}

最新文章

  1. Nuget包之间的依赖
  2. python 获取脚本所在目录
  3. C++ Pitfalls 之 reference to an object in a dynamically allocated containter
  4. IOS之未解问题--关于IOS图像渲染CPU和GPU
  5. Helpers\Pagination
  6. &lt;转&gt;MySQL性能优化的最佳20+条经验
  7. [Mugeda HTML5技术教程之12]制作跨屏互动应用
  8. Spark调研笔记第2篇 - 怎样通过Sparkclient向Spark提交任务
  9. (中级篇 NettyNIO编解码开发)第八章-Google Protobuf 编解码-1
  10. 如何给框架添加API接口日志
  11. nginx设置默认server
  12. Robot Framework 自动化测试 Selenium2Library 库 用法
  13. 基于iview 封装一个vue 表格分页组件
  14. Codeforces Gym100543G Virus synthesis 字符串 回文自动机 动态规划
  15. JavaScript基础视频教程总结(041-050章)
  16. [php] yii debug设置
  17. 微软BI 之SSIS 系列 - 在 SSIS 输出平面文件时根据运行日期生成不同的文件名称
  18. elk问题,求教各位大虾!
  19. Ren&#39;Py视觉小说安装,玩一下吧,上班很闲的话
  20. Mac-控制台更新svn版本

热门文章

  1. exception processing, template error resolving template
  2. react组件直接在document上添加事件
  3. P2P文件上传
  4. mac os idea的快捷键
  5. git push 提示 Everything up-to-date
  6. 用华为eNSP模拟器配置Hybrid、Trunk和Access三种链路类型端口
  7. swift版的元组
  8. Linux 系统的网络基础_【all】
  9. 铁乐学Python_Day33_网络编程Socket模块1
  10. linux之安装nali本地解析IP归属