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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 给出一个$n\times m$的矩阵,每个位置上有一个非负整数,代表这个位置的海拔高度。一开始时,有一个人站在其中一个位置上。这个人可以向上、下、左、右四个方向移动,但是只能移动到海拔高度比当前位置低或者相等的位置上。一次移动只能移动一个单位长度。定义一个位置为“山顶”,当且仅当从这个位置开始移动,可以一直走到海拔高度比它低的位置上。请问,这个矩阵中最多有多少个“山顶”? 输入格式 第一行两个整数,分别表示$n$和$m$。 接下来$n$行,每行$m$个整数,表示整个矩阵。 输出格式 输出一个整数,表示最多有多少个“山顶”。 样例输入 4 4 3 2 1 4 2 3 4 3 5 6 7 8 4 5 6 7 样例输出 5 算法1 (递归dp) $O(nm)$ 对于这道题,我们可以使用递归DP来解决,用$f(i,j)$表示以$(i,j)$为起点的路径最大长度,那么最后的答案就是所有$f(i,j)$中的最大值。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码 算法2 (动态规划) $O(nm)$ 动态规划的思路与递归DP类似,只不过转移方程和实现方式有所不同。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值