HDU 1618 Oulipo KMP Problem

Source: Internet
Author: User

HDU 1618 Oulipo KMP Problem

Returns two strings to find the frequency at which one string appears in another string.

In the past, there was another trap in kmp. The following notes show that if the subscript is not well performed, there will be a certain probability of timeout and errors, depending on the specific string.

Just modify it. The kmp speed is very fast.


# Include
 
  
# Include
  
   
Const int MAX_TXT = 1000001; const int MAX_WORD = 10001; int gWlen, gTlen; char word [MAX_WORD]; int next [MAX_WORD]; char txt [MAX_TXT]; void getNext () {memset (next, 0, sizeof (int) * gWlen); for (int I = 1, j = 0; I <gWlen ;) {if (word [I] = word [j]) next [I ++] = ++ j; else if (j> 0) j = next [J-1]; else I ++ }}int kmpSearch () {int I = 0, j = 0, ans = 0; for (; gWlen-j <= gTlen-I ;) {if (word [j] = txt [I] ) {J ++; I ++; if (j = gWlen) {ans ++; j = next [J-1];} // else I ++; I ++ times out here, which is a probability of timeout. Note that! } Else if (j> 0) j = next [J-1]; else I ++;} return ans;} int main () {int T; scanf ("% d ", & T); getchar (); while (T --) {gets (word); gWlen = strlen (word); gets (txt); gTlen = strlen (txt ); getNext (); printf ("% d \ n", kmpSearch ();} 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.