Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3432 Accepted Submission(s): 869
1 #include <stdio.h> 2 #include <string.h> 3 #define maxn 100000 4 int next[maxn+1]; 5 char ps[maxn+1],pt[maxn+1]; 6 void get_next(char const * t,int *next,int const lent) 7 { 8 int i=0,j=-1; 9 memset(next,0,sizeof(next)); 10 next[0]=-1; 11 while(i<lent) 12 { 13 if(j==-1||t[i]==t[j]) 14 { 15 ++i; 16 ++j; 17 if(t[i]!=t[j]) 18 next[i]=j; 19 else 20 next[i]=next[j]; 21 22 } 23 else 24 j=next[j]; 25 } 26 } 27 28 int ext_kmp(char const * ps, char const *pt,int const lens,int const lent) 29 { 30 int i=lens-lent-1,j=-1; 31 get_next(pt,next,lent); 32 if(i<-1)i=-1; 33 while(i<lens) 34 { 35 if(j==-1||ps[i]==pt[j]) 36 { 37 ++i; 38 ++j; 39 } 40 else 41 j=next[j]; 42 } 43 return j; 44 } 45 46 int main() 47 { 48 int i,ansa,ansb; 49 int lens,lent; 50 while(scanf("%s%s",ps,pt)!=EOF) 51 { 52 lens=strlen(ps); 53 lent=strlen(pt); 54 ansa=ext_kmp(ps,pt,lens,lent); 55 ansb=ext_kmp(pt,ps,lent,lens); 56 if(ansa>ansb) 57 { 58 printf("%s",ps); 59 for(i=ansa;i<lent;i++) 60 printf("%c",pt[i]); 61 } 62 else if(ansa<ansb) 63 { 64 printf("%s",pt); 65 for(i=ansb;i<lens;i++) 66 printf("%c",ps[i]); 67 } 68 else 69 { 70 if(strcmp(ps,pt)<0) 71 { 72 printf("%s",ps); 73 for(i=ansa;i<lent;i++) 74 printf("%c",pt[i]); 75 } 76 else 77 { 78 printf("%s",pt); 79 for(i=ansb;i<lens;i++) 80 printf("%c",ps[i]); 81 } 82 } 83 putchar(10); 84 } 85 return 0; 86 }