KMP algorithm: KMP pattern matching (string), kmp Algorithm

Source: Internet
Author: User

KMP algorithm: KMP pattern matching (string), kmp Algorithm

A-KMP mode matches A string) Crawling in process... Crawling failed Time Limit:1000 MS Memory Limit:131072KB 64bit IO Format:% Lld & % llu

Description

Locate the next value of the substring, store it in the next array, and output all

Input

Enter a string

Output

Output all next values

Sample Input

abaabcac

Sample Output

0 1 1 2 2 3 1 2


#include<iostream>#include<string>#include<cstring>using namespace std;int next[10005];char str[10005];int len;void getnext(char *str,int next[]){    int j,k;next[1]=0;j=1;k=0;while(j<=len)if((k==0)||(str[j]==str[k])){++j;++k;next[j]=k;}elsek=next[k];}int main(){char s[1005];cin>>s;len =strlen(s);int j,k;for(j=1,k=0;k<len;j++,k++){str[j]=s[k];}int i;getnext(str,next);for(i=1;i<len;i++)        cout<<next[i]<<" ";cout<<next[len]<<endl;return 0;}





KMP pattern matching algorithm

I answered a similar question and explained the principle.
Zhidao.baidu.com/question/329386416.html
If you only need code
A simple code
# Include <stdio. h>
# Include <string. h>
Int index_KMP (char * s, char * t, int pos );
Void get_next (char * t, int *);

Char s [10] = "abcacbcba ";
Char t [4] = "bca ";
Int next [4];
Int pos = 0;

Int main ()
{
Int n;
Get_next (t, next );
N = index_KMP (s, t, pos );
Printf ("% d", n );
Return 0;
}

Int index_KMP (char * s, char * t, int pos)
{
Int I = pos, j = 1;
While (I <= (int) strlen (s) & j <= (int) strlen (t ))
{
If (j = 0 | s [I] = t [J-1])
{
I ++;
J ++;
}
Else j = next [j];
}
If (j> (int) strlen (t ))
Return I-strlen (t) + 1;
Else
Return 0;
}

Void get_next (char * t, int * next)
{

Int I = 1, j = 0;
Next [0] = next [1] = 0;
While (I <(int) strlen (t ))
{
If (j = 0 | t [I] = t [j])
{
I ++;
J ++;
Next [I] = j;
}
Else j = next [j];
}

}

Data Structure string mode matching problem KMP Algorithm

Your program's own ideas are correct, but the errors are as follows:
1. There are strings S and T in the program. You use S [0] to represent the length of the string, but S is a string. Is S [0] A length?
2. in the main function, the S and T you input both use gets (S) or gets (T). Then they all start with 0, and you should process them, make it start with 1 (gets (& S [1]); then S [0] = strlen (& S [1]) + '0 '; when S [0] is used as the length, it can be changed from a character to a number ).

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.