• POJ 2250 Compromise(LCS)


    POJ 2250 Compromise(LCS)解题报告

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/D

    题目:

    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
    
    题目大意:
    给出两篇文章,文章有单词构成,每个单词由空格隔开,文章以#结束,输出两篇文章的公共单词串。

    分析:
    LCS(最长公共子序列问题)。单词串是一个个单词。用二维数组来记录每次的结果,当一个单词完全相等的时候输出。

    代码:
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 using namespace std;
     5 
     6 char a[35][105],b[35][105],c[35][105];
     7 int dp[105][105],mark[105][105],l1,l2,cnt;
     8 
     9 void LCS()
    10 {
    11     int i,j;
    12     memset(dp,0,sizeof(dp));
    13     memset(mark,0,sizeof(mark));
    14     for(i=0;i<=l1;i++)
    15         mark[i][0]=1;
    16     for(i=0;i<=l2;i++)
    17         mark[0][i]=-1;
    18     for(i=1;i<=l1;i++)
    19     {
    20         for(j=1;j<=l2;j++)
    21         {
    22             if(!strcmp(a[i-1],b[j-1]))
    23             {
    24                 dp[i][j]=dp[i-1][j-1]+1;
    25                 mark[i][j]=0;
    26             }
    27             else if(dp[i-1][j]>=dp[i][j-1])
    28             {
    29                 dp[i][j]=dp[i-1][j];
    30                 mark[i][j]=1;
    31             }
    32             else
    33             {
    34                 dp[i][j]=dp[i][j-1];
    35                 mark[i][j]=-1;
    36             }
    37         }
    38     }
    39 }
    40 
    41 void print(int i,int j)
    42 {
    43     if(!i&&!j)
    44         return;
    45     if(mark[i][j]==0)
    46     {
    47         print(i-1,j-1);
    48         strcpy(c[cnt++],a[i-1]);
    49     }
    50     else if(mark[i][j]==1)
    51         print(i-1,j);
    52     else 
    53         print(i,j-1);
    54 }
    55 
    56 int main()
    57 {
    58     while(scanf("%s",a[0])!=EOF)
    59     {
    60         l1=1;
    61         while(strcmp(a[l1-1],"#"))
    62             scanf("%s",a[l1++]);
    63         l1=l1-1;
    64         scanf("%s",b[0]);
    65         l2=1;
    66         while(strcmp(b[l2-1],"#"))
    67             scanf("%s",b[l2++]);
    68         LCS();
    69         cnt=0;
    70         print(l1,l2);
    71         printf("%s",c[0]);
    72         for(int i=1;i<cnt;i++)
    73             printf(" %s",c[i]);
    74         printf("
    ");
    75     }
    76     return 0;
    77 }
    
    
    




  • 相关阅读:
    KBEngine源码:EntityCall
    skynet 学习笔记-sproto模块(2)
    mongodb:为什么用mongodb
    编写高效服务器程序,需要考虑的因素
    b+树
    mysql:架构
    超越函数/微分方程 /积分中的技术/级数
    积分从入门到放弃<2>
    PyQt4 / PyQt5
    图形学算法:
  • 原文地址:https://www.cnblogs.com/ttmj865/p/4732660.html
Copyright © 2020-2023  润新知