Compromise - POJ 2250 UVa 531 dp
来源:互联网 发布:工商银行查余额软件 编辑:程序博客网 时间:2024/05/23 07:24
Compromise
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6398 Accepted: 2868 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.
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.
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
题意:输出最长的公共子序列。
思路:递推的同时记录路径。
AC代码如下:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;char s1[110][40],s2[110][40];int dp[110][110],p[110][110][3],len1,len2,a,b,ans[110];int main(){ int n,i,j,k; while(~scanf("%s",s1[1])) { len1=1;len2=1; memset(dp,0,sizeof(dp)); while(s1[len1][0]!='#') scanf("%s",s1[++len1]); len1--; scanf("%s",s2[1]); while(s2[len2][0]!='#') scanf("%s",s2[++len2]); len2--; for(i=1;i<=len1;i++) for(j=1;j<=len2;j++) { if(dp[i-1][j]>=dp[i][j-1]) { dp[i][j]=dp[i-1][j]; p[i][j][0]=0; p[i][j][1]=i-1; p[i][j][2]=j; } else { dp[i][j]=dp[i][j-1]; p[i][j][0]=0; p[i][j][1]=i; p[i][j][2]=j-1; } if(strcmp(s1[i],s2[j])==0) { if(dp[i-1][j-1]+1>dp[i][j]) { dp[i][j]=dp[i-1][j-1]+1; p[i][j][0]=1; p[i][j][1]=i-1; p[i][j][2]=j-1; } } } i=len1;j=len2; while(i*j) { if(p[i][j][0]==1) ans[dp[i][j]]=i; a=i;b=j; i=p[a][b][1]; j=p[a][b][2]; } if(dp[len1][len2]>0) printf("%s",s1[ans[1]]); for(i=2;i<=dp[len1][len2];i++) printf(" %s",s1[ans[i]]); printf("\n"); }}
0 0
- Compromise - POJ 2250 UVa 531 dp
- uva--531Compromise+dp
- POJ 2250 Compromise (UVA 531)
- UVa 531 Compromise (DP&LCS)
- UVA 531 - Compromise(dp + LCS打印路径)
- poj 2250 Compromise(DP)
- poj 2250 Compromise dp lcs 路径输出
- poj 2250 Compromise(裸LCS DP)
- UVA 531 Compromise 最长公共子序列(DP)
- UVa 531 Compromise (DP 最长公共子序列)
- [dp] poj 1015 Jury Compromise
- poj 1015 Jury Compromise dp
- POJ 1015 Jury Compromise(DP)
- POJ 1015 Jury Compromise ---- DP
- DP-POJ-1015-Jury Compromise
- POJ 1015 Jury Compromise【DP】
- uva 531 Compromise
- UVA 531 Compromise
- 计算机科学框架
- Java类集-多对多关系范例
- 《JSP+DreamweaverCS4+CSS+Ajax动态网站开发典型案例》
- 考研路上的那些一战二战三战成功与失败的故事系列之十四
- UVA 11136 - Hoax or what(set模拟)
- Compromise - POJ 2250 UVa 531 dp
- Linux认证笔试 基本题(答案见最后)
- C++ unordered Associative Containers(C++11)
- Google产品经历笔试题
- .NET应用架构设计—服务端多线程使用小结(多线程使用常识)
- 考研路上的那些一战二战三战成功与失败的故事系列之十五
- Linux笔试题与答案
- Java读写pdf文档和office文档的示例
- 歌手成绩2