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): 3613    Accepted Submission(s): 1867 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
     
     
    题意:这道题就是给你两个单词,然后你要把两个单词拼接成一个新单词,使得新单词的子序列中包含两个单词,并且要使这个新单词最短。所以这道题就是求最长公共子序列. 思路:将每个字符都进行标记,看两个字符串中对应的字符究竟处于什么状态,然后输出,其标记为公共子串的字符只输出一次.
     
    注意在所有的最长公共子序列中,数组都要从1开始,别从零开始,方便dp。
     
    #include<stdio.h>  
    #include<string.h>  
    #include<algorithm>  
    #include<iostream>
    using namespace std;  
    int dp[110][110];  
    char a[110],b[110];  
    char s[220];  
    int main()  
    {  
        while(~scanf("%s%s",a+1,b+1))  
        {  
            memset(dp,0,sizeof(dp));  
            int n = strlen(a+1);
            int m = strlen(b+1);
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){ //求出最长公共子序列长度 
                    if(a[i] == b[j]){
                        dp[i][j] = dp[i-1][j-1]+1;
                    }
                    else dp[i][j] = max(dp[i][j-1],dp[i-1][j]);
                }
            }
            int i,j,k;
            i = n;
            j = m;
            k = 0;
            while(dp[i][j]){//将要输出的字符串先到存到s数组中 
                if(a[i] == b[j]){//当a,b,中字符相等  
                    s[k++] = a[i];
                    i--;
                    j--;
                } 
                else if(dp[i][j-1] < dp[i-1][j]){ //注意这种情况选取a数组还是b数组  
                    s[k++] = a[i];
                    i--;
                }
                else if(dp[i][j-1] >= dp[i-1][j]){
                    s[k++] = b[j];
                    j--;
                }
            }
            while(i>=1){//a中不存在公共子序列的部分  
                s[k++] = a[i];
                i--;
            }
            while(j>=1){//b中不存在公共子序列的部分 
                s[k++] = b[j];
                j--;
            }
            for(int t = k-1;t>=0;t--){
                cout << s[t];
            }
            cout << endl;
        }  
        return 0;  
    }  
     
    彼时当年少,莫负好时光。
  • 相关阅读:
    程序命名标准规范(自定义与其他标准无关)
    asp.net(C#)excel导入导出类
    CSS 技巧
    sql server 更改端口之后的登入方式
    日志插件 log4net 的使用
    js动态调用方法
    mongodb
    Spring 之工具类中注入bin
    解释:什么是云计算?
    当 ADO.NET 遇上 dynamic
  • 原文地址:https://www.cnblogs.com/l609929321/p/7251279.html
Copyright © 2011-2022 走看看