Compromise (LCS记录路径)

题目链接:https://vjudge.net/contest/244922#problem/Q

参考链接:https://blog.csdn.net/bmamb/article/details/51333154

 Compromise

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<string> 
#include<cstdio>
#include<cstring>
using namespace std;
string s1[105];
string s2[105];
int vec[105][105];
int findx[105];
string s;
int main(){
	int cut1=1;
	int cut2=1;	
	while(cin>>s){
		
		memset(findx,0,sizeof(findx));
		memset(vec,0,sizeof(vec));
		
		s1[cut1++]=s;
		while(cin>>s){
			if(s!="#"){
				s1[cut1++]=s;
			}else{
				break;
			}
		}
		while(cin>>s){
			if(s!="#"){
				s2[cut2++]=s;
			}else{
				break;
			}
		}
		int c=0;
		for(int i=1;i<cut1;i++){
			for(int j=1;j<cut2;j++){
				if(s1[i]==s2[j]){
					vec[i][j]=vec[i-1][j-1]+1;
				}else{
					vec[i][j]=max(vec[i-1][j],vec[i][j-1]);
				}
			}
		}
		int i=cut1-1;
		int j=cut2-1;
		int cut=0;
		while(vec[i][j]){//回溯寻找路径, 
			// 如果vec[i][j]==vec[i-1][j],说明vec[i][j]是从vec[i-1][j]过来的; 
			// 那么i--; 
			if(vec[i][j]==vec[i-1][j]){
				i--;
				
			// 如果vec[i][j]==vec[i][j-1],说明vec[i][j]是从vec[i][j-1]过来的; 
			// 那么j--; 
			}else if(vec[i][j]==vec[i][j-1]){
				j--;
				
			// 如果vec[i][j]==vec[i-1][j-1],说明vec[i][j]是从vec[i-1][j-1]过来的; 
			// 此时需要记录路径; 
			}else{
				findx[cut++]=i;//将i的值放入findx[]数组中,i就是最长公共子序列在s1中的坐标; 
				i--;
				j--;
			}
		}
		for(int i=cut-1;i>0;i--){
			cout<<s1[findx[i]]<<' ';
		}
		cout<<s1[findx[0]];
		cout<<endl;
		cut1=1;
		cut2=1;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/guozuofeng/article/details/81604136