ZOJ 3228 searching the String (AC automaton)

Source: Internet
Author: User

Searching the String Time limit: 7 Seconds Memory Limit: 129872 KB

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 is 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

Ab20 ab1 ababababac20 aba1 abaabcdefghijklmnopqrstuvwxyz30 ABC1 def1 JMN

Sample Output

Case 111Case 232Case 3110

/*zoj 3228 Searching the string (AC automaton) gives you several substrings, and then queries the string for how many times they appear. But 0 means you can repeat, and 1 means you can't repeat. At the beginning of the thinking is to build two and then separate query. But the discovery can be solved at a time                     TATABABABAC20 Aba1 ABA As far as this group of data is concerned. Built: Root/①a /②b/③a for the parts that can be duplicated, look directly at the line. Because the leaf node of a nex[a][b] is its Father B node//can refer to the "floating calf" summary, mainly the fail pointer understanding so lead to a①-> b②-> a③-> b②-> A③ And then went to the leaf node A. And it's only possible to go to the end of a string. +1 and then at the time of the completion of a substring (through the ED Judgment) to determine its most recent occurrence of the position of two is greater than the length of the substring can be hhh-2016-04-26 20:19:35*/ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <map > #include <vector> #include <queue> #include <functional> #include <algorithm>using namespace std; #define Lson (i<<1) #define Rson ((i<<1) | |) typedef unsigned LONG long ll;typedef unsigned int u l;const int mod = 20090717;const int INF = 0x3f3f3f3f;const int N = 100005*6;int Pos[100005];char str[100005];struct Matri x{    int Len;    int ma[111][111];    Matrix () {};    Matrix (int L) {len = L;    }};struct tire{int nex[n][26],fail[n],ed[n];    int dep[n];    int tan[n][2];    int root,l;        int NewNode () {for (int i = 0; i <; i++) nex[l][i] = 1;        ed[l++] = 0;    return L-1;        } void Ini () {L = 0;        root = NewNode ();    Dep[root] = 0;        } int cal (char ch) {if (ch = = ' A ') return 0;        else if (ch = = ' C ') return 1;        else if (ch = = ' G ') return 2;    else if (ch = = ' T ') return 3;        } int Inser (char buf[]) {int len = strlen (BUF);        int now = root;            for (int i = 0; i < len; i++) {int ta = buf[i]-' a ';                if (nex[now][ta] = =-1) {Nex[now][ta] = NewNode ();            Dep[nex[now][ta]] = i+1;        } now = Nex[now][ta];        } Ed[now] + +; return now;    } void Build () {Queue<int >q;        Fail[root] = root;            for (int i = 0; i < i++) if (nex[root][i] = = 1) nex[root][i] = root;                else {Fail[nex[root][i]] = root;            Q.push (Nex[root][i]);            } while (!q.empty ()) {Int. now = Q.front ();            Q.pop ();//if (Ed[fail[now]])//Ed[now] = Ed[fail[now]]; for (int i = 0; i < i++) {if (nex[now][i] = = 1) nex[now][i] = Nex[fail [Now]]                [i];                    else {Fail[nex[now][i]] = nex[fail[now]][i];                Q.push (Nex[now][i]);        }}}} Matrix To_mat () {Matrix mat (L);        memset (mat.ma,0,sizeof (mat.ma)); for (int i = 0, i < L; i++) {for (int j = 0; J < 4; J + +) {if (!ed[nEX[I][J]]) Mat.ma[i][nex[i][j]] + +;    }} return mat;    } int last[n];        void query (char buf[]) {int len = strlen (BUF);        int cur = root;        memset (tan,0,sizeof (tan));        Memset (Last,-1,sizeof (last));            for (int i = 0;i < len;i++) {int ta = buf[i]-' a ';            cur = Nex[cur][ta];            int t = cur;                    while (t! = root) {if (Ed[t]) {tan[t][0]++;                        if (I-last[t] >= dep[t]) {last[t] = i;                    TAN[T][1] + +;            }} t = Fail[t];    }} return; }};    Tire Ac;char s[10];int ty[100004];int main () {int cas = 1;    int n;        while (scanf ("%s", str)! = EOF) {scanf ("%d", &n);        Ac.ini ();        printf ("Case%d\n", cas++);      for (int i = 0; i < n; i++)  {scanf ("%d%s", &ty[i],s);        Pos[i] = Ac.inser (s);        } ac.build (); Ac.query (str);//for (int i = 0;i < n;i++)//cout << pos[i] << "";//cout <<e        Ndl        for (int i = 0;i < n;i++) {printf ("%d\n", Ac.tan[pos[i]][ty[i]]);    } printf ("\ n"); } return 0;}

  

ZOJ 3228 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.