HDU 2222 Keywords Search 【AC自动机(模板题)】

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 53340    Accepted Submission(s): 17396

Problem Description
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
 
Input
First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
 
Output
Print how many keywords are contained in the description.
 
Sample Input
  
  
1 5 she he say shr her yasherhs
 
Sample Output
  
  
3



题意:给定多个模式串,再给一个主串,问主串中含有多少个模式串;

思路:AC自动机,直接上模板,注意模式串可能都有重复的,但重复也得加;

失误:模板有动态的,有静态的,做下对比,另外字典树的节点数组最好开大一点,防止溢出;


动态:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

char str[1100000];
int head,tail;
typedef struct Trie{
	int num;
	Trie *fail;
	Trie *son[26];
}Node;

Node *root,*Q[1000000+22];

void Init(Node *p)
{
    int i=0;
    for(i=0;i<26;++i) p->son[i]=NULL; 
    p->fail=NULL; p->num=0;
}
void Insert(char *str)
{
	int i=0; Node *p1=root,*p2;
	while(str[i])
    {
		int pos=str[i]-'a';
		if(p1->son[pos]==NULL)
		{
		     p2=new Node; Init(p2);
			 p1->son[pos]=p2; 
		}
	    p1=p1->son[pos]; ++i; 
	} 
	++p1->num;
}
void Built()
{
	Q[tail++]=root;
	while(head!=tail) 
	{
		Node *now=Q[head++]; int i=0;
		for(i=0;i<26;++i)
	    {
			if(now->son[i]!=NULL)
			{
				if(now==root) 
				{
					now->son[i]->fail=root;
				}
				else 
				{
					Node *tem=now->fail;
					while(tem!=root&&tem->son[i]==NULL)  tem=tem->fail;
					now->son[i]->fail=!tem->son[i]?root:tem->son[i];
				}
				Q[tail++]=now->son[i];
			 } 
		} 
	} 
}
int Query(char *str)
{
	Node *p=root; int i=0,res=0;
	while(str[i])
    {
	    int pos=str[i]-'a';
		while(p->son[pos]==NULL&&p!=root) p=p->fail;
		p=p->son[pos];
	    if(p==NULL) p=root; ++i;
	    Node *tem=p;
	    while(tem!=root&&tem->num!=-1) {
	    	res+=tem->num;
	    	tem->num=-1;
	    	tem=tem->fail;
		}
	}
	return res;
}
int main()
{
	int T,i,N;
	scanf("%d",&T);
	while(T--)
	{
		head=tail=0; 
		root=new Node; Init(root);
		scanf("%d",&N); 
		while(N--) {
			scanf("%s",str); Insert(str);
		} 
		Built();
		scanf("%s",str);  
		printf("%d\n",Query(str));
	} 
	return 0;
} 


静态写的刚开始700ms 还不如静态300ms 加了一句顿时时间减少了;

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
 
const int MAXN=1e6+33;

struct AC_Trie{
	int son[MAXN][26],sum[MAXN],fail[MAXN];
	int sn,root;
	void Init()
    {
	     sn=0;	root=NewNode(); 
	}
    int NewNode()
    {
    	int i=0; 
    	for(i=0;i<26;++i)
    	{
    		son[sn][i]=-1;
		}
		sum[sn]=0; ++sn; 
		return sn-1;
	}
	void Insert(char *str)
	{
		int i=0,p=root;
		while(str[i]) {
			int pos=str[i]-'a';
			if(son[p][pos]==-1) son[p][pos]=NewNode();
			p=son[p][pos]; ++i;
		}
		++sum[p];
	}
	void Built()
	{
		queue<int> Q; int i=0;
		for(i=0;i<26;++i)
		{
			if(son[root][i]==-1) son[root][i]=root;
			else {
				fail[son[root][i]]=root;
				Q.push(son[root][i]);
			}
		}
		while(!Q.empty()) {
			int now=Q.front(); Q.pop();
			for(i=0;i<26;++i) 
			{
				if(son[now][i]==-1) son[now][i]=son[fail[now]][i];
				else {
					fail[son[now][i]]=son[fail[now]][i];
					Q.push(son[now][i]);
				} 
			 } 
		}
	}
	int Query(char *str)
	{
		int i=0,p=root,res=0;
		while(str[i])
		{
			int pos=str[i]-'a';
			p=son[p][pos]; ++i;
			int tem=p;
			while(tem!=root&&sum[tem])//加上sum[tem]时间到200ms  
			{
				res+=sum[tem];  
				sum[tem]=0;
				tem=fail[tem];
			}
		}
		return res;
	}
}AC;
char str[1000000+33];
int main()
{
	int T,i,N;
	scanf("%d",&T);
	while(T--)
	{
		AC.Init();
		scanf("%d",&N);
		while(N--)
		{
			scanf("%s",str); AC.Insert(str); 
		}
		scanf("%s",str);
		AC.Built();
		printf("%d\n",AC.Query(str));
	}
	return 0;
} 

怎么都感觉用数组不爽,再写个结构体版的,这样感觉好多了;

下面模板主要就是Built函数中a[root].son[i]和优化的理解;

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

typedef struct Trie{
	int num,fail,son[26]; 
}Node;
Node a[1000000+11];
int sn,root=0;

int NewNode()
{
	int i=0;
	for(i=0;i<26;++i) a[sn].son[i]=0; 
	a[sn].num=a[sn].fail=0; ++sn;
	return sn-1;
}
void Insert(char *str)
{
	int i=0,p=root;
	while(str[i]) {
		int pos=str[i]-'a';
		if(a[p].son[pos]==0) a[p].son[pos]=NewNode();
		p=a[p].son[pos]; ++i;
	}
	++a[p].num;
}
void Built()
{
	int i=0; queue<int> Q; 
	for(i=0;i<26;++i)
	{
		if(a[root].son[i]) {
			int p=a[root].son[i];
			a[p].fail=root; Q.push(p);
		}
	}
	while(!Q.empty()) {
		int now=Q.front(); Q.pop();
		for(i=0;i<26;++i)
		{
			if(!a[now].son[i]) //难理解的主要就在这里 为了Query中查询方便 
			     a[now].son[i]=a[a[now].fail].son[i];//匹配某一字符直接就找到他的节点编号 
			else{                                    //省去了其他的繁琐操作 
				int p=a[now].son[i]; 
				a[p].fail=a[a[now].fail].son[i];
				Q.push(p);
			}
		} 
	}
} 
int Query(char *str)
{
	int i=0,p=root,res=0;
	while(str[i]) {
		int pos=str[i]-'a';
		p=a[p].son[pos];//就是为了这一句 不管在哪 直接匹配到 牛 
	    ++i;
		int tem=p;
		while(tem!=root&&a[tem].num)//自己发现的tem为0的话 要么已经访问过  
		{                          //要么表示模式串的结尾 就没必要加了 
			res+=a[tem].num;       //加上之后明显优化 不加就可能挂 
			a[tem].num=0; 
			tem=a[tem].fail;
		}
	}
	return res;
}
char str[1000000+11]; 
int main()
{
	int T,i,N;
	scanf("%d",&T);
	while(T--)
	{
		sn=0; root=NewNode();
		scanf("%d",&N);
		while(N--){
			scanf("%s",str); Insert(str);
		}
		scanf("%s",str); Built();
		printf("%d\n",Query(str));
	} 
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值