zoj3228(AC自动机进阶)

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 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
这个题就比较难了。想了好久,因为它是分两种情况,即可重叠和不可重叠,可重叠的很简单了。正常的AC自动机就是搜索完回溯的,不可重叠我们用到的办法是,上次串的位置,只要加上串的长度小于当前位置,即可计算。而且,注意和标准的AC自动机模板不同,比如第一组数据,正常建树的时候第二个ab会覆盖第一个ab,所以在结构体里直接加入结果数组vis1,2,然后处理主串的时候直接更改vis值,而不是像其他一样新建ans数组。
#include 
#include 
#include 
#include 
#define mem(a) memset((a),0,sizeof(a))
using namespace std;
const int N=26;
struct node
{
    int num;
    node *fail;
    node *next[N];
    int len,tmp;
    int vis[2];
    node()
    {
        fail=NULL;
        num=0;
        len=0;
        tmp=-1;
        for(int i=0; inext[tmp]==NULL)
            p->next[tmp]=new node();
        p=p->next[tmp];
    }
    p->num=pos;
    p->len=len;
    p->tmp=-1;
}
void get_fail()
{
    ac[tail++]=root;
    while(head!=tail)
    {
        node *p=ac[head++];
        node *tmp=NULL;
        for(int i=0; inext[i]!=NULL)
            {
                if(p==root)
                    p->next[i]->fail=root;
                else
                {
                    tmp=p->fail;
                    while(tmp!=NULL)
                    {
                        if(tmp->next[i]!=NULL)
                        {
                            p->next[i]->fail=tmp->next[i];
                            break;
                        }
                        tmp=tmp->fail;
                    }
                    if(tmp==NULL)
                        p->next[i]->fail=root;
                }
                ac[tail++]=p->next[i];
            }
        }
    }
}
int ans0[100005],ans1[100005];
void query(char *str)
{
    node *p=root;
    int tmp,len=strlen(str);
    for(int i=0; inext[tmp]==NULL&&p!=root)
            p=p->fail;
        p=p->next[tmp];
        if(p==NULL) p=root;
        node *exp=p;
        while(exp!=root)
        {
            if(exp->num!=0)
            {
                exp->vis[0]++;
                if(exp->tmp==-1||exp->tmp+exp->len<=i)
                {
                    exp->vis[1]++;
                    exp->tmp=i;
                }
            }
            exp=exp->fail;
        }
    }
}
int get_ans(char *s,int flag)
{
    int i=0,j;
    node *p=root;
    while(s[i])
    {
        j=s[i++]-'a';
        p=p->next[j];
    }
    return p->vis[flag];
}
void del(node *root)
{
    int i;
    if(root!=NULL)
    {
        for(i=0; i<26; i++)
        {
            if(root->next[i]!=NULL)
                del(root->next[i]);
        }
        free(root);
    }
}
int main()
{
    int n,con=1;
    while(~scanf("%s",S))
    {
        scanf("%d",&n);
        root=new node();
        head=tail=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d %s",&c[i],str[i]);
            build(str[i],i);
        }
        mem(ans0);
        mem(ans1);
        get_fail();
        query(S);
        printf("Case %d\n",con++);
        for(int i=1; i<=n; i++)
            printf("%d\n",get_ans(str[i],c[i]));
        printf("\n");
        del(root);
    }
    return 0;
}


你可能感兴趣的:(AC自动机)