UVA 531 - Compromise(dp + LCS打印路径)
2013-09-01 19:58
387 查看
Compromise |
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 Specification
The input file 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 Specification
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问题。要打印路径。可以用一个VIS数组在过程中记录下状态转移方式。最后在根据这去遍历输出。
代码:
#include <stdio.h> #include <string.h> #include <ctype.h> char a[105][35], b[105][35], c, cc, flag; int dp[105][105], vis[105][105], aa, bb, aaa , bbb, i, j; void print(int i, int j) { if (!i || !j) return; if (vis[i][j] == 1) { print(i - 1, j - 1); if (flag) printf(" "); else flag = 1; printf("%s", a[i - 1]); } else if(vis[i][j] == 0) print(i - 1, j); else print(i, j - 1); } int main() { flag = aa = bb = aaa = bbb = 0; memset(dp, 0, sizeof(dp)); memset(vis, 0, sizeof(vis)); memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); while (~scanf("%s", a[aa])) { if (a[aa][0] == '#') { while (~scanf("%s", b[bb]) && b[bb][0] != '#') bb ++; for (i = 1; i <= aa; i ++) for (j = 1; j <= bb; j ++) { if (strcmp(a[i - 1], b[j - 1]) == 0) { dp[i][j] = dp[i - 1][j - 1] + 1; vis[i][j] = 1; } else { if (dp[i - 1][j] > dp[i][j - 1]) { dp[i][j] = dp[i - 1][j]; vis[i][j] = 0; } else { dp[i][j] = dp[i][j - 1]; vis[i][j] = -1; } } } print(aa, bb); printf("\n"); flag = aa = bb = aaa = bbb = 0; memset(dp, 0, sizeof(dp)); memset(vis, 0, sizeof(vis)); memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); continue; } aa ++; } return 0; }
相关文章推荐
- UVA 531 - Compromise(dp + LCS打印路径)
- uva 531 - Compromise(LCS+打印路径)
- uva531- Compromise(lcs+打印路径)
- UVA 531 DP LCS 打印路径
- UVA 531 Compromise(LCS算法+打印路径)
- LCS(打印路径) POJ 2250 Compromise
- POJ2250 - Compromise(LCS+打印路径)
- uva531 - Compromise(动规,lcs)
- uva 11404 LCS打印字典序最小路径
- UVA - 116 - Unidirectional TSP (简单DP + 打印路径)
- Pku oj 2250 Compromise(LCS+打印路径)
- UVA - 531 - Compromise (LCS)
- UVa 531 Compromise (DP&LCS)
- [LCS]Uva531 Compromise
- uva 531 Compromise(LCS)
- Light oj 1110 LCS带打印路径
- UVALive 4261——Trip Planning——————【dp+打印路径】
- Compromise(最长公共子串变形,打印路径)
- uva 624 CD (0-1背包打印路径)
- LCS(打印全路径) POJ 2264 Advanced Fruits