zoj 3228 Searching the String 母串共包含多少子串(重叠和不重叠)

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





#include
#include
#include
using namespace std;
const int maxn=110000;
struct Node
{
    int index;
    Node* next[26];
    Node* fail;
};
Node temp[maxn*6];
int tp;
Node *root;
//0 可重叠   1 不可重叠
int ans[2][maxn];
int pos[maxn];
//1 不可重叠时候  pos[i] = -1...
//这样只要  p - pos[i] >= len[i].(p为当前成功匹配的位置)
//pos[i]为模式i上一次成功匹配的位置
char str[maxn][8];
int flag[maxn];
void reset(Node* p)
{
    p->index=-1;
    for(int i=0;i<26;i++) p->next[i]=NULL;
    p->fail=root;//important
    if(p==root) p->fail=NULL;//important
}
//初始化
void init()
{
    memset(ans,0,sizeof(ans));
    memset(pos,-1,sizeof(pos));
    tp=0;
    root=&temp[tp++];
    reset(root);
}
//插入字典树
int insert(char *word,int index)
{
    Node *p=root;
    for(int i=0;word[i];i++)
    {
        int x=word[i]-'a';
        if(p->next[x]==NULL)
        {
            p->next[x]=&temp[tp++];
            reset(p->next[x]);
        }
        p=p->next[x];
    }
    if(p->index==-1) p->index=index;
    return p->index;
}
//构造失败指针
Node *que[maxn*6];
int front,rear;
void DFA()
{
    Node* p=root;
    front=rear=0;
    que[rear++]=p;
    while(front    {
        Node* t=que[front++];
        for(int i=0;i<26;i++)
        {
            Node* cnt=t->next[i];
            if(cnt!=NULL)
            {
                Node* fath=t->fail;
                while(fath!=NULL&&fath->next[i]==NULL)
                {
                    fath=fath->fail;
                }
                if(fath!=NULL)
                {
                    cnt->fail=fath->next[i];
                }
                else
                {
                    cnt->fail=p;//important
                }
                que[rear++]=cnt;
            }
        }
    }
}
char mat[maxn];//母串
void query()
{
    Node* p=root;
    Node* cnt;
    Node* fath;
    for(int i=0;mat[i];i++)
    {
        int x=mat[i]-'a';
        cnt=p->next[x];
        if(cnt!=NULL)
        {
            for(fath=cnt;fath!=NULL;fath=fath->fail)
            {
                if(fath->index!=-1)
                {
                    ans[0][fath->index]++;


                    int len=strlen(str[fath->index]);
                    if(i-pos[fath->index]>=len)
                    {
                        ans[1][fath->index]++;
                        pos[fath->index]=i;
                    }
                }
            }
            p=cnt;
        }
        else
        {
            fath=p->fail;
            while(fath!=NULL&&fath->next[x]==NULL) fath=fath->fail;
            if(fath!=NULL)
            {
                cnt=fath->next[x];
                for(fath=cnt;fath!=NULL;fath=fath->fail)
                {
                    if(fath->index!=-1)
                    {
                        ans[0][fath->index]++;


                        int len=strlen(str[fath->index]);
                        if(i-pos[fath->index]>=len)
                        {
                            ans[1][fath->index]++;
                            pos[fath->index]=i;
                        }
                    }
                }
                p=cnt;
            }
            else
            {
                p=root;
            }
        }
    }
}
int main()
{
    int cas=1;
    while(scanf("%s",mat)==1)
    {
        init();
        int n;scanf("%d",&n);
        for(int i=0;i        {
            scanf("%d%s",&flag[i],str[i]);
            insert(str[i],i);
        }
        DFA();
        query();//0:overlap   1:not overlap
        printf("Case %d\n",cas++);
        for(int i=0;i        {
            int index=insert(str[i],-1);
            printf("%d\n",ans[flag[i]][index]);
        }printf("\n");
    }
    return 0;
}

你可能感兴趣的:(acm_数据结构)