POJ 2250 Compromise (线性dp LCS +递归路径)

Compromise

Time Limit: 1000MSMemory Limit: 65536K

Total Submissions: 6735Accepted: 3009Special 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 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

Source

题目链接:?id=2250题目大意:求两个段落中单词的最大公共子序列并输出它们题目分析:裸的LCS题,因为是单词,用string就行了,二维字符数组也可以,然后用一个数组记录一下路径关于LCS即最长公共子序列的状态状态转移方程解释一下:dp[i][j]表示第一个表示A的前i位和B的前j位的最长公共子序列的长度,那么有三种情况1.若A[i – 1] == B[j – 1],,dp[i][j] = dp[i – 1][j – 1] + 1 即如果A[i – 1] == B[j – 1],则这一组相同的情况记录下即+1,接着我们只要继续比较A的前i-1和B的前j-1部分2.若dp[i – 1][j] > dp[i][j – 1],dp[i][j] = dp[i – 1][j]即A的前i-1位和B的前j位的LCS大于A的前i位和B的前j-1位,则我们取大的3.若dp[i – 1][j] < dp[i][j – 1],dp[i][j] = dp[i][j – 1]本题需要记录路径,我们用path数组,若为情况1则path[i][j] = 0, 若为情况2则path[i][j] = 1, 否则path[i][j] = -1,然后递归记录答案#include <iostream>#include <cstring>#include <string>#include <algorithm>using namespace std;int dp[105][105];int path[105][105];string ans[105];string a[105], b[105];int cnta, cntb, cnt, num;void init(){cnt = cnta = cntb = num = 0;memset(dp, 0, sizeof(dp));memset(path, 0, sizeof(path));}void Get(int i, int j){if(!i || !j)return;if(path[i][j] == 0){Get(i – 1, j – 1);ans[num ++] = a[i – 1];}else if(path[i][j] == 1)Get(i – 1, j);elseGet(i, j – 1);}int main(){string s;init();while(cin >> s){if(s[0] == '#')cnt ++;if(cnt == 0 && s[0] != '#')a[cnta ++] = s;if(cnt == 1 && s[0] != '#')b[cntb ++] = s;if(cnt == 2){for(int i = 1; i <= cnta; i++){for(int j = 1; j <= cntb; j++){if(a[i – 1] == b[j – 1]){dp[i][j] = dp[i – 1][j – 1] + 1;path[i][j] = 0;}else if(dp[i – 1][j] > dp[i][j – 1]){dp[i][j] = dp[i – 1][j];path[i][j] = 1;}else{dp[i][j] = dp[i][j – 1];path[i][j] = -1;}}}Get(cnta, cntb);for(int i = 0; i < num – 1; i++)cout << ans[i] << " ";cout << ans[num – 1] << endl;init();}}}

每一件事与每一个美丽景色,都有可能成为一生中的难忘。

POJ 2250 Compromise (线性dp LCS +递归路径)

相关文章:

你感兴趣的文章:

标签云: