hdu1867A + B for you again

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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>

using namespace std;

char s1[110000],s2[110000],s3[210000],s4[210000];
int nextt[110000];
int match[110000];


int main()
{
    int i,j,k;
    while(scanf("%s%s",s1+1,s2+1)!=EOF)
    {
        int l1 = strlen(s1+1),l2 = strlen(s2+1);
        nextt[1] = 0;
        for(i = 2;i<=l2;i++)
        {
            int t = nextt[i-1];
            while(t&&s2[i]!=s2[t+1]) t = nextt[t];
            if(s2[i] == s2[t+1]) t++;
            nextt[i] = t;
        }
        match[0] = 0;
        for(i = 1;i<=l1;i++)
        {
            int t = match[i-1];
            while(t&&s1[i]!=s2[t+1]) t = nextt[t];
            if(s1[i] == s2[t+1]) t++;
            match[i] = t;
        }
        int t1 = match[l1];
        for(i = 1;i<=l1;i++)
            s3[i] = s1[i];
        for(i = l1+1;i<=l1+l2-t1+1;i++)
            s3[i] = s2[i-l1+t1];
        nextt[1] = 0;
        for(i = 2;i<=l1;i++)
        {
            int t = nextt[i-1];
            while(t&&s1[i]!=s1[t+1]) t = nextt[t];
            if(s1[i] == s1[t+1]) t++;
            nextt[i] = t;
        }
        match[0] = 0;
        for(i = 1;i<=l2;i++)
        {
            int t = match[i-1];
            while(t&&s2[i]!=s1[t+1]) t = nextt[t];
            if(s2[i] == s1[t+1]) t++;
            match[i] = t;
        }
        int t2 = match[l2];
        for(i = 1;i<=l2;i++)
            s4[i] = s2[i];
        for(i = l2+1;i<=l2+l1-t2+1;i++)
            s4[i] = s1[i-l2+t2];
        if(t1>t2) printf("%s",s3+1);
        else if(t2>t1) printf("%s",s4+1);
        else
        {
            int l = l1+l2-t2;
            bool bb = 0;
            for(i = 1;i<=l;i++)
            {
                if(s3[i]<s4[i])
                {
                    printf("%s",s3+1);
                    bb = 1;
                    break;
                }
                else if(s3[i]>s4[i])
                {
                    printf("%s",s4+1);
                    bb = 1;
                    break;
                }
            }
            if(!bb) printf("%s",s3+1);
        }
        puts("");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wos1239/p/4398558.html