A + B for you again
A + B for you again
Time Limit : 5000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 18 Accepted Submission(s) : 10
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
asdf ghjk
Sample Output
asdfg
asdfghjk
asdfghjk
Author
Wang Ye
Source
2008杭电集训队选拔赛——热身赛
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 int Len_S,Len_t; 5 int next[200005]; 6 char S[2000050],t[2000050]; 7 void getNext(char p[]) 8 { 9 int i=1,j=0; 10 next[j]=-1; 11 while(i<Len_t) 12 { 13 if(j==-1||p[i]==p[j]) 14 { 15 i++;j++; 16 next[i]=j; 17 } 18 else 19 j=next[j]; 20 } 21 } 22 int KMPMatch(char s[],char p[]) 23 { 24 int i=0,j=0; 25 Len_S=strlen(s);Len_t=strlen(p); 26 getNext(p); 27 while(i<Len_S&&j<Len_t) 28 { 29 if(j==-1||s[i]==p[j]) 30 { 31 i++;j++; 32 } 33 else 34 j=next[j]; 35 36 } 37 if(i==Len_S) 38 return j; 39 return 0; 40 } 41 42 int main() 43 { 44 int Len1,Len2; 45 while(scanf("%s%s",S,t)!=EOF) 46 { 47 Len1=KMPMatch(S,t); 48 Len2=KMPMatch(t,S); 49 if(Len1==Len2) 50 { 51 if(strcmp(S,t)>0) 52 printf("%s%s\n",t,S+Len1); 53 else 54 printf("%s%s\n",S,t+Len1); 55 } 56 else if(Len1>Len2) 57 printf("%s%s\n",S,t+Len1); 58 else 59 printf("%s%s\n",t,S+Len2); 60 } 61 return 0; 62 }
转载请备注:
**************************************
* 作者: Wurq
* 博客: https://www.cnblogs.com/Wurq/
* Gitee: https://gitee.com/wurq
**************************************
**************************************
* 作者: Wurq
* 博客: https://www.cnblogs.com/Wurq/
* Gitee: https://gitee.com/wurq
**************************************