KMP mode matching 2 (string) and kmp mode matching

Source: Internet
Author: User

KMP mode matching 2 (string) and kmp mode matching
For the original article, visit my blog:Xiaoshig.sinaapp.com
KMP mode matching 2 (string)
Time Limit:1000 MSMemory Limit:131072KB64bit IO Format:% Lld & % lluSubmit Status

Description

Enter a primary string and a sub-string, and use KMP for matching. If the match fails, the output is 0.

Input

Enter a main string and a sub-string

Output

Number of matched shards

Sample Input

ababcabcacbababcac

Sample Output

3
#include<iostream>#include<cstring>using namespace std;int a[100000];char c[10000];char b[100000];int main(){int j,n,i,k;cin>>c;cin>>b;a[0]=-1;j=-1;i=0;while(b[i]!='\0'){if(j==-1||b[j]==b[i]){j++;i++;//if(b[i]!=b[j])a[i]=j;//else//{if(a[j]==-1)//a[i]=0;//else//a[i]=a[j];}}else j=a[j];}i=0;j=0;k=0;n=1;while(c[i]!='\0'){if(c[i]==b[j]){i++;j++;if(b[j]=='\0'){k=1;break;}}else if(a[j]==-1){i++;j=0;n++;}else {j=a[j];n++;}}if(k==0)cout<<-1<<endl;else cout<<n<<endl;return 0;}



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 ).

Next [j] of the data structure KMP pattern matching algorithm why is it written below?

Your understanding is a little biased. If you set the pattern string to string [I], calculate next [j], instead of adding one if the first one is equal, it depends on how many substrings are equal, when j = 4, the substring is only string [3] = string [1], so next [j] = 2, similarly, when j = 5, only string [4] = string [1] is available, so next [j] = 2. To use next [5] = 3, you must meet the conditions of string [3] = string [1] and string [4] = string [2.
Do you think the data structure of Tsinghua University is a bit unclear. The algorithm for finding the next array can be viewed several times.

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.