您的位置:首页 > 其它

UVA 531 Compromise(LCS算法+打印路径)

2015-03-18 18:47 369 查看


Compromise

Time Limit: 3000ms Memory Limit: 131072KB

[PDF Link]
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 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算法+打印路径,增加path【】【】数组。

代码:

#include<iostream>
#include<string>
#include<cstring>
#include<vector>

using namespace std;

const int maxn = 105;
string s1[maxn],s2[maxn];
int m,n,dp[maxn][maxn],path[maxn][maxn];
vector <string> answer;

void LcsLength(){
    memset(dp, 0, sizeof(dp));
    memset(path, 0, sizeof(path));
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            if(s1[i-1]==s2[j-1]) {
                dp[i][j]=dp[i-1][j-1]+1;
                path[i-1][j-1]=1;//path[m]
没有被赋值,只跟新到了path[m-1][n-1],利用#号多读一个,就可以访问到最后一个单词了
            }
            else{
                if(dp[i-1][j]>=dp[i][j-1]) {
                    dp[i][j]=dp[i-1][j];
                    path[i-1][j-1]=2;
                }
                else {
                    dp[i][j]=dp[i][j-1];
                    path[i-1][j-1]=3;
                }
            }
        }
    }

}

void LCSPrint(int i,int j){
    if(i<0||j<0) return ;
    if(path[i][j]==1){
        LCSPrint(i-1,j-1);
        answer.push_back(s1[i]);
    }
    else if(path[i][j]==2){
        LCSPrint(i-1,j);
    }
    else  LCSPrint(i,j-1);
}

void output(){
    for(int i=0;i<answer.size();i++){
        if(i!=0) cout<<" ";
        cout<<answer[i];
    }
    cout<<endl;
}
int main(){
    int casen=0,r=0;
    while(cin>>s1[0]){
        answer.clear();
        for(int i=1;;i++){
            cin>>s1[i];
            m=i;
            if(s1[i]=="#") break;

        }
         for(int i=0;;i++){
            cin>>s2[i];
            n=i;
            if(s2[i]=="#") break;

        }
        LcsLength();
        LCSPrint(m-1,n-1);//把#号丢掉
        output();
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: