ZOJ 题目3228 Searching the String(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 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 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.


Author: LI, Jie

Source: ZOJ Monthly, July 2009

题目大意:就是0表示可以覆盖的匹配的个数,1表示不可以覆盖的匹配个数

本来挺水的一道题,不可覆盖的时候就是匹配的位置比上个位置大于等于这个串的长度就行,结果各种MLE,,都快哭了,,从网上找代码也是也是MTE,我以为oj炸了,,各种减啊,,还是MTE,,,最后想到把字典树上分配的内存free掉好了,,提交就过了,,激动坏了,想想之前的可不可以,,找到之前第一次提交的代码,加上del,,也过了大哭大哭,弱就是弱啊,,整好几个小时就是忘free了,,大哭,,不多还是多少感觉对ac自动机多了一些理解

之前的ac代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char str[100010],key[100010][10];
int op[100010];
int head,tail;  
struct node  
{  
    node *fail;  
    node *next[26];  
    int cnt[2],flag,time,len;  
    node()
    {  
        fail=NULL;
		flag=time=0;
       cnt[0]=cnt[1]=0;  
        for(int i=0;i<26;i++)  
            next[i]=NULL;  
    }
}*q[500050];  
node *root;  
void insert(char *s)  
{  
    int temp,len,i;  
    node *p=root;  
    len=strlen(s);  
    for(i=0;i<len;i++)  
    {  
        temp=s[i]-'a';  
        if(p->next[temp]==NULL)  
            p->next[temp]=new node();  
        p=p->next[temp];  
    }  
    p->flag=1;
	p->len=len;
}  
void build_ac()  
{  
    q[tail++]=root;  
    while(head!=tail)  
    {  
        node *p=q[head++];  
        node *temp=NULL;  
        for(int i=0;i<26;i++)  
        {  
            if(p->next[i]!=NULL)  
            {  
                if(p==root)  
                    p->next[i]->fail=root;  
                else  
                {  
                    temp=p->fail;  
                    while(temp!=NULL)  
                    {  
                        if(temp->next[i]!=NULL)  
                        {  
                            p->next[i]->fail=temp->next[i];  
                            break;  
                        }  
                        temp=temp->fail;  
                    }  
                    if(temp==NULL)  
                    {  
                        p->next[i]->fail=root;  
                    }  
                }  
                q[tail++]=p->next[i];  
            }  
        }  
    }  
}  
void query()  
{   
	int t=0;
    int len=strlen(str);  
    node *p=root,*temp;  
    for(int i=0;i<len;i++)  
    {  
        int x=str[i]-'a';
		t++;
        while(p->next[x]==NULL&&p!=root)  
            p=p->fail;  
        p=p->next[x];  
        if(p==NULL)  
        {  
            p=root;  
        }  
        temp=p;  
        while(temp!=root)  
        {  
			if(temp->flag)
			{
				temp->cnt[0]++;
				if(t-temp->time>=temp->len)
				{
					temp->cnt[1]++;
					temp->time=t;//最后匹配的位置
				}
			}
            temp=temp->fail;  
        }  
    }
}
int find(char s[],int flag)  
{  
    int len=strlen(s),i,j,now;  
    node *current=NULL;  
    current=root;  
    for(i=0;i<len;i++)  
    {  
        now=s[i]-'a';  
        if(current->next[now]!=NULL)  
        {  
            current=current->next[now];  
        }  
        else  
            return 0;  
    }  
    return current->cnt[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 c=0;
	while(scanf("%s",str)!=EOF)
	{
		int n;
		scanf("%d",&n);
		int i;
		head=tail=0;
		root=new node();
		for(i=1;i<=n;i++)
		{
			scanf("%d%s",&op[i],key[i]);
			//len[i]=strlen(key[i]);
			insert(key[i]);
		}
		build_ac();
		query();
		printf("Case %d\n",++c);
		for(i=1;i<=n;i++)
		{
			int ans=find(key[i],op[i]);
			printf("%d\n",ans);
		}
		printf("\n");
		del(root);\
	}
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
char str[100001],key[7];
bool op[100001];
int pos[100001],ans[100001][2];
int head,tail;  
struct node  
{  
    node *fail;  
    node *next[26];  
    int flag,time,len,id;  
    node()
    {  
        fail=NULL;
		flag=time=0;
      // cnt[0]=cnt[1]=0;
		id=0;
        for(int i=0;i<26;i++)  
            next[i]=NULL;  
    }
}*q[600005];  
node *root;  
int insert(char *s,int id)
{  
    int temp,len,i;  
    node *p=root;  
    len=strlen(s);  
    for(i=0;i<len;i++)  
    {  
        temp=s[i]-'a';  
        if(p->next[temp]==NULL)  
            p->next[temp]=new node();  
        p=p->next[temp];  
    }  
    p->flag=1;
	p->len=len;
	if(p->id==0)
		p->id=id;
	return p->id;
}  
void build_ac()  
{  
    q[tail++]=root;
    while(head!=tail)  
    {  
        node *p=q[head++]; 
        node *temp=NULL;  
        for(int i=0;i<26;i++)  
        {  
            if(p->next[i]!=NULL)  
            {  
                if(p==root)  
                    p->next[i]->fail=root;  
                else  
                {  
                    temp=p->fail;  
                    while(temp!=NULL)  
                    {  
                        if(temp->next[i]!=NULL)  
                        {  
                            p->next[i]->fail=temp->next[i];  
                            break;  
                        }  
                        temp=temp->fail;  
                    }  
                    if(temp==NULL)  
                    {  
                        p->next[i]->fail=root;  
                    }  
                }  
                q[tail++]=p->next[i];  
            }  
        }  
    }  
}  
void query()  
{   
	int t=0;
    int len=strlen(str);  
    node *p=root,*temp;  
    for(int i=0;i<len;i++)  
    {  
        int x=str[i]-'a';
		t++;
        while(p->next[x]==NULL&&p!=root)  
            p=p->fail;  
        p=p->next[x];  
        if(p==NULL)  
        {  
            p=root;  
        }  
        temp=p;  
        while(temp!=root)  
        {  
			if(temp->flag)
			{
				ans[temp->id][0]++;
				if(t-temp->time>=temp->len)
				{
					ans[temp->id][1]++;
					temp->time=t;//最后匹配的位置
				}
			}
            temp=temp->fail;  
        }  
    }
}
/*int find(char s[],int flag)  
{  
    int len=strlen(s),i,j,now;  
    node *current=NULL;  
    current=root;  
    for(i=0;i<len;i++)  
    {  
        now=s[i]-'a';  
        if(current->next[now]!=NULL)  
        {  
            current=current->next[now];  
        }  
        else  
            return 0;  
    }
	int kk=flag?1:0;
    return current->cnt[kk];  
}*/
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 c=0;
	while(scanf("%s",str)!=EOF)
	{
		int n;
		scanf("%d",&n);
		int i;
		head=tail=0;
		root=new node();
		memset(ans,0,sizeof(ans));
		for(i=1;i<=n;i++)
		{
			scanf("%d%s",&op[i],key);
			//len[i]=strlen(key[i]);
			pos[i]=insert(key,i);
		}
		build_ac();
		query();
		printf("Case %d\n",++c);
		for(i=1;i<=n;i++)
		{
			int k=op[i]?1:0;
			printf("%d\n",ans[pos[i]][k]);
		}
		printf("\n");
	//	del(root);
	}
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值