• Advanced Fruits(HDU 1503 LCS变形)


    Advanced Fruits

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2358    Accepted Submission(s): 1201
    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
     
    Source
    先求出最长公共子序列LCS,然后就是掉渣天的回溯法输出只含有一个公共序列并且包含输入的两个序列,LCS的变形题。这里需要设置一个标志数组,用来记录dp的刷新路径。从而在回溯的时候根据标志数组来沿着路径输出。
     
    贴一个回溯求出公共子序列的代码:
      
     1 #include <cstring>
     2 #include <algorithm>
     3 #include <cstdio>
     4 #include <iostream>
     5 using namespace std;
     6 #define Max 102
     7 int dp[Max][Max];
     8 int mark[Max][Max];
     9 char s[Max],t[Max];
    10 int len1,len2;
    11 void LCS()        //计算LCS,并用mark标记数组记录dp数组的传递过程
    12 {
    13     int i,j;
    14     memset(mark,0,sizeof(mark));
    15     memset(dp,0,sizeof(dp));
    16     for(i=1;i<=len1;i++)
    17     {
    18         for(j=1;j<=len2;j++)
    19         {
    20             if(s[i-1]==t[j-1])
    21             {
    22                 dp[i][j]=dp[i-1][j-1]+1;
    23             //    cout<<s[i-1]<<" ";                                                                      
    24                 mark[i][j]=0;
    25             }
    26             else if(dp[i-1][j]>=dp[i][j-1])
    27             {
    28                 dp[i][j]=dp[i-1][j];        //从上面传递下来
    29                 mark[i][j]=1;
    30             }
    31             else
    32             {
    33                 dp[i][j]=dp[i][j-1];        //从左边传递下来
    34                 mark[i][j]=2;
    35             }
    36         }
    37     }
    38 
    39         return;
    40 }
    41 void output(int i,int j)        //回溯输出
    42 {
    43     /*if(i==0&&j!=0)
    44     {
    45         output(i,j-1);
    46         //printf("%c",t[j-1]);
    47     }
    48     else if(i!=0&&j==0)
    49     {
    50         output(i-1,j);
    51         //printf("%c",s[i-1]);
    52     }
    53     else if(i==0&&j==0)
    54         return;*/
    55     if(i==0||j==0)
    56         return;
    57     if(mark[i][j]==0)
    58     {
    59         output(i-1,j-1);
    60         printf("%c",s[i-1]);
    61     }
    62     else if(mark[i][j]==1)
    63     {
    64         output(i-1,j);
    65         //printf("%c",s[i-1]);
    66     }
    67     else
    68     {
    69         output(i,j-1);
    70         //printf("%c",t[j-1]);
    71     }
    72     return;
    73 }
    74 int main()
    75 {
    76     int i,j;
    77     freopen("in.txt","r",stdin);
    78     while(scanf("%s%s",s,t)!=EOF)
    79     {
    80         len1=strlen(s),len2=strlen(t);
    81         LCS();
    82         output(len1,len2);
    83         printf("
    ");
    84     }
    85 }
    ACcode:
     1 #include <cstring>
     2 #include <algorithm>
     3 #include <cstdio>
     4 #include <iostream>
     5 using namespace std;
     6 #define Max 102
     7 int dp[Max][Max];
     8 int mark[Max][Max];
     9 char s[Max],t[Max];
    10 int len1,len2;
    11 void LCS()        //计算LCS,并用mark标记数组记录dp数组的传递过程
    12 {
    13     int i,j;
    14     memset(mark,0,sizeof(mark));
    15     memset(dp,0,sizeof(dp));
    16     for(i=1;i<=len1;i++)
    17     {
    18         for(j=1;j<=len2;j++)
    19         {
    20             if(s[i-1]==t[j-1])
    21             {
    22                 dp[i][j]=dp[i-1][j-1]+1;
    23             //    cout<<s[i-1]<<" ";                                                                      
    24                 mark[i][j]=0;
    25             }
    26             else if(dp[i-1][j]>=dp[i][j-1])
    27             {
    28                 dp[i][j]=dp[i-1][j];        //从上面传递下来
    29                 mark[i][j]=1;
    30             }
    31             else
    32             {
    33                 dp[i][j]=dp[i][j-1];        //从左边传递下来
    34                 mark[i][j]=2;
    35             }
    36         }
    37     }
    38 
    39         return;
    40 }
    41 void output(int i,int j)        //回溯输出
    42 {
    43     if(i==0&&j!=0)
    44     {
    45         output(i,j-1);
    46         printf("%c",t[j-1]);
    47     }
    48     else if(i!=0&&j==0)
    49     {
    50         output(i-1,j);
    51         printf("%c",s[i-1]);
    52     }
    53     else if(i==0&&j==0)
    54         return;
    55     else if(mark[i][j]==0)
    56     {
    57         output(i-1,j-1);
    58         printf("%c",s[i-1]);
    59     }
    60     else if(mark[i][j]==1)
    61     {
    62         output(i-1,j);
    63         printf("%c",s[i-1]);
    64     }
    65     else
    66     {
    67         output(i,j-1);
    68         printf("%c",t[j-1]);
    69     }
    70     return;
    71 }
    72 int main()
    73 {
    74     int i,j;
    75     freopen("in.txt","r",stdin);
    76     while(scanf("%s%s",s,t)!=EOF)
    77     {
    78         len1=strlen(s),len2=strlen(t);
    79         LCS();
    80         output(len1,len2);
    81         printf("
    ");
    82     }
    83 }
     
  • 相关阅读:
    点击添加,上面div获取到值
    解决ios浏览器视频无法播放问题
    jquery中使用$(#form).submit()提交表单无效原因分析及解决Script
    html页面,css引用外部字体
    当页面信息填写不完整,点击提交后,直接跳转到未填写位置
    layui日历控件添加js校验后 页面点击提交后,点击日历插件不起作用
    jquery在线引用
    左侧导航高度,自适应右侧内容高度
    Mysql
    Linux文件编辑器,链接,及常用系统命令
  • 原文地址:https://www.cnblogs.com/a1225234/p/5223490.html
Copyright © 2020-2023  润新知