In the beginning WA a lot of, and finally found himself and before the other KMP topic confused.
The substring cannot appear in the sentence.
#include <stdio.h> #include <string.h> #define N 1000002 #define M 1000002 Char a[m], b[n];
int next[n];
void GetNext (char s[], int len) {int I, J; i = 0;
j =-1;
Next[0] =-1;
while (I < len) {if (j = =-1 | | s[i] = = S[j]) ++i, ++j, next[i] = j;
else J = Next[j];
}} int KMP (const char a[], const char b[], int pos, int next[], int la, int lb) {int I, J;
i = pos, j = 0;
while (I < LA) {if (j = =-1 | | a[i] = = B[j]) ++i, ++j;
else J = Next[j];
if (J==lb&&i<la)//Because cannot be in the middle of the main string, so the main string does not run out, can not exit {j=0;
}} return J;
} int main () {int numa,numb,lena,lenb;
while (scanf ("%s%s", A, b)!=eof)//Note that the OLE {Lena=strlen (a) is always preceded by a null call;
Lenb=strlen (b);
GetNext (b, LenB);
NUMB=KMP (A, B, 0,next, LENA,LENB);
GetNext (A, Lena);
NUMA=KMP (b, A, 0,next, Lenb,lena); if (numa>numb) {printf("%s%s\n", B,a+numa);
} else if (numa<numb) {printf ("%s%s\n", a,b+numb);
} else//before comparing the integrated dictionary sequence I didn't think of the dictionary order {if (strcmp (b) <=0) printf ("%s%s\n", a,b+numb) that only considered a A/b original string;
else printf ("%s%s\n", B,a+numa);
}} return 0; }