HDU2203 affinity string "KMP"

Source: Internet
Author: User

Topic Links:

http://acm.hdu.edu.cn/showproblem.php?pid=2203


Main topic:

The definition of a given affinity string: given two strings S1 and S2, if the S2 can be included in the S1 by S1 cycle Shift,

Then we'll say that S2 is the affinity string of S1. Now give you two strings S1 and S2 to determine if S2 is S1 affinity string.


Ideas:

Determine if the string length of the S2 is less than or equal to the length of S1, because if S2 string length is longer than S1, S2 is not

may be the affinity string of S1. Then in the S1 string behind in the street S1 the string, to S1 and S2 KMP algorithm, see S1 string

Whether the string containing S2, if included, then S2 is s1 affinity string, otherwise it is not affinity string.


AC Code:

# include<stdio.h># Include<string.h>char a[200010],b[100010];int next[100010];void getnext (char *pat) {    int len_pat = strlen (PAT);    int i = 0;    Int j =-1;    Next[0] =-1;            while (I < Len_pat) {if (j = =-1 | | pat[i] = = Pat[j]) {i++;            j + +;        Next[i] = j;    } else J = next[j];    }}int KMP (Char *str, char *pat) {int i = 0;    int j = 0;    int len_str = strlen (str);    int len_pat = strlen (PAT);    GetNext (PAT);            while (I < LEN_STR) {if (j = =-1 | | str[i] = = Pat[j]) {i++;        j + +;        } else J = next[j];    if (J >= Len_pat) return 1; } return 0;}    int main () {int len_a,len_b,i;        while (~SCANF ("%s%s", A, B)) {len_a = strlen (a);        Len_b = strlen (b);            if (Len_a < Len_b) {printf ("no\n");        Continue } for (i = 0; I <len_a; i++) a[i+leN_A] = A[i];        A[i+len_a] = ' + '; if (!        KMP (A, B)) printf ("no\n");    else printf ("yes\n"); } return 0;}


HDU2203 affinity string "KMP"

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.