ZOJ3228 Searching the String

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

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.

题意:先输入一个模板串,然后输入n个子串,查询子串出现的子树,0代表可以重叠,1代表不可以重叠。。

分析:这两个查询其实是独立的,但我们也可以把他一起完成,可以重叠的就是模板,不可以重叠的需要记录下插入的每个子串的位置,询问的时候要减去上一个询问的位置大于这个子串的长度才可以。


#include
#include
#include
#include
using namespace std;
const int MAXN=100010;
const int INF=1<<30;
struct node
{
    int pos[2],cnt[2],fail,len;
    int next[26];
    void init()
    {
        pos[0]=pos[1]=cnt[0]=cnt[1]=0;
        fail=len=0;
        memset(next,0,sizeof(next));
    }
}tree[MAXN*6];
char s[10],str[MAXN];
int size,ans[MAXN],pos[MAXN],vis[MAXN];
int find(int x)
{
    int p=0,i=0,index;
    while(s[i])
    {
        index=s[i]-'a';
        if(!tree[p].next[index])
            return 0;
        p=tree[p].next[index];
        i++;
    }
    return tree[p].pos[x];
}
void insert(int pos,int x)
{
    int i=0,p=0,index;
    while(s[i])
    {
        index=s[i]-'a';
        if(!tree[p].next[index])
        {
            tree[++size].init();
            tree[p].next[index]=size;
        }
        p=tree[p].next[index];
        i++;
    }
    tree[p].len=i;  //记录长度
    tree[p].pos[x]=pos; //记录位置
    tree[p].cnt[x]++;
}
void build_ac_automation()
{
    int i;
    queue q;
    q.push(0);
    while(!q.empty())
    {
        int temp=q.front();
        q.pop();
        for(i=0;i<26;i++)
        {
            if(tree[temp].next[i])
            {
                int p=tree[temp].next[i];
                if(temp)
                {
                    tree[p].fail=tree[tree[temp].fail].next[i];
                }
                q.push(p);
            }
            else
                tree[temp].next[i]=tree[tree[temp].fail].next[i];
        }
    }
}
void query()
{
    int temp=0,index,p,i=0;
    while(str[i])
    {
        index=str[i]-'a';
        temp=tree[temp].next[index];
        for(p=temp;p!=0;p=tree[p].fail)
        {
            if(tree[p].cnt[0])
                ans[tree[p].pos[0]]+=tree[p].cnt[0];
            if(tree[p].cnt[1]&&i-vis[tree[p].pos[1]]>=tree[p].len)  //当前位置减去上一位置要大于插入字符串的长度
            {
                vis[tree[p].pos[1]]=i;
                ans[tree[p].pos[1]]+=tree[p].cnt[1];
            }
        }
        i++;
    }
}
int main()
{
    int n,x,i,flag=1;
    while(scanf("%s",str)!=EOF)
    {
        scanf("%d",&n);
        size=0;
        tree[0].init();
        for(i=1;i<=n;i++)
        {
            scanf("%d%s",&x,s);
            ans[i]=0;
            vis[i]=-INF;
            pos[i]=find(x);
            if(!pos[i]) //防止询问相同类型的字符串,相同的字符串后面也会输出
            {
                pos[i]=i;   //这里也确保了插入的树里pos和外面的pos相同
                insert(i,x);
            }
        }
        build_ac_automation();
        query();
        printf("Case %d\n",flag++);
        for(i=1;i<=n;i++)
            printf("%d\n",ans[pos[i]]);
        printf("\n");
    }
    return 0;
}


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