HDU 1686 Oulipo The maximum number of small strings (KMP) to be matched in a large string

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=1686

Oulipo

Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 6098 Accepted Submission (s): 2448


Problem DescriptionThe French author Georges perec (1936–1982) once wrote a book, La disparition, without the letter ' E '. He was a member of the Oulipo group. A quote from the book:

Tout avait Pair normal, mais Tout s ' affirmait faux. Tout avait Fair normal, d ' Abord, puis surgissait l ' inhumain, l ' affolant. Il aurait voulu savoir oùs ' articulait l ' association qui l ' unissait au roman:stir son tapis, Assaillantàtout instant s On imagination, l ' intuition d ' un tabou, la vision d ' un mal obscur, d ' un quoi vacant, d ' UN non-dit:la vision, l ' avision D ' Un oubli commandant tout, oùs ' abolissait la raison:tout avait l ' air normal mais ...

Perec would probably has scored high (or rather, low) in the following contest. People is asked to write a perhaps even meaningful text on some subject with as few occurrences of a given "word" as poss Ible. Our tasks are to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the Competit Ors. These competitors often write very long texts with nonsense meaning; A sequence of 500,000 consecutive ' T ' is not unusual. And they never use spaces.

So we want to quickly find out what often a word, i.e., a given string, occurs in a text. More Formally:given the alphabet {' A ', ' B ', ' C ', ..., ' Z '} and both finite strings over that alphabet, a word W and a text T , count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. occurrences may overlap.

Inputthe first line of the input file contains a single number:the number of test cases to follow. Each test case has the following format:

One line with the word W, a string over {' A ', ' B ', ' C ', ..., ' Z '}, with 1≤| w| ≤10,000 (Here | w| Denotes the length of the string W).
One line with the text T, a string over {' A ', ' B ', ' C ', ..., ' Z '}, with | w| ≤| T| ≤1,000,000.

Outputfor every test case in the input file, the output should contain a single number, on a single line:the number of OC Currences of the word W in the text T.

Sample Input3bapcbapcazaazazazaverdiaverdxivyerdian
1#include <iostream>2#include <stdlib.h>3#include <stdio.h>4#include <cstring>5 using namespacestd;6 intn,m,nxt[10005],kk,t;7 Charb[10005],a[1000005];8 ///This topic adds multiple matches on the base KMP. 9 ///It means that after we've matched the string, we're going to jump to the right place and continue looking forTen ///continue to use KMP's ideas. Some locations have already been matched, so don't match them.  One ///xxxxxxxabbaab*xxxxxx A ///Abbaaba - //our position after jumping Abbaaba and jumping position is related to next array - voidbuildnxt () the { -    intj,k; -m=strlen (b); -nxt[0]=-1; +j=0; k=-1; -      while(j<m) +     { A         if((k==-1)|| b[j]==B[k]) at         { -J + +; -k++; -nxt[j]=K; -         } -         Elsek=Nxt[k]; in     } - } to intKMP () + { -     intk=0, l=0, cou=0; then=strlen (a); *     intANS=M,KK=NXT[M];///ans in string subscript, and starting distance ans+1 $      while(1)Panax Notoginseng     { -         if(kk!=0&&kk!=-1) {ans=kk;kk=Nxt[ans];} the         Else  Break; +}///to find the smallest jumping point, return from the next end to find the first non-negative value.  A      while(k<N) the     { +         if((l==-1)|| a[k]==B[l]) -         { $k++; $l++; -         } -         ElseL=Nxt[l]; the         if(l==m) -         {Wuyicou++; the             if(kk==0)Continue;///If the end of the next array is 0, there is no repetition in the matching substring in the main string.  -             ///In other words, there is no point in the matching main string that can jump.  Wu             if(k==n-1) Break;///if K is already at the end of the main string, you cannot have a string that continues to match.  -K=k-l+ans;///K-L (starting point) +ans AboutL=0; $         } -     } -     returncou; - } A intMain () + { thescanf"%d",&t); - GetChar (); $      while(t--) the     { the gets (b); the gets (a); thememset (NXT,0,sizeof(NXT)); - buildnxt (); inprintf"%d\n", KMP ()); the     } the     return 0; About}
View Code

HDU 1686 Oulipo The maximum number of small strings (KMP) to be matched in a large string

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.