HDU1867 A + B for your again KMP application

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.