zoukankan      html  css  js  c++  java
  • Advanced Fruits(好题,LCS的模拟)

    Advanced Fruits

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2052    Accepted Submission(s): 1053
    Special Judge


    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
     题解:
    这个题就是让找出这两个串的最长公共子序列,然后加上这两个串中减去公共子序列的字符,输出就行;
    我的思路就是先求出最长公共子序列的dp数组,然后再倒过来,模拟dp数组走的路径倒着记录就行了;
    代码:
     
     1 #include<stdio.h>
     2 #include<string.h>
     3 #define MAX(x,y) x>y?x:y
     4 const int MAXN=110;
     5 int dp[MAXN][MAXN];
     6 char s1[MAXN],s2[MAXN],ans[MAXN*2];
     7 int t1,t2,t;
     8 void LCS(){
     9     memset(dp,0,sizeof(dp));
    10     t1=strlen(s1+1);t2=strlen(s2+1);
    11     for(int i=1;i<=t1;i++){
    12         for(int j=1;j<=t2;j++){
    13             if(s1[i]==s2[j])dp[i][j]=dp[i-1][j-1]+1;
    14             else{
    15                 dp[i][j]=MAX(dp[i-1][j],dp[i][j-1]);
    16             }
    17         }
    18     }
    19 }
    20 void add(char a){
    21     ans[t++]=a;
    22     ans[t]='';
    23 }
    24 void print(){
    25     while(dp[t1][t2]){
    26             if(s1[t1]==s2[t2]){
    27                 add(s1[t1]);
    28                 t1--;t2--;
    29             }
    30             else{
    31                 if(dp[t1-1][t2]>dp[t1][t2-1]){
    32                     add(s1[t1]);
    33                     t1--;
    34                 }
    35                 else{
    36                     add(s2[t2]);
    37                     t2--;
    38                 }
    39             }
    40         }
    41         while(t1>0)add(s1[t1--]);
    42     while(t2>0)add(s2[t2--]);    
    43 }
    44 int main(){
    45     while(~scanf("%s%s",s1+1,s2+1)){
    46         t=0;
    47         LCS();
    48         print();
    49         for(int i=t-1;i>=0;i--)printf("%c",ans[i]);
    50         puts("");
    51     }
    52     return 0;
    53 }

  • 相关阅读:
    Python装饰器的解包装(unwrap)
    《Python cookbook》 “定义一个属性可由用户修改的装饰器” 笔记
    关于Python的函数(Method)与方法(Function)
    判断python对象是否可调用的三种方式及其区别
    tornado返回指定的http code
    Mac下安装pymssql
    iptables
    OpenFlow通信流程解读
    Prometheus的架构及持久化
    ansible总结
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4726261.html
Copyright © 2011-2022 走看看