ZOJ Problem Set - 3228 Searching the String AC自动机

来源:互联网 发布:淘宝宝贝橱窗推荐 编辑:程序博客网 时间:2024/05/22 06:21

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 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 stringA. 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 integerN (N <= 10^5 ), which denotes the number of queries. The next N lines, each with an integertype and a string a ( length <= 6 ),type = 0 denotes substringa 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

ab20 ab1 ababababac20 aba1 abaabcdefghijklmnopqrstuvwxyz30 abc1 def1 jmn

Sample Output

Case 111Case 232Case 3110

  给一个原始串,N个询问串,0代表询问串可以重叠,1不能重叠,输出询问串的出现次数。

  离线处理,可以重叠的就是最一般的AC自动机做法,对于不能重叠,用pos[u]记录这个节点上次出现在原始串中的位置(只对于标记节点),如果这次匹配的位置和上次匹配的位置大于这个串的长度(也就是节点u的深度,构造AC自动机的时候记录一下节点深度)。

  因为是离线处理,所以要复杂一点,用cnt[u][0]记录节点u可重叠出现的总次数,cnt[u][1]记录节点u不可重叠出现的总次数。最开始插入串的时候记下每个串对应的节点,最后一起输出。

#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<iostream>#include<queue>using namespace std;typedef unsigned long long ULL;const int MAXN=100010;const int MAXM=8;const int MAXL=100010;const int MAXNODE=MAXN*MAXM;const int LOGMAXN=50;const int INF=0x3f3f3f3f;const int SIGMA_SIZE=26;const int MOD=20090717;int T,N;int p[MAXN],type[MAXN];char P[MAXL];char str[MAXM];struct AC{    int ch[MAXNODE][SIGMA_SIZE];    int val[MAXNODE];    int f[MAXNODE];    int last[MAXNODE];    int pos[MAXNODE];    int type[MAXNODE];    int dep[MAXNODE];    int cnt[MAXNODE][2];    int sz;    void clear(){        memset(ch[0],0,sizeof(ch[0]));        val[0]=0;        sz=1;    }    int idx(char c){        return c-'a';    }    int insert(char* s,int v,int t){        int u=0;        for(int i=0;s[i];i++){            int c=idx(s[i]);            if(!ch[u][c]){                memset(ch[sz],0,sizeof(ch[sz]));                val[sz]=0;                cnt[sz][0]=cnt[sz][1]=0;                pos[sz]=-1;                type[sz]=-1;                dep[sz]=i+1;                ch[u][c]=sz++;            }            u=ch[u][c];        }        val[u]=v;        if(type[u]!=-1&&type[u]!=t) type[u]=2;        else type[u]=t;        return u;    }    void get_fail(){        queue<int> q;        f[0]=last[0]=0;        for(int c=0;c<SIGMA_SIZE;c++){            int u=ch[0][c];            if(u){                f[u]=last[u]=0;                q.push(u);            }        }        while(!q.empty()){            int r=q.front();            q.pop();            for(int c=0;c<SIGMA_SIZE;c++){                int u=ch[r][c];                if(!u){                    ch[r][c]=ch[f[r]][c];                    continue;                }                q.push(u);                f[u]=ch[f[r]][c];                last[u]=val[f[u]]?f[u]:last[f[u]];            }        }    }    void print(int j,int i){        if(!j) return;        if(type[j]==0||type[j]==2) cnt[j][0]++;        if(type[j]==1||type[j]==2){            if(pos[j]==-1||i-pos[j]>=dep[j]){                pos[j]=i;                cnt[j][1]++;            }        }        print(last[j],i);    }    void find(char* T){        int j=0;        for(int i=0;T[i];i++){            int c=idx(T[i]);            j=ch[j][c];            if(val[j]) print(j,i);            else print(last[j],i);        }    }}ac;int main(){    freopen("in.txt","r",stdin);    int cas=0;    while(scanf("%s",P)!=EOF){        scanf("%d",&N);        ac.clear();        for(int i=1;i<=N;i++){            scanf("%d%s",&type[i],str);            p[i]=ac.insert(str,1,type[i]);        }        ac.get_fail();        ac.find(P);        printf("Case %d\n",++cas);        for(int i=1;i<=N;i++) printf("%d\n",ac.cnt[p[i]][type[i]]);        puts("");    }    return 0;}


0 0
原创粉丝点击