码迷,mamicode.com
首页 > 其他好文 > 详细

ac自动机基础模板(hdu2222)

时间:2017-06-26 23:58:09      阅读:399      评论:0      收藏:0      [点我收藏+]

标签:node   sys   ant   struct   wan   time   amp   arc   经历   

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.

InputFirst 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.
OutputPrint how many keywords are contained in the description.Sample Input
1
5
she
he
say
shr
her
yasherhs
Sample Output
3
题目就是给定一些串,让你在一个字符串中搜出现几个串。这就是ac自动机的基本模板。
注意事项:memset是非常慢的,尽量少用,注意节点的跳转,就是我代码中的u节点,注意scanf printf 而不是cin cout
ps:动态开节点,动态初始化,注意是动态。
惨痛的经历,刷了一面半的wa tle re
加!!为犯错误的地方,引起重视。
#pragma GCC optimize("O2")
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue> 
#define maxnode 501000//!!

using namespace std;

struct Trie
{
	int ch[maxnode][26],val[maxnode],fail[maxnode];
	int sz=0,root=0;
	
	int newnode()//!!
	{
		for(int i=0;i<26;i++)
			ch[sz][i]=-1;
		val[sz++]=0;
		return sz-1;
	}
	
	void clear(){sz=0;root=newnode();}//!!
	
	int idx(char s){return s-‘a‘;}
	
	void insert(char* s)
	{
		int u=0,len=strlen(s);
		for(int i=0;i<len;i++)
		{
			int v=idx(s[i]);
			if(ch[u][v]==-1)
				ch[u][v]=newnode();//!!
			u=ch[u][v];
		}
		val[u]++;
	}
	
	void build()
	{
		queue<int> q;
		fail[root]=root;
		for(int i=0;i<26;i++)
		{
			if(ch[root][i]==-1)//!!
				ch[root][i]=root;
			else
			{
				fail[ch[root][i]]=root;
				q.push(ch[root][i]);
			}
		}
		while(!q.empty())
		{
			int u=q.front();q.pop();//!!
			for(int i=0;i<26;i++)
			{
				if(ch[u][i]==-1)
					ch[u][i]=ch[fail[u]][i];//!!
				else
				{
					fail[ch[u][i]]=ch[fail[u]][i];//!!
					q.push(ch[u][i]);
				}
			}
		}
	}
	
	int query(char* s)
	{
		int u=root,len=strlen(s);
		int ans=0;
		for(int i=0;i<len;i++)//!!
		{
			int v=idx(s[i]);
			u=ch[u][v];//!!
			int tmp=u;
			while(tmp!=root)
			{
				ans+=val[tmp];
				val[tmp]=0;//!!
				tmp=fail[tmp];
			}
		}
		return ans;
	}
	
};
Trie ac;
char  str[maxnode*2];//!!

int main()
{
	int t,n;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		ac.clear();//!!
		while(n--)
		{
			scanf("%s",str);
			ac.insert(str);
		}
		ac.build();
		scanf("%s",str);
		printf("%d\n",ac.query(str));
	}
}

 

ac自动机基础模板(hdu2222)

标签:node   sys   ant   struct   wan   time   amp   arc   经历   

原文地址:http://www.cnblogs.com/foreverpiano/p/7082771.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!