zoukankan      html  css  js  c++  java
  • HDU1867 A + B for you again(KMP)

    A + B for you again

    Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 7978    Accepted Submission(s): 2005


    Problem Description
    Generally speaking, there are a lot of problems about strings processing. Now you encounter another such problem. If you get two strings, such as “asdf” and “sdfg”, the result of the addition between them is “asdfg”, for “sdf” is the tail substring of “asdf” and the head substring of the “sdfg” . However, the result comes as “asdfghjk”, when you have to add “asdf” and “ghjk” and guarantee the shortest string first, then the minimum lexicographic second, the same rules for other additions.
     
    Input
    For each case, there are two strings (the chars selected just form ‘a’ to ‘z’) for you, and each length of theirs won’t exceed 10^5 and won’t be empty.
     
    Output
    Print the ultimate string by the book.
     
    Sample Input
    asdf sdfg asdf ghjk
     
    Sample Output
    asdfg asdfghjk
     
    Author
    Wang Ye
     
    Source
     
    Recommend
    lcy
     
    分析:合并的时候,一个字符串的后缀和另一个字符串的前缀的公共部分只计算一次,于是很容易想到KMP的Next数组的定义(前缀和后缀的最大匹配长度)
    故可以把它们连接在一起(中间用'#'分割)求出Next数组,Next[len]就等于它们前缀和后缀的最大公共长度,然后就能生成它们合并后的字符串1.  因为题目中所给的两个字符串谁在前,谁在后是不一定的, 所以再把题目中的两个字符串颠倒再求一次,生成字符串2.  比较它们的长度和字典序,就能得出答案
    代码如下:
    #include <cstdio>
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    const int N = 2e5+10;
    int Next[N];
    char S[N], T[N];
    char a[N],b[N];
    char tmp[N];
    char res1[N],res2[N];
    int slen, tlen;//注意每次一定要计算长度
    int alen,blen;
    int ans;
    int len1,len2;
    void getNext()
    {
        int j, k;
        j = 0; k = -1; Next[0] = -1;
        while(j < tlen)
            if(k == -1 || T[j] == T[k])
                Next[++j] = ++k;
            else
                k = Next[k];
    
    }
    int main()
    {
        while(scanf("%s%s",a,b)!=EOF){
         alen=strlen(a);
         blen=strlen(b);
         for(int i=0;i<blen;i++)
            T[i]=b[i];
          T[blen]='#';
         for(int i=0;i<alen;i++)
         T[blen+1+i]=a[i];
         tlen=alen+blen+1;
         T[tlen]=0;
         getNext();
         ans=Next[tlen];
         for(int i=0;i<alen;i++)
         res1[i]=a[i];
         for(int i=ans;i<blen;i++)
         res1[alen+i-ans]=b[i];
         len1=alen+blen-ans;
         res1[len1]=0;
         memcpy(tmp,a,sizeof(a));
         memcpy(a,b,sizeof(b));
         memcpy(b,tmp,sizeof(tmp));
         swap(alen,blen);
    
          for(int i=0;i<blen;i++)
            T[i]=b[i];
          T[blen]='#';
         for(int i=0;i<alen;i++)
         T[blen+1+i]=a[i];
         tlen=alen+blen+1;
         T[tlen]=0;
         getNext();
         ans=Next[tlen];
         for(int i=0;i<alen;i++)
         res2[i]=a[i];
         for(int i=ans;i<blen;i++)
         res2[alen+i-ans]=b[i];
         len2=alen+blen-ans;
         res2[len2]=0;
            if(len1<len2)
            printf("%s
    ",res1);
            else if(len1>len2)
            printf("%s
    ",res2);
            else
            {
                if(strcmp(res1,res2)<=0)
                printf("%s
    ",res1);
                else
                printf("%s
    ",res2);
            }
        }
        return 0;
    }
  • 相关阅读:
    设计模式之抽象工厂模式
    MQ任意延时消息(三)基于服务端实现
    MQ任意延时消息(二)基于客户端实现
    MQ任意延时消息(一)实现原理概述
    sqlyog报错2058
    base标签的作用
    相对路径和绝对路径的解释
    自定义Tomcat部署目录
    常用正则表达式
    接口的结构定义
  • 原文地址:https://www.cnblogs.com/a249189046/p/7447354.html
Copyright © 2011-2022 走看看