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

题意:求两段文本的最长公共文本;

思路:最长公共子序列+打印公共序列;

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[505][505],num1,num2;
char s[505][505],s1[505][505],s2[505][505];
void lcs(int a,int b)
{
    if(a==0||b==0) return;
    if(s[a][b]==0)
    {
        lcs(a-1,b-1);printf("%s ",s1[a]);
    }
    else if(s[a][b]==-1)
    {
        lcs(a-1,b);
    }
    else
    {
        lcs(a,b-1);
    }
}
int main()
{
    int i,j,k;
    while(scanf("%s",s1[1])!=EOF)
    {
        num1=2,num2=1;
        memset(s,0,sizeof(s));
        memset(dp,0,sizeof(dp));
        while(scanf("%s",s1[num1])&&s1[num1][0]!=‘#‘) num1++;
        while(scanf("%s",s2[num2])&&s2[num2][0]!=‘#‘) num2++;
        for(i=1;i<num1;i++)
        {
            for(j=1;j<num2;j++)
            {
                if(!strcmp(s1[i],s2[j]))
                {
                    dp[i][j]=dp[i-1][j-1]+1;s[i][j]=0;
                }
                else
                {
                    if(dp[i][j-1]>dp[i-1][j])
                    {
                        dp[i][j]=dp[i][j-1];s[i][j]=1;
                    }
                    else
                    {
                        dp[i][j]=dp[i-1][j];s[i][j]=-1;
                    }
                }

            }
        }
        lcs(num1-1,num2-1);
        printf("\n");
    }
    return 0;
}
时间: 2025-01-14 15:30:33

POJ 2250 Compromise(最长公共子序列)的相关文章

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

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

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

compromise Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u 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 n

周赛F题 POJ 1458(最长公共子序列)

F - F Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another

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 &amp; UVA 531 Compromise(字符串、 最长公共子序列)

Compromise 题目: 题目大意: 这里有两篇短文,每篇短文有若干个单词,求这两篇短文中的最长公共子序列,并将其输出来! 没篇短文输入 为 "#" 时,结束该篇短文的输入. 这道题是多组测试数据,如果只写一组,那么就会 WA,我因为这就被 WA 了一次! 最长公共子序列的解法,就不多说了,基本上所有的算法书上都有介绍讲解. 这道题,题意和解法我认为都不是难点,我个人认为难点是在最长公共子序列的保存记录上. 对于最长公共子序列的记录保存上,我用了 C++ 的 STL 中的strin

POJ 2250(最长公共子序列 变形)

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

POJ 3356 AGTC(最长公共子序列)

AGTC Description Let x and y be two strings over some finite alphabet A. We would like to transform x into y allowing only operations given below: Deletion: a letter in x is missing in y at a corresponding position. Insertion: a letter in y is missin

POJ 1458 - Common Subsequence(最长公共子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=1458 题目大意: 有若干组数据,每组给出两个字符串(中间用任意数量的空格间隔),输出这两个字符串最长公共子序列的长度.每次输出后换行. 分析: 动态规划求LCS,f[i][j]表示第一个字符串匹配到第i位,第二个字符串匹配到第j位时最长公共子序列的长度. 转移方程:当a[i] = b[i]时,f[i][j] = f[i-1][j-1]+1,其他情况时f[i][j

POJ 1458 Common Subsequence(最长公共子序列LCS)

POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列长度. 分析: 本题不用输出子序列,非常easy,直接处理就可以. 首先令dp[i][j]==x表示A串的前i个字符和B串的前j个字符的最长公共子序列长度为x. 初始化: dp全为0. 状态转移: IfA[i]==B[j] then dp[i][j]= dp[i-1][j-1]+1 else dp[