ZOJ 3228 Searching the String

Searching the String

Time Limit: 7000ms
Memory Limit: 129872KB
This problem will be judged on ZJU. Original ID: 3228
64-bit integer IO format: %lld      Java class name: Main

Little jay really hates to deal with string. But moondy likes it very much, and she's so mischievous that 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 is 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 find how many times each substring appeared in string A. What's more, she would denote whether or not founded appearances of this substring are allowed to overlap.

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

I know you're a good guy and will help with jay even without bg, won't you?

Input

Input consists of multiple cases( <= 20 ) 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 are lowercase.

There is a blank line between two consecutive cases.

Output

For each case, output 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 certain 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 not allowed to overlap.

For C++ users, kindly use scanf to avoid TLE for huge inputs.

 

Source

Author

LI, Jie
 
解题:Trie图或者AC自动机
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 1000010;
 4 struct Trie{
 5     int ch[maxn][26],fail[maxn],pre[maxn],cnt[maxn][2];
 6     int len[maxn],tot;
 7     int newnode(){
 8         memset(ch[tot],0,sizeof ch[tot]);
 9         pre[tot] = cnt[tot][0] = cnt[tot][1] = 0;
10         fail[tot] = len[tot] = 0;
11         return tot++;
12     }
13     void init(){
14         tot = 0;
15         newnode();
16     }
17     int insert(char *str,int root = 0){
18         for(int i = 0; str[i]; ++i){
19             int &x = ch[root][str[i]-'a'];
20             if(!x) x = newnode();
21             root = x;
22             len[root] = i + 1;
23         }
24         return root;
25     }
26     void build(int root = 0){
27         queue<int>q;
28         for(int i = 0; i < 26; ++i)
29             if(ch[root][i]) q.push(ch[root][i]);
30         while(!q.empty()){
31             root = q.front();
32             q.pop();
33             for(int i = 0; i < 26; ++i){
34                 int &x = ch[root][i];
35                 if(x){
36                     fail[x] = ch[fail[root]][i];
37                     q.push(x);
38                 }else x = ch[fail[root]][i];
39             }
40         }
41     }
42     void query(char *str,int root = 0){
43         for(int i = 0; str[i]; ++i){
44             int x = root = ch[root][str[i]-'a'];
45             while(x){
46                 ++cnt[x][0];
47                 if(i - pre[x] + 1 >= len[x]){
48                     pre[x] = i + 1;
49                     ++cnt[x][1];
50                 }
51                 x = fail[x];
52             }
53         }
54     }
55 }ac;
56 char str[100010],ss[100];
57 int pos[100010],ty[100010],n,cs = 1;
58 int main(){
59     while(~scanf("%s",str)){
60         ac.init();
61         scanf("%d",&n);
62         for(int i = 0; i < n; ++i){
63             scanf("%d%s",ty + i,ss);
64             pos[i] = ac.insert(ss);
65         }
66         ac.build();
67         ac.query(str);
68         printf("Case %d\n",cs++);
69         for(int i = 0; i < n; ++i)
70             printf("%d\n",ac.cnt[pos[i]][ty[i]]);
71         putchar('\n');
72     }
73     return 0;
74 }
View Code

 

posted @ 2015-11-04 20:53  狂徒归来  阅读(205)  评论(0编辑  收藏  举报