HDU1867 A + B for you again(KMP)

Posted hinata_hajime

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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\n",res1);
        else if(len1>len2)
        printf("%s\n",res2);
        else
        {
            if(strcmp(res1,res2)<=0)
            printf("%s\n",res1);
            else
            printf("%s\n",res2);
        }
    }
    return 0;
}

 

以上是关于HDU1867 A + B for you again(KMP)的主要内容,如果未能解决你的问题,请参考以下文章

hdu 1867 A + B for you again

hdu - 1867 - A + B for you again

HDU1867 A + B for you again(KMP)

HDU 1867 A + B for you again ----KMP

HDU 4027 Can you answer these queries?(线段树)

A + B for you again