博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
集训第五周动态规划 D题 LCS
阅读量:6424 次
发布时间:2019-06-23

本文共 3775 字,大约阅读时间需要 12 分钟。

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 landwirtesind fuer die abgeordneten ein buch mit sieben siegelnum dem abzuhelfenmuessen dringend alle subventionsgesetze verbessert werden#die steuern auf vermoegen und einkommensollten nach meinung der abgeordnetennachdruecklich erhoben werdendazu muessen die kontrollbefugnisse der finanzbehoerdendringend 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=110;string a[maxn],b[maxn],ans[maxn];int dp[maxn][maxn],c[maxn][maxn],m,n;void Init(){    memset(dp,0,sizeof(dp));    int i=2,j=1;    while(cin>>a[i]&&a[i]!="#") {i++;dp[i][0]=0;}    m=i-1;    while(cin>>b[j]&&b[j]!="#") {j++;dp[0][j]=0;}    n=j-1;   // memset(dp,0,sizeof(dp));}void Work(){    memset(c,0,sizeof(c));    for(int i=1;i<=m;i++)    {        for(int j=1;j<=n;j++)        {            if(a[i]==b[j]) {dp[i][j]=dp[i-1][j-1]+1;c[i][j]=3;}            else            {                if(dp[i-1][j]>dp[i][j-1])                {                    dp[i][j]=dp[i-1][j];                    c[i][j]=1;                }                else                {                    dp[i][j]=dp[i][j-1];                    c[i][j]=2;                }            }        }    }}void Print(){    int mn=dp[m][n];    int i=m,j=n;    while(mn)    {        while(c[i][j]!=3)        {            if(c[i][j]==1) i--;            else j--;        }        ans[mn]=a[i];        mn--;        i--;j--;    }    int k;    for(k=1;k
>a[1]) { Init(); Work(); Print(); } return 0;}
O(^_^)O
 

 

 
 

转载于:https://www.cnblogs.com/zsyacm666666/p/4724873.html

你可能感兴趣的文章
讲一讲什么叫阻塞非阻塞同步异步
查看>>
选择器补遗
查看>>
C# 实体集合和实体转换成相应的string、XDocument、XElement、XDocument
查看>>
轻松记住大端小端的含义(附对大端和小端的解释)
查看>>
dreamweaver中的 map怎么调用?_制作热点图像区域
查看>>
代码19
查看>>
Win10系列:UWP界面布局进阶5
查看>>
ABP Zero 本地化语言的初始化和扩展
查看>>
转Hibernate 一对多关联的CRUD__@ManyToOne(cascade=(CascadeType.ALL))
查看>>
FCT需求分析
查看>>
开门人和关门人(杭电1234)
查看>>
万能adapter
查看>>
开发指南专题六:JEECG微云高速开发平台代码生成
查看>>
cocos2d-x 游戏优化方案
查看>>
1.3 Quick Start中 Step 6: Setting up a multi-broker cluster官网剖析(博主推荐)
查看>>
remote desktop connection manager
查看>>
开源库RxJava、ButterKnife
查看>>
JDK内置工具jstack(Java Stack Trace)(转)
查看>>
百度之星 / 初赛第二场 B题
查看>>
Http压测工具wrk使用指南
查看>>