KMP algorithm Template C

Source: Internet
Author: User
/** * NAME:KMP * time:2012-11-22 * String Quick Match */#include <stdio.h> #include <string.h>typedef struct seqstring{ char string[100];int length;} Seqstring;void GetNext (seqstring p,int next[]) {int i,j;next[0]=-1;//next[0] put -1i=0;//pointer to each character of the string J=-1;while (i< P.length) {//does not reach the end if (j==-1| | P.STRING[I]==P.STRING[J]) {//If it is the first character or encounters the same character i++;j++;next[i]=j;} ELSEJ=NEXT[J];} for (i=0;i<p.length;i++) {//output next[] value printf ("%d", Next[i]);}} int KMP (seqstring t,seqstring p,int next[]) {int I,j;i=j=0;while (i<t.length&&j<p.length) {if (j==-1| | T.string[i]==p.string[j]) {i++;j++;} ELSEJ=NEXT[J];} if (j==p.length) return i-p.length;else return-1;} int main () {seqstring t,p;int next[50];p rintf ("Please input string T:"), scanf ("%s", t.string); T.length=strlen (t.string );p rintf ("Please input string P:"); scanf ("%s", p.string);p. Length=strlen (p.string); GetNext (P,next);p rintf ("\n%d\n" , KMP (T,p,next));}

  

KMP algorithm Template C

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.