AYITACM2016省赛第三周 C-妥协(最长公共单词序列)

来源:互联网 发布:陕西网络广播 编辑:程序博客网 时间:2024/05/21 09:46


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 file 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 there will be at least one common word in each text. Each text 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

分析:

题意很简单,就是寻找两个文本文件的公共单词,动态规划的灵活运用,最重要的就是找到状态转移方程,多做题,了解动态规划的思想,就不会感觉那么难了!,再加上递归打印出结果,还是感觉灵活运用时最重要的!

#include<stdio.h>#include<string.h>int a[110][110],p[110][110],c,c1,f;char d1[110][35],d2[110][35],t;void LCS( ){    int i,j;    memset(p,0,sizeof(p));    memset(a,0,sizeof(a));    for(i=1; i<c; i++)        for(j=1; j<c1; j++)            if(!strcmp(d1[i],d2[j]))  //如果两个单词相等            {                p[i][j]=p[i-1][j-1]+1;//公共序列长度加一                a[i][j]=1;            }            else if(p[i][j-1]<p[i-1][j]) //如果不相同,哪个序列长就等于哪个            {                p[i][j]=p[i-1][j];                a[i][j]=2;            }            else            {                p[i][j]=p[i][j-1];                a[i][j]=3;            }}void print(int i,int j){    if(a[i][j]==1)//如果等于1说明是公共单词       {        print(i-1,j-1);//递归输出所有的结果        if(f)            f=0;        else            printf(" ");        printf("%s",d1[i]);    }    else if(a[i][j]==2)        print(i-1,j);    else if(a[i][j]==3)//寻找下一个公共单词        print(i,j-1);}int main(){    int i,j;    while(scanf("%s",d1[1])!=EOF)    {        c=2,f=1,c1=1;        while(scanf("%s",d1[c])&&d1[c][0]!='#')            c++;        while(scanf("%s",d2[c1])&&d2[c1][0]!='#')            c1++;        LCS();//找出最长公共子序列        print(c-1,c1-1);//输出最长公共子序列        printf("\n");    }    return 0;}



0 0
原创粉丝点击