首页 > 代码库 > poj 2250 Compromise dp lcs 路径输出

poj 2250 Compromise dp lcs 路径输出

点击打开链接题目链接

Compromise
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6520 Accepted: 2922 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 werden
代码1:
#include<cstdio>
#include<cstring>
int dp[110][110];
char str1[110][35];
char str2[110][35];
char ans[110][110][110][35];
int main()
{
    int a,b,i,j;
    while(scanf("%s",str1[1])!=EOF)
    {
        b=1;a=2;
    while(1)
    {
        scanf("%s",str1[a]);
        if(str1[a][0]=='#')
        {
            a--;
            break;
        }
        a++;
    }
    while(1)
    {
        scanf("%s",str2[b]);
        if(str2[b][0]=='#')
        {
            b--;
            break;
        }
        b++;
    }
    memset(dp,0,sizeof(dp));
    memset(ans,0,sizeof(ans));
    int k;
    for(i=1;i<=a;i++)
    {
        for(j=1;j<=b;j++)
        {
            if(strcmp(str1[i],str2[j])==0)
            {
                dp[i][j]=dp[i-1][j-1]+1;
                for(k=0;k<dp[i-1][j-1];k++)
                {
                    strcpy(ans[i][j][k],ans[i-1][j-1][k]);
                }
                strcpy(ans[i][j][k],str1[i]);
            }
            else
            {
                if(dp[i-1][j]>=dp[i][j-1])
                {
                    dp[i][j]=dp[i-1][j];
                    for(k=0;k<dp[i-1][j];k++)
                    {
                        strcpy(ans[i][j][k],ans[i-1][j][k]);
                    }
                }
                else
                {
                    dp[i][j]=dp[i][j-1];
                    for(k=0;k<dp[i][j-1];k++)
                    {
                        strcpy(ans[i][j][k],ans[i][j-1][k]);
                    }
                }
            }
        }
    }
    for(i=0;i<dp[a][b];i++)
    {
        if(i<dp[a][b]-1)
            printf("%s ",ans[a][b][i]);
        else
            printf("%s\n",ans[a][b][i]);
    }
    }
    return 0;
}

代码2:
#include<cstdio>
#include<cstring>
char str1[110][35],str2[110][35],ans[110][35];
int dp[110][110],mark[110][110];
int t;
void Print(int i,int j)
{
    if(j==0&&i==0)
        return ;
    if(mark[i][j]==0)
    {
        Print(i-1,j-1);
        strcpy(ans[t++],str1[i-1]);
    }
    else if(mark[i][j]==1)
    {
        Print(i-1,j);
    }
    else if(mark[i][j]==-1)
    {
        Print(i,j-1);
    }

}
int main()
{
    int a,b,i,j,k;
    while(scanf("%s",str1[0])!=EOF)
    {
        a=1,b=0;
        t=0;
        memset(dp,0,sizeof(dp));
        memset(mark,0,sizeof(mark));
        memset(ans,0,sizeof(ans));

        while(1)
        {
            scanf("%s",str1[a]);
            if(str1[a][0]=='#')
            {
                break;
            }
            a++;
        }
        while(1)
        {
            scanf("%s",str2[b]);
            if(str2[b][0]=='#')
            {
                break;
            }
            b++;
        }
        for(int i=0;i<=a;i++)
		{
			mark[i][0]=1;
		}
		for(int i=0;i<=b;i++)
		{
			mark[0][i]=-1;
		}
        for(i=1;i<=a;i++)
        {
            for(j=1;j<=b;j++)
            {
                if(strcmp(str1[i-1],str2[j-1])==0)
                {
                    dp[i][j]=dp[i-1][j-1]+1;
                    mark[i][j]=0;
                }
                else if(dp[i-1][j]>dp[i][j-1])
                {
                    mark[i][j]=1;
                    dp[i][j]=dp[i-1][j];
                }
                else
                {
                    mark[i][j]=-1;
                    dp[i][j]=dp[i][j-1];
                }
            }
        }
        Print(a,b);
        for(i=0;i<t;i++)
        {
            printf("%s",ans[i]);
            if(i<t-1)
                printf(" ");
            else puts("");
        }
    }
    return 0;
}