• 刷题总结——advanced fruits(hud1503)


    题目:

    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. 

    InputEach 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. 
    OutputFor 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解决最长公共子序列问题····

    关于这类问题参考:http://blog.csdn.net/yysdsyl/article/details/4226630/,这里就不多说了···

    这道题其实和要求输出最长公共子序列问题的方法基本是一样的··只不过在dfs时除了要输出最长公共子序列以外还要按顺序输出两个单词其他部分的子串·····详细见dfs时的输出·····

    看不懂可以结合上面链接中画的图

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<ctime>
    #include<string>
    #include<cstring>
    #include<algorithm>
    #include<cctype>
    using namespace std;
    const int N=105;
    char s[N],t[N],ans[N];
    int n,m,f[N][N],d[N][N];
    inline void dp()
    {
      for(int i=1;i<=n;i++)  d[i][0]=0;
      for(int i=1;i<=m;i++)  d[0][i]=1;
      for(int i=1;i<=n;i++)    
        for(int j=1;j<=m;j++)
        {  
          if(s[i]==t[j])  f[i][j]=f[i-1][j-1]+1,d[i][j]=2;  
          else if(f[i][j-1]>f[i-1][j])  f[i][j]=f[i][j-1],d[i][j]=1;    
          else f[i][j]=f[i-1][j],d[i][j]=0;
        }
    }
    inline void dfs(int x,int y)
    { 
      if(x==0&&y==0)  return;  
      else 
      {
        if(d[x][y]==2)  {dfs(x-1,y-1);printf("%c",s[x]);}
        else if(d[x][y]==1)  {dfs(x,y-1);printf("%c",t[y]);} 
        else if(d[x][y]==0)  {dfs(x-1,y);printf("%c",s[x]);} 
      }
    }
    int main()
    {
      freopen("a.in","r",stdin);
      while(~scanf("%s%s",s+1,t+1))    
      {
        memset(f,0,sizeof(f));memset(d,0,sizeof(d));
        n=strlen(s+1);m=strlen(t+1);
        dp();dfs(n,m);printf("
    ");
      }
      return 0;
    }
  • 相关阅读:
    innerHTML和innerText
    Function 构造器及其对象、方法
    构造函数
    jquery 获取及设置input各种类型的值
    在浏览器中输入URL并回车后都发生了什么?
    :after和:before的作用及使用方法
    使用JS监听键盘按下事件(keydown event)
    Javascript模块化编程(三):require.js的用法
    Javascript模块化编程(二):AMD规范
    Javascript模块化编程(一):模块的写法
  • 原文地址:https://www.cnblogs.com/AseanA/p/7644326.html
Copyright © 2020-2023  润新知