HDU 1867 A + B for you again

Source: Internet
Author: User
Tags printf strlen
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; }


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.