UVA 531 Compromise(LCS路径还原)

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

题目大意:给出两篇文章,让你求出两篇文章的公共单词,按顺序输出。

文章?单词??别想多,单词对于文章,其实和字符对于字符串,”地位上是一样的”(理解一下)。就是找最长公共子序列罢了。

#include<iostream>
#include<vector>
#include<cstring>
#include<string>
using namespace std;
vector<string> s;         
vector<string> t;
 
int dp[1000][1000];      //dp数组 
int pre[1000][1000];      //标记路径 
vector<string> ans;       //保存路径

void output(int x,int y){
	if(x==0 || y==0) 
	return ;
	else if(pre[x][y]==1){	
		output(x-1,y-1);
		ans.push_back(s[x-1]);
	}
	else if(pre[x][y]==2)
	   output(x-1,y);
	else output(x,y-1);    
}

int main(){
	string r;
	int cnt=0;
	while(cin>>r){
	   if(r=="#") cnt++;  
	   
	   if(cnt==0) s.push_back(r);
	   
	   else if(cnt==1){
	   	 if(r=="#"){        //这里要注意一下,不跳过的话t(第二篇文章)开头会多一个"#". 
	   	    cnt++; continue;  	
		 }
	   	 t.push_back(r); 
	   }
	   else if(cnt==2){    	  	   	   	   	   	   
	   	   int n=s.size(),m=t.size();
	   	   for(int i=0;i<n;i++){
	   	   	   for(int j=0;j<m;j++){
	   	   	       if(s[i]==t[j]){ 
				         dp[i+1][j+1]=dp[i][j]+1;
				         pre[i+1][j+1]=1;
				   }
				   else{
				   	     if(dp[i][j+1]>dp[i+1][j]){
				   	     	   dp[i+1][j+1]=dp[i][j+1];
				   	     	   pre[i+1][j+1]=2;
						 }
						 else{
						 	   dp[i+1][j+1]=dp[i+1][j];
						 	   pre[i+1][j+1]=3;
						 }
				    }
			    }
		    }
		    output(s.size(),t.size()); 
		    for(int i=0;i<ans.size();i++){
		    	if(i!=ans.size()-1)
		    	cout<<ans[i]<<" ";
		    	else
		    	cout<<ans[i]<<endl;
			}
		    ans.clear(); s.clear(); t.clear();  //不要忘记初始化 
		    cnt=0;
		    memset(pre,0,sizeof(pre));
		    memset(dp,0,sizeof(dp));
	   }	
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40922859/article/details/81545962