Problem Description
Generally speaking, there is a lot of problems about strings processing. Now you encounter another such problem. If you get both strings, such as "asdf" and "SDFG", the result of the addition between them is ' ASDFG ', for ' SDF ' is the TA Il substring of "asdf" and the head substring of the "SDFG". However, the result comes as "ASDFGHJK", when you had to add "asdf" and "GHJK" and guarantee the shortest string first, t Hen the minimum lexicographic second, the same rules for other additions.
Input
For each case, there is strings (the chars selected just form ' a ' to ' Z ') for your, and each length of theirs ' t ex Ceed 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
Thinking of solving problems
KMP the two strings separately to get the same prefix length. Then judging by the length of how to output
#include <cstdio>#include <cstring>ConstintMAXN =100010;int Next[Maxn];char S1[MAXN],S2[MAXN];intLen1,len2;void Get_next (Char*a){intLen = strlen (a);intK =-1;intj =0;Next[j] = k; while(A[j]! =' + ') {if(k = =-1|| A[J] = = A[k]) {k++;j++;Next[j] = k; }ElseK =Next[K]; }}intKMP (Char*a, Char*b){//aMain string B-mode string get_next (b);inti =0, j =0; while(A[i]! =' + ') {if(j = =-1|| A[i] = = B[j]) {i++;j++; }Elsej =Next[j]; }returnJ;}intMain () { while(SCANF ('%s'%s ', s1,s2)! = EOF) {len1 = strlen (S1); Len2 = strlen (s2);intSTR1 = KMP (S1,S2);intstr2 = KMP (S2,S1);if(str1 = = str2) {if(strcmp (S1,S2) <0)printf('%s%s\ n ', S1,S2+STR1);Else printf('%s%s\ n ', S2,S1+STR1); }Else if(Str1 < STR2) {printf('%s%s\ n ', S2,S1+STR2); }Else printf('%s%s\ n ', S1,S2+STR1); }return 0;}
HDU1867 A + B for your again KMP application