POJ 2250 (LCS,经典输出LCS序列 dfs)
生活随笔
收集整理的這篇文章主要介紹了
POJ 2250 (LCS,经典输出LCS序列 dfs)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:
http://poj.org/problem?id=2250
Compromise| Time Limit: 1000MS | ? | Memory Limit: 65536K | ||
| Total Submissions: 9284 | ? | Accepted: 3972 | ? | 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 werdenSource
Ulm Local 1997 分析: 經典的LCS問題,重點是采用DFS輸出此LCS序列中的一個 注意輸入兩個序列的方式,學習了 注意輸入序列的下標從1開始 注意DFS要先遞歸到最后再輸出序列 注意dp的初始化,直接memset就可以 代碼如下: 代碼如下; #include<cstring> #include<cstdio> #include<string> #include<iostream> #include<algorithm> #define max_v 1005 using namespace std; string x[max_v],y[max_v]; int dp[max_v][max_v]; int l1,l2; int dfs(int i,int j) {if(i==0||j==0)return 0 ;if(x[i]==y[j])//來自左上角 {dfs(i-1,j-1);cout<<x[i]<<" ";//先遞歸到最后再輸出,,這樣就是順序的 }else{if(dp[i-1][j]>dp[i][j-1])//來自上面 {dfs(i-1,j);}else//來自左邊 {dfs(i,j-1);}}return 0; } int main() {string s;while(cin>>s){l1=l2=0;if(s!="#"){x[++l1]=s;while(cin>>s&&s!="#"){x[++l1]=s;}}while(cin>>s&&s!="#"){y[++l2]=s;}memset(dp,0,sizeof(dp));for(int i=1; i<=l1; i++){for(int j=1; j<=l2; j++){if(x[i]==y[j]){dp[i][j]=dp[i-1][j-1]+1;}else{dp[i][j]=max(dp[i-1][j],dp[i][j-1]);}}}dfs(l1,l2);cout<<endl;}return 0; }?
轉載于:https://www.cnblogs.com/yinbiao/p/9067766.html
總結
以上是生活随笔為你收集整理的POJ 2250 (LCS,经典输出LCS序列 dfs)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux最大线程数限制及当前线程数查询
- 下一篇: Linux学习总结(二) 网络配置-NA