Number of hdu1686 calculation mode string matching times

Source: Internet
Author: User

Question:

Enter a t to indicate that there are T groups of test data;

Each group of test data includes a string W, T, and T with a length greater than W less than 1000000, W with a length less than 10000, and the number of successful matching times W to T is calculated;

 

It is obvious that the KMP algorithm is used to solve this problem. Otherwise, it is easy to time out. However, when using the KMP algorithm, pay attention to the fact that after I obtain the matched position for the first time, I cyclically enter the KMP algorithm, it starts from the previous matched position, but times out. After I asked the senior student, I knew that there was no need to loop into KMP and it could be done once directly. When each pattern string matches to the end, it would count + 1, then, based on next [m], re-obtain J and continue matching with the original string until the original string ends.

 

The Code is as follows:

 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 using namespace std; 6  7 char b[10100]; 8 char a[1000100]; 9 int next[10100];10 int n,m,count;11 12 void getNext(char *b)13 {14     int j;15     next[0]=0,next[1]=0;16     for(int i=1;i<m;i++){17         j=next[i];18         while(j&&b[j]!=b[i]) j=next[j];19         if(b[j]==b[i]) next[i+1]=j+1;20         else next[i+1]=0;21     }22 }23 24 void kmp(char *a,char *b,int *next)25 {26     int j=0;27     for(int i=0;i<n;i++){28 29         while(j&&a[i]!=b[j]) j=next[j];30         if(a[i]==b[j]) j++;31         if(j==m) count++,j=next[j];32     }33 }34 35 int main()36 {37     int T;38     cin>>T;39     while(T--){40         count=0;41         string W,T;42         cin>>W>>T;43         n=T.size();44         m=W.size();45         for(int i=0;i<n;i++){46             a[i]=T.at(i);47         }48         for(int i=0;i<m;i++){49             b[i]=W.at(i);50         }51 52         getNext(b);53         kmp(a,b,next);54         cout<<count<<endl;55     }56     return 0;57 }

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.