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

这道题是LCS的变种,只需把数组类型改成string就可以了,重点在于对LCS模版的理解
LCS:求解最长公共子序列,由于动态规划无非是递归的一种优化,不如先来看看递归的解法
LCS(i,j)表示数组以数组1的i个为止和以数组2的第j个为止的最长公共子序列,注意是第i个字符为子串的末尾字符(这样才能达到无后效性的目的)
1.LCS(i-1,j-1)+1 (a【i】=b【j】)
LCS(i,j)={
2.max{LCS(i-1,j),LCS(i,j-1)} (a【i】!=b【j】) 对比递归,就能写出状态转移方程,可以用二维数组dp【i】【j】来表示状态LCS(i,j)
不过这个动态规划方程属于从已知推未知型,要特别注意给初始值 1.dp【i-1】【j-1】+1 (a【i】=b【j】) //如果相等,公共串的长度又加一了

               2.dp【i】【j】={  2.dp【0】【i】=0,dp【i】【0】=0           //初始值,如果某个数组的长度为0,那没必要说公共串了

3.max{dp【i-1】【j】,dp【i】【j-1】} (a【i】!=b【j】)   //如果不相等,只能选择前一个值最大的状态

然后再因题制宜,在dp寻求最长公共子串时记录一下选了哪些字符,最后输出就好
#include"iostream"
#include"cstring"
#include"cstdio"
using namespace std; const int maxn=;
string a[maxn],b[maxn],ans[maxn];
int dp[maxn][maxn],c[maxn][maxn],m,n; void Init()
{
memset(dp,,sizeof(dp));
int i=,j=;
while(cin>>a[i]&&a[i]!="#") {i++;dp[i][]=;}
m=i-;
while(cin>>b[j]&&b[j]!="#") {j++;dp[][j]=;}
n=j-;
// memset(dp,0,sizeof(dp));
} void Work()
{
memset(c,,sizeof(c));
for(int i=;i<=m;i++)
{
for(int j=;j<=n;j++)
{
if(a[i]==b[j]) {dp[i][j]=dp[i-][j-]+;c[i][j]=;}
else
{
if(dp[i-][j]>dp[i][j-])
{
dp[i][j]=dp[i-][j];
c[i][j]=;
}
else
{
dp[i][j]=dp[i][j-];
c[i][j]=;
}
}
}
}
} void Print()
{
int mn=dp[m][n];
int i=m,j=n;
while(mn)
{
while(c[i][j]!=)
{
if(c[i][j]==) i--;
else j--;
}
ans[mn]=a[i];
mn--;
i--;j--;
}
int k;
for(k=;k<dp[m][n];k++) cout<<ans[k]<<" ";
cout<<ans[k]<<endl;
} int main()
{
while(cin>>a[])
{
Init();
Work();
Print();
}
return ;
}

O(^_^)O



 

集训第五周动态规划 D题 LCS的更多相关文章

  1. 集训第五周 动态规划 B题LIS

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Des ...

  2. 集训第五周动态规划 I题 记忆化搜索

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  3. 集训第五周动态规划 H题 回文串统计

    Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A ...

  4. 集训第五周动态规划 G题 回文串

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  5. 集训第五周动态规划 F题 最大子矩阵和

    Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...

  6. 集训第五周动态规划 C题 编辑距离

    Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...

  7. 集训第五周 动态规划 K题 背包

    K - 背包 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  8. 集训第五周动态规划 J题 括号匹配

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  9. 集训第五周动态规划 E题 LIS

    Description The world financial crisis is quite a subject. Some people are more relaxed while others ...

随机推荐

  1. Qt对象模型之一:信号和槽

    一.信号和槽机制概述 信号槽是 Qt 框架引以为豪的机制之一.所谓信号槽,实际就是观察者模式.当某个事件发生之后,比如,按钮检测到自己被点击了一下,它就会发出一个信号(signal).这种发出是没有目 ...

  2. 专 linux命令之set x详解

    set -x与set +x指令   用于脚本调试.set是把它下面的命令打印到屏幕 set -x 是开启 set +x是关闭 set -o是查看 (xtrace),set去追中一段代码的显示情况. 执 ...

  3. RabbitMQ五:生产者--队列--多消费者

    一.生成者-队列-多消费者(前言) 上篇文章,我们做了一个简单的Demo,一个生产者对应一个消费者,本篇文章就介绍 生产者-队列-多个消费者,下面简单示意图 P 生产者    C 消费者  中间队列 ...

  4. echart动态加载数据

    <!DOCTYPE html> <head>     <meta charset="utf-8">     <title>EChar ...

  5. 微信小程序flex布局

    一.flex布局基础 二.相对定位和绝对定位   flex的容器和元素   主轴(左-右),交叉轴(上-下)     flex容器属性详解 flex-direction 决定元素的排列方向(默认row ...

  6. wordpress在撰写新文章界面的显示选项按钮点击无反应的解决办法

    原文链接:wordpress在撰写新文章界面的显示选项按钮点击无反应的解决办法 最近升级wordpress之后,发现在文章编辑界面的添加新媒体和可视化按钮点击无反应,如下:  然后就在网上找解决办法, ...

  7. ScrollView嵌套GridView,GridView显示不全

    最近开发有个需求是以A-Z的方式区分全国城市(搜索功能),并实现字母索引的功能(有点类似微信,不过比较坑的是用的是GridView, 并且GridView上面还有几个LinearLayout). 详细 ...

  8. A/B宣言

    作者:Dunne & Raby A B 肯定的  批判的 解决问题的 发现问题的 设计即流程 设计即方法 给出答案 问问题 为行业中服务 为社会服务 说明世界是怎样的 说明世界可能是怎样的 科 ...

  9. vue框架的知识

    基础:实例----组件----指令----选项-----计算属性----事件绑定----模板渲染-----内置动画 ---组件交互----路由. vuejs干了什么事情:数据渲染/数据同步 组件化/模 ...

  10. raw cannot be resolved or is not a field解决办法

    解决raw文件夹问题 查看左侧项目/res文件夹下是否有raw文件夹,(一定是放到res文件夹下,raw在项目开始创建时候不会自动创建,所以要自己创建)