首页 > 代码库 > ac自动机基础模板(hdu2222)
ac自动机基础模板(hdu2222)
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.
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
15shehesayshrheryasherhsSample 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)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。