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;
}

poj 2250 Compromise dp lcs 路径输出

时间: 2024-10-29 00:28:19

poj 2250 Compromise dp lcs 路径输出的相关文章

POJ 2250 Compromise (DP,最长公共子序列)

Compromise Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6440 Accepted: 2882 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 fulfille

poj 2250 Compromise (LCS)

题目大意:给出两段文字,求出最长的公共单词串. 直接是以前的代码改一点就A了. #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; char s1[35][100],s2[35][100],s[35][100]; int len1,len2,dp[105][105],mark[105][105],l; void LCS() { int i,j; memset(d

周赛 POJ 2250 Compromise

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 Germa

LightOJ 1110 An Easy LCS LCS路径输出

点击打开链接题目链接 1110 - An Easy LCS PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB LCS means 'Longest Common Subsequence' that means two non-empty strings are given; you have to find the Longest Common Subsequence between them.

LIS POJ 2250 Compromise

题目传送门 1 /* 2 LIS模板题:题目看错了,是求单词的最长上升子序列! 3 编程好累:) 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <algorithm> 9 #include <string> 10 using namespace std; 11 12 const int MAXN = 1e2 + 10; 13 const

POJ 2250 Compromise(最长公共子序列LCS)

http://poj.org/problem?id=2250 题意: 给你两段由空格分隔的语句, 要你求该两段语句的最长公共子序列. 且随便输出一个解即可. 注意每个单词需要看成我们一般处理字符串子序列的一个单独字符. 即每个单词是一个整体. 分析: 与往常计算最长公共子序列一样的方式即可. 然后用DFS输出序列即可.本题与POJ1458提供的解法本质一样. http://blog.csdn.net/u013480600/article/details/40741333 AC代码: #inclu

POJ 2250 Compromise (线性dp LCS +递归路径)

Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6735   Accepted: 3009   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 fu

POJ 1934 Trip(LCS+枚举+输出所有路径)

这道题一看就是LCS,直接写个裸的,硬搜.TLE void print(int r,int c,int n) { if(dp[r][c]==0) { string ss=tp; ans[ss]=1; //sprintf(ansstr[cnt++],"%s\n",tp); return; } if(ph[r][c]==1) tp[n]=sa[r-1],print(r-1,c-1,n-1); else if(ph[r][c]==3) print(r,c-1,n),print(r-1,c,n

POJ 1015 Jury Compromise DP+记录路径

找每个点能转移出去的状态时要回溯到根去掉所有能转移的点来去重.. 可能这种做法在距离根距离较小的时候能用..(隐隐感觉有bug,还是人云亦云地做掉先了..) #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include