poj2250 动态规划+路径标记

Posted Energy_girl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj2250 动态规划+路径标记相关的知识,希望对你有一定的参考价值。

Compromise
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8209   Accepted: 3546   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


================模板===============
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <queue>
 7 #include <set>
 8 #include <map>
 9 #include <string>
10 #include <cmath>
11 #include <stdlib.h>
12 #define MAXSIZE 1005
13 using namespace std;
14 
15 int tag[105][105],dp[105][105];
16 char a[35][105],b[35][105],c[35][105];
17 int cnt,l1,l2;
18 
19 void LCS()
20 {
21     int i,j;
22     memset(dp,0,sizeof(dp));
23     memset(tag,0,sizeof(tag));
24 
25 //满满的套路~~~
26     for(i = 0;i<=l1;i++)
27         tag[i][0] = 1;
28     for(i = 0;i<=l2;i++)
29         tag[0][i] = -1;
30     for(i = 1;i<=l1;i++)
31     {
32         for(j = 1;j<=l2;j++)
33         {
34             if(!strcmp(a[i-1],b[j-1]))
35             {
36                 dp[i][j] = dp[i-1][j-1]+1;
37                 tag[i][j] = 0;
38             }
39             else if(dp[i][j-1]>dp[i-1][j])
40             {
41                 dp[i][j] = dp[i][j-1];
42                 tag[i][j] = -1;
43             }
44             else
45             {
46                 dp[i][j] = dp[i-1][j];
47                 tag[i][j] = 1;
48             }
49         }
50     }
51 }
52 void print(int i,int j)
53 {
54     if(!i && !j)
55         return;
56     if(tag[i][j] == 0)
57     {
58         print(i-1,j-1);
59         strcpy(c[cnt++],a[i-1]);
60     }
61     else if(tag[i][j] == 1)
62         print(i-1,j);
63     else print(i,j-1);
64 }
65 int main()
66 {
67     freopen("caicai.txt","r",stdin);
68     while(~scanf("%s",a[0]))
69     {
70         l1 = 1;
71         while(strcmp(a[l1-1],"#"))
72             scanf("%s",a[l1++]);
73         l1 -= 1;
74         l2 = 1;
75         scanf("%s",b[0]);
76         while(strcmp(b[l2-1],"#"))
77             scanf("%s",b[l2++]);
78         l2 -= 1;
79         LCS();
80         cnt = 0;
81         print(l1,l2);
82         cout<<c[0];
83         for(int i = 1;i<cnt;i++)
84             printf(" %s",c[i]);
85         cout<<endl;
86     }
87     return 0;
88 }
View Code

 

以上是关于poj2250 动态规划+路径标记的主要内容,如果未能解决你的问题,请参考以下文章

poj 动态规划专题练习

8.14动态规划例题:数字三角形

POJ1573(枚举,动态规划)

poj 1742 Coins (动态规划,背包问题)

算法动态规划 ⑤ ( LeetCode 63.不同路径 II | 问题分析 | 动态规划算法设计 | 代码示例 )

算法动态规划 ⑤ ( LeetCode 63.不同路径 II | 问题分析 | 动态规划算法设计 | 代码示例 )