zoukankan      html  css  js  c++  java
  • Advanced Fruits HDU杭电1503【LCS的保存】

    Problem Description
    The company "21st Century Fruits" has specialized in creating new sorts of fruits by transferring genes from one fruit into the genome of another one. Most times this method doesn't work, but sometimes, in very rare cases, a new fruit emerges that tastes like a mixture between both of them. 
    A big topic of discussion inside the company is "How should the new creations be called?" A mixture between an apple and a pear could be called an apple-pear, of course, but this doesn't sound very interesting. The boss finally decides to use the shortest string that contains both names of the original fruits as sub-strings as the new name. For instance, "applear" contains "apple" and "pear" (APPLEar and apPlEAR), and there is no shorter string that has the same property. 

    A combination of a cranberry and a boysenberry would therefore be called a "boysecranberry" or a "craboysenberry", for example. 

    Your job is to write a program that computes such a shortest name for a combination of two given fruits. Your algorithm should be efficient, otherwise it is unlikely that it will execute in the alloted time for long fruit names. 
     

    Input
    Each line of the input contains two strings that represent the names of the fruits that should be combined. All names have a maximum length of 100 and only consist of alphabetic characters.

    Input is terminated by end of file. 
     

    Output
    For each test case, output the shortest name of the resulting fruit on one line. If more than one shortest name is possible, any one is acceptable.
     

    Sample Input
    apple peach ananas banana pear peach
     

    Sample Output
    appleach bananas pearch
     


    /*
    这道题是这样输出的。首先读取s1的字符,读取时推断是否是公共字符。假设是就把s1前面的字符所有输出,
    然后就来推断s2。(和推断s1是一样的),然后把公共字符输出,一直这样推断,直到最后一个公共字符
    当最后一个公共字符推断完了后,把剩下的输出,先输s1,再s2.
    
    */
    
    #include<stdio.h>
    #include<string.h>
    #define max(a,b) (a)>(b)?

    (a):(b) char s1[200],s2[200]; int dp[200][200]; struct subsequence { int i,j; char ch; }common[120]; int main() { int i,j,k; int len1,len2; while(~scanf("%s%s",s1,s2)) { len1=strlen(s1); len2=strlen(s2); memset(dp,0,sizeof(dp)); for(i=1;i<=len1;++i) //LCS { for(j=1;j<=len2;++j) { if(s1[i-1]==s2[j-1]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i-1][j],dp[i][j-1]); } } if(dp[len1][len2]==0)//假设没有公共的,直接输出 { printf("%s%s ",s1,s2); continue; } else//有就開始记录 。倒着记录 { i=len1;j=len2; k=0; while(i>=1&&j>=1) { if(dp[i][j]==dp[i-1][j-1]+1&&s1[i-1]==s2[j-1])//最后一位同样,就存起来 { common[k].i=i-1;//记录s1串的公共子序列(最后一个。倒数第二个.....) 元素的位置 common[k].j=j-1;//记录s2串的公共子序列(最后一个,倒数第二个.....) 元素的位置 common[k].ch=s1[i-1];//记录该公共字符 i--,j--; k++; } else if(dp[i-1][j]>dp[i][j-1])//当去掉s1的最后一个元素的s1比去掉s2最后一个元素的最大公共子序列还要大的时候,说明s1的末尾不是最长子序列的一部分 i--; else j--; } } i=j=0; for(k=k-1;k>=0;--k) { while(common[k].i!=i)//先输出s1 { printf("%c",s1[i]); ++i; } while(common[k].j!=j)//再输s2 { printf("%c",s2[j]); ++j; } printf("%c",common[k].ch); ++i,++j; } while(s1[i]!='')//输出剩下的 { printf("%c",s1[i]); ++i; } while(s2[j]!='') { printf("%c",s2[j]); ++j; } puts(""); } return 0; }




  • 相关阅读:
    如何修改ls命令列出来的目录颜色
    如何替换vi的配色方案
    grep如何结尾匹配
    机器学习模型如何转换成零依赖代码
    在ubuntu bionic下对基于qemu的arm64进行linux内核5.0.1版本的编译和运行
    分析linux内核中的slub内存管理算法
    windows下如何解决chrome浏览器左下角总提示'Downloading proxy script'的问题
    发现vi出现此错误~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycm_core.so: undefined symbol: clang_getCompletionFixIt
    打开vi后提示The ycmd server SHUT DOWN (restart with :YcmRestartServer)该如何处理
    ubuntu下如何修改时区和时间
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/6885529.html
Copyright © 2011-2022 走看看