zoukankan      html  css  js  c++  java
  • HDU

    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

    首先要看懂题意,题目说两个亲本的名字要作为新一代植株名字的母串,且新名字要尽可能的短。

    思路很明显,最长公共字符串问题,两个名字中相同的字符串肯定只需要输出一次就够了,然后将剩下的字符按顺序输出即可。

    当时不知道怎么按顺序输出,一直在考虑两个字符串同步输出,后来才想到先定下一个,然后在需要时再输出另一个的方法。
    两个字符串同时输出,肯定是不可能的啊!竟然犯这么低级的错误,太恶心了。。。。

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string.h>
     4 #include<math.h>
     5 #include<algorithm>
     6 #include<queue>
     7 #include<stack>
     8 #include<deque>
     9 #include<iostream>
    10 using namespace std;
    11 typedef long long  LL;
    12 char str1[110],str2[110];
    13 int dp[110][110];
    14 void output(int i,int j)
    15 {
    16     if(i==0||j==0)
    17         return ;
    18     else
    19     {
    20         if(str1[i-1]==str2[j-1])
    21         {
    22             output(i-1,j-1);
    23             str1[i-1]-=90;
    24             str2[j-1]-=90;
    25         }
    26         else
    27         {
    28             if(dp[i][j-1]>=dp[i-1][j])
    29                 output(i,j-1);
    30             else
    31                 output(i-1,j);
    32 
    33         }
    34     }
    35 }
    36 int main()
    37 {
    38     int i,p,j,k;
    39     int len1,len2;
    40     while(scanf("%s%s",str1,str2)!=EOF)
    41     {
    42         getchar();
    43         memset(dp,0,sizeof(dp));
    44         len1=strlen(str1);
    45         len2=strlen(str2);
    46         for(i=1; i<=len1; i++)
    47             for(j=1; j<=len2; j++)
    48             {
    49                 if(str1[i-1]==str2[j-1])
    50                     dp[i][j]=dp[i-1][j-1]+1;
    51                 else
    52                     dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
    53             }
    54         k=0;
    55         output(len1,len2);
    56         /*
    57         for(i=0;i<len1;i++)
    58             if(str1[i]<50)
    59                 printf("%c",str1[i]+90);*/
    60 
    61         for(i=0;i<len1;i++)
    62         {
    63 
    64             if(str1[i]<50)
    65             {
    66                 char mid=str1[i]+90;
    67                 for(j=k;j<len2;j++)
    68                 {
    69                     if(str2[j]<50)
    70                     {
    71                         str2[j]=0;
    72                         printf("%c",mid);
    73                         break;
    74                     }
    75                     else
    76                     {
    77                         printf("%c",str2[j]);
    78                     }
    79                 }
    80                 k=j+1;
    81             }
    82             else
    83                 printf("%c",str1[i]);
    84         }
    85         for(j=k;j<len2;j++)
    86             printf("%c",str2[j]);
    87         putchar('
    ');
    88     }
    89     return 0;
    90 }
    View Code
  • 相关阅读:
    SpringBoot中添加事务
    隐藏样式
    Mybatis配置解析
    题目1064:反序数------玩转小聪明
    题目1063:整数和
    题目1062:分段函数23333333333333
    题目1060:完数VS盈数------这题做得我想骂人
    题目1059:abc----------就喜欢这样的题
    题目1050:完数-----------runtime error的问题
    题目1049:字符串去特定字符
  • 原文地址:https://www.cnblogs.com/daybreaking/p/9384350.html
Copyright © 2011-2022 走看看