Click to open link
Problem Descriptiongenerally 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.
Inputfor each case, there is the strings (the chars selected just form ' a ' to ' Z '), and each length of theirs won ' t exceed 10^5 and won ' t be empty.
Outputprint the ultimate string by the book.
Sample Input
ASDF SDFGASDF GHJK
Sample Output
Asdfgasdfghjk
Problem-Solving ideas: The topic is the string pattern matching, pay attention to some details problems on it. But Java writes is infinite super memory, today mood is not good, again hand over, it is no language
Import Java.util.*;class p1867{static int[] Next=new int[100005]; public static void Main (String args[]) {int n,m,i,len1,len2; String str1,str2; Scanner sc=new Scanner (system.in); while (Sc.hasnext ()) {str1=sc.next (); Str2=sc.next (); Len1=str1.length (); Len2=str2.length (); N=KMP (STR1,STR2); M=KMP (STR2,STR1); if (n==m) {if (Str1.compareto (str2) >0) {System.out.println (str2+str1.substring (N,LEN1)) ; }else{System.out.println (str1+str2.substring (n,len2)); }}else if (n>m) {System.out.println (str1+str2.substring (n,len2)); }else{System.out.println (str2+str1.substring (M,LEN1)); }}} public static void Set_next (String str) {int i=0,j=-1; Next[0]=-1; int Len=str.length (); while (I<len) { if (j==-1| | Str.charat (i) ==str.charat (j)) {i++; j + +; Next[i]=j;///system.out.print (next[i]+ ""); }else{J=next[j]; }}//system.out.println (); } public static int kmp (String str1,string str2) {int i=0,j=0; int Len1=str1.length (), len2=str2.length (); Set_next (STR2); while (I<LEN1) {//system.out.print (j+ ""); if (j==-1| | (J<len2&&str1.charat (i) ==str2.charat (j))) {i++; J++;//system.out.print ("* *"); }else{J=next[j]; }//system.out.print (j+ "*"); }//system.out.println (); if (I==LEN1) {return J; } return 0; }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
hdu1867 (A + B for you again) Hangzhou electric Java A Real Pit