ZOJ3228---Searching the String (AC automaton)

Source: Internet
Author: User

Little Jay really hates to deal with string. But Moondy likes it very much, and she's so mischievous the she often gives Jay some dull problems related to string. And one day, Moondy gave Jay another problem, poor Jay finally broke out and cried, ' Who can help me? I ' ll BG him! “

So what's the problem this time?

First, Moondy gave Jay a very long string a. Then she gave him a sequence of very short substrings, and asked him to the find how many times each substring appeared in Str ing A. What's more, she would denote whether or isn't founded appearances of this substring is allowed to overlap.

At first, Jay just read string A from the begin to end to search all appearances the given substring. But he soon felt exhausted and couldn ' t go on any more, so he gave up and broke off this time.

I know you ' re a good guy and would help with Jay even without BG, won ' t?

Input

Input consists of multiple cases (<=) and terminates with end of file.

For each case, the first line contains string A (length <= 10^5). The second line contains an integer n (n <= 10^5), which denotes the number of queries. The next N lines, each with an integer type and a string a (length <= 6), type = 0 denotes substring A are allowed to Overlap and type = 1 denotes not. Note that all input characters is lowercase.

There is a blank line between the consecutive cases.

Output

For each case, output of the case number first (based on 1, see Samples).

Then for each query, output an integer in a single line denoting the maximum times you can find the substring under Certai N rules.

Output an empty line after each case.

Sample Input

Ab
2
0 AB
1 AB

Abababac
2
0 ABA
1 ABA

Abcdefghijklmnopqrstuvwxyz
3
0 ABC
1 def
1 JMN

Sample Output

Case 1
1
1

Case 2
3
2

Case 3
1
1
0

Hint

In case 2,you can find the first substring starting in position (indexed from 0) 0,2,4, since they ' re allowed to overlap. The second substring starts in position 0 and 4, since they ' re isn't allowed to overlap.

For C + + users, kindly use scanf to avoid TLE for huge inputs.
Author:li, Jie
Source:zoj Monthly, July 2009

For those can overlap the string, is the template, for non-overlapping, record the node on the last access to the position can be, beginning with the vector record each node end of the string ID, matching the time to traverse and then time out, and then the end of each string to record the node is over

/************************************************************************* > File Name:zoj3228.cpp > Auth Or:alex > Mail: [email protected] > Created time:2015 March 04 Wednesday 16:44 33 seconds ******************************** ****************************************/#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace STD;Const DoublePI =ACOs(-1);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;Const intMax_node =500100;Const intChild_num = -;intTag[max_node];intnum[max_node][2];intpos[100100];intty[100100];structac_automation{intNext[max_node][child_num];intFail[max_node];intPre[max_node];intSize[max_node];intRoot, L;intNewNode () { for(inti =0; i < Child_num; ++i) {Next[l][i] =-1; } ++l;returnL1; }voidInit () {L =0;memset(Pre,-1,sizeof(pre));memset(Num,0,sizeof(num));memset(Size,0,sizeof(size));        root = NewNode (); Size[root] =0; }intBuild_trie (CharBuf[]) {intnow = root;intLen =strlen(BUF); for(inti =0; i < Len; ++i) {if(Next[now][buf[i]-' A '] == -1) {Next[now][buf[i]-' A '] = NewNode (); } now = Next[now][buf[i]-' A ']; Size[now] = i +1; }returnNow }voidBuild_ac () { Queue <int>Qu Fail[root] = root; for(inti =0; i < Child_num; ++i) {if(Next[root][i] = =-1) {Next[root][i] = root; }Else{Fail[next[root][i]] = root;            Qu.push (Next[root][i]); }        } while(!qu.empty ()) {intnow = Qu.front (); Qu.pop (); for(inti =0; i < Child_num; ++i) {if(Next[now][i] = =-1) {Next[now][i] = Next[fail[now]][i]; }Else{Fail[next[now][i]] = next[fail[now]][i];                Qu.push (Next[now][i]); }            }        }    }voidMatch (CharBuf[]) {intLen =strlen(BUF);intnow = root; for(inti =0; i < Len; ++i) {now = Next[now][buf[i]-' A '];intTMP = Now; while(tmp! = root) {++num[tmp][0];if(I-pre[tmp] >= size[tmp]) {++num[tmp][1];                PRE[TMP] = i;            } TMP = fail[tmp]; }}}}ac;Charbuf[100010];Charstr[Ten];intMain () {intIcase =1; while(~scanf('%s ', buf)) {ac.init ();intNscanf("%d", &n); for(inti =1; I <= N; ++i) {scanf("%d%s", &ty[i], str); Pos[i] = AC.        Build_trie (str); } AC.        Build_ac (); AC. Match (BUF);printf("Case%d\n", icase++); for(inti =1; I <= N; ++i) {printf("%d\n", Num[pos[i]][ty[i]]); }printf("\ n"); }return 0;}

ZOJ3228---Searching the String (AC automaton)

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.