zoukankan      html  css  js  c++  java
  • HDU 1503 Advanced Fruits

    Advanced Fruits

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 3622    Accepted Submission(s): 1872
    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
     题目大意就是找到最长公共子序列。然后输出输入的两个字符串,注意,取出来的公共子序列只输出一遍!
    好吧,我承认我当时也看不懂题意。只能按照公共子序列的套路来咯
     
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 int dp[105][105];
     5 using namespace std;
     6 struct Node{//i,j记录最长公共子序列在a,b串的位置,ch保存字符
     7     int i,j;
     8     char ch;
     9 };
    10 int main()
    11 {
    12     char a[105],b[105];
    13     while(~scanf("%s%s",a+1,b+1))
    14     {
    15         memset(dp,0,sizeof(dp));
    16         int alen=strlen(a+1);
    17         int blen=strlen(b+1);
    18         for(int i=1;i<=alen;i++)
    19         {
    20             for(int j=1;j<=blen;j++)
    21             {
    22                 if(a[i]==b[j]){
    23                     dp[i][j]=dp[i-1][j-1]+1;
    24                 }else{
    25                     dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
    26                 }
    27             }
    28         }
    29         Node ans[105];
    30         int len=0;
    31         if(dp[alen][blen]==0)
    32             printf("%s%s",a,b);
    33         else{
    34             int i=alen,j=blen;
    35             while(i!=0&&j!=0)//倒序取出最长公共子序列,得到最长公共子序列的dp过程的逆推
    36             {
    37                 if(dp[i][j]==dp[i-1][j-1]+1&&a[i]==b[j]){
    38                     ans[len].i=i;
    39                     ans[len].j=j;
    40                     ans[len++].ch=a[i];
    41                     i--;
    42                     j--;
    43                 }else if(dp[i-1][j]>dp[i][j-1]){
    44                     i--;
    45                 }else{
    46                     j--;
    47                 }
    48             }
    49         }
    50         int i=1,j=1;
    51         for(int k=len-1;k>=0;k--)//输出结果
    52         {
    53             while(i!=ans[k].i){
    54                 cout<<a[i];
    55                 i++;
    56             }
    57             while(j!=ans[k].j){
    58                 cout<<b[j];
    59                 j++;
    60             }
    61             cout<<ans[k].ch;
    62             i++;
    63             j++;
    64         }
    65         printf("%s%s",a+1+ans[0].i,b+1+ans[0].j);
    66         cout<<endl;
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    [转]C++中const、volatile、mutable的用法
    [转]pugixml使用教程
    [转]xml解析工具的效率比较QDomDocument、TinyXml-2、RapidXml、PugiXml
    [原][spark]帧序列的纹理UV索引,修改spark源码,改变纹理索引方式,支持常规帧序列
    [原][unreal][UE][spark]分析unreal engine 虚幻引擎的粒子编辑器:Cascade
    [转][C++]佛祖保佑,永无bug。C++ BUG解决方案
    [转]VS中的路径宏 OutDir、ProjectDir、SolutionDir各种路径含义
    [转]jsbsim基础概念
    [原][粒子特效][spark]调节器modifier
    项目管理(八)- 控制项目的范围
  • 原文地址:https://www.cnblogs.com/ISGuXing/p/7257678.html
Copyright © 2011-2022 走看看