zoj3228 Searching the String AC自动机

来源:互联网 发布:苹果手机必须软件 编辑:程序博客网 时间:2024/06/03 04:44

Description
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.

0代表可以重复的字符串出现的次数,1代表不可以重复的字符串出现的次数

#include<cstdio>#include<cmath>#include<cstring>#include<iostream>#include<algorithm>#include<queue>#include<vector>#include<map>#include<stack>#pragma comment(linker,"/STACK:102400000,102400000")#define pi acos(-1.0)#define EPS 1e-6#define INF (1<<24)using namespace std;struct Trie{    int next[700005][26],fail[700005],deep[700005];    int root,L;    int newnode()    {        int i;        for(i=0;i<26;i++) next[L][i]=-1;        L++;        return L-1;    }    void init()    {        L=0;        root=newnode();        deep[root]=0;    }    int insert(char buf[])    {        int len=strlen(buf);        int now=root;        for(int i=0;i<len;i++)        {            if(next[now][buf[i]-'a']==-1)            {                next[now][buf[i]-'a']=newnode();                deep[next[now][buf[i]-'a']]=i+1;            }            now=next[now][buf[i]-'a'];        }        return now;    }    void build()    {        queue<int>Q;        fail[root]=root;        for(int i=0;i<26;i++)        {            if(next[root][i]==-1) next[root][i]=root;            else            {                fail[next[root][i]]=root;                Q.push(next[root][i]);            }        }        while(!Q.empty())        {            int now=Q.front();            Q.pop();            for(int i=0;i<26;i++)            {                if(next[now][i]==-1) next[now][i]=next[fail[now]][i];                else                {                    fail[next[now][i]]=next[fail[now]][i];                    Q.push(next[now][i]);                }            }        }    }    int cnt[2][700005];    int last[700005];    void query(char buf[])    {        int len=strlen(buf);        int now=root;        memset(cnt,0,sizeof(cnt));        memset(last,-1,sizeof(last));        for(int i=0;i<len;i++)        {            now=next[now][buf[i]-'a'];            int temp=now;            while(temp!=root)            {                cnt[0][temp]++;    //0可以重复情况+1                if(i-last[temp]>=deep[temp])                {                    last[temp]=i;                    cnt[1][temp]++;  //1不可重复情况+1                }                temp=fail[temp];            }        }    }};Trie ac;char str[100005];char buf[22];int typ[100005],pos[100005];int main(){    int n;    int T=0;    while(scanf("%s",str)==1)    {        T++;        printf("Case %d\n",T);        scanf("%d",&n);        ac.init();        for(int i=0;i<n;i++)        {            scanf("%d %s",&typ[i],buf);            pos[i]=ac.insert(buf);        }        ac.build();        ac.query(str);        for(int i=0;i<n;i++) printf("%d\n",ac.cnt[typ[i]][pos[i]]);        printf("\n");    }    return 0;}
0 0