Ytu 2296:KMP Pattern matching Two (string) __ytu

Source: Internet
Author: User
2296:KMP Pattern matching Two (string)Time limit: 1 Sec Memory limit: 128 MB
Submitted: 29 Settlement: 17
Topic Description

Enter a main string and a substring, match with KMP, ask for a few matches to succeed, if not successful, then output 0 input

Enter a main string and a substring output

Matching number of trip examples input

Ababcabcacbab
ABCAC
Sample Output
3
lost in the valley of the birds, flying alone in this huge world, but do not know where to fly to ...
#include <stdio.h> #include <string.h> #define SIZEMAX typedef struct {char Data[sizemax];
int length;
}sqstring;
    void GetNext (sqstring s,int next[]) {int j=0,k=-1;
    Next[0]=-1; while (j<s.length-1) {if (k==-1| |
        S.DATA[J]==S.DATA[K]) j++,k++,next[j]=k;
    else K=next[k];
    } void Strassign (Sqstring &s,char cstr[]) {int i;
    For (i=0 i< (int) strlen (CStr); i++) s.data[i]=cstr[i];
S.length=i;
    int KMP (sqstring s,sqstring t,int next[]) {int i=0,j=0,ci=1; while (i<s.length&&j<t.length) {if (j==-1| |
        S.DATA[I]==T.DATA[J]//pattern string and main string corresponding character match succeeded {i++;j++;            else j=next[j],ci++;
    The pattern string pointer returns to the corresponding next, matches the number +1} if (j>=t.length) return CI;
else return 0;
    int main () {sqstring s,t;
    int next[sizemax]={-1};
    Char C[sizemax],d[sizemax];
    Gets (c);
    Gets (d);             Strassign (S,C); Set up a string strassign(T,D);            GetNext (T,next);       Get Next value int ci=kmp (s,t,next);
    Get a match to the location printf ("%d\n", CI);
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.