hdu 2222 AC自动机模板题(指针版+数组版)
来源:互联网 发布:2016日本进出口数据 编辑:程序博客网 时间:2024/05/16 04:04
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 44959 Accepted Submission(s): 14215
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.
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.
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
15shehesayshrheryasherhs
Sample Output
3
指针版
#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <algorithm>#include <cstdio>#include <string>#include <cstring>#include <cmath>#include <ctime>using namespace std;#pragma comment(linker,"/STACK:102400000,102400000") /// kuo zhan#define clr(s,x) memset(s,x,sizeof(s))#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define lowbit(x) (x&(-x))#define PB push_back#define For(i,a,b) for(int i=a;i<b;i++)#define FOR(i,a,b) for(int i=a;i<=b;i++)typedef long long LL;typedef unsigned int uint;typedef unsigned long long ULL;typedef vector<int> vint;typedef vector<string> vstring;void RI (int& x){ x = 0; char c = getchar (); while (c == ' '||c == '\n') c = getchar (); bool flag = 1; if (c == '-'){ flag = 0; c = getchar (); } while (c >= '0' && c <= '9'){ x = x * 10 + c - '0'; c = getchar (); } if (!flag) x = -x;}void RII (int& x, int& y){RI (x), RI (y);}void RIII (int& x, int& y, int& z){RI (x), RI (y), RI (z);}const double PI = acos(-1.0);const int maxn = 1e6 + 100;const int maxm = 55;const int kind = 26;const LL mod = 1e9 + 7;const double eps = 1e-9;const int INF = 0x7fffffff;/************************************END DEFINE*********************************************/struct node{ int cnt; node *nxt[kind]; node *fail;};node *q[maxn];char kw[maxm];char s[maxn];int head,tail;node *root;void add_word(char *word,node *root){ int idx,len; node *p = root,*newnode; len = strlen(word); For(i,0,len){ idx = word[i] - 'a'; if(!p->nxt[idx]){ newnode = (struct node *)malloc(sizeof(struct node)); For(j,0,kind)newnode->nxt[j] = 0; newnode->cnt = 0; newnode->fail = 0; p->nxt[idx] = newnode; } p = p->nxt[idx]; } p->cnt++;}void build(node *root){ head = 0,tail = 1; q[head] = root; node *tmp,*p; while(head<tail){ tmp = q[head++]; For(i,0,kind){ if(tmp->nxt[i]){ if(tmp == root)tmp->nxt[i]->fail = root; else{ p = tmp->fail; while(p){ if(p->nxt[i]){ tmp->nxt[i]->fail = p->nxt[i]; break; } p = p->fail; } if(!p)tmp->nxt[i]->fail = root; } q[tail++] = tmp->nxt[i]; } } }}int query(node *root){ int ct = 0,idx = 0,len = strlen(s); node *p = root; For(i,0,len){ idx = s[i]-'a'; while(!p->nxt[idx]&&p!=root)p = p->fail; p=p->nxt[idx]; if(!p)p=root; node *tmp = p; while(tmp!=root){ if(tmp->cnt>=0){ ct+=tmp->cnt; tmp->cnt=-1; } else break; tmp = tmp->fail; } } return ct;}int main(){ int T; RI(T); while(T--){ int n; RI(n); node *root = (struct node *)malloc(sizeof(struct node)); For(i,0,kind)root->nxt[i] = 0; root->fail = 0; root->cnt = 0; while(n--){ scanf("%s",kw); add_word(kw,root); } build(root); scanf("%s",s); int ans = query(root); printf("%d\n",ans); } return 0;}
数组版#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <algorithm>#include <cstdio>#include <string>#include <cstring>#include <cmath>#include <ctime>using namespace std;#pragma comment(linker,"/STACK:102400000,102400000") /// kuo zhan#define clr(s,x) memset(s,x,sizeof(s))#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define lowbit(x) (x&(-x))#define PB push_back#define For(i,a,b) for(int i=a;i<b;i++)#define FOR(i,a,b) for(int i=a;i<=b;i++)typedef long long LL;typedef unsigned int uint;typedef unsigned long long ULL;typedef vector<int> vint;typedef vector<string> vstring;void RI (int& x){ x = 0; char c = getchar (); while (c == ' '||c == '\n') c = getchar (); bool flag = 1; if (c == '-'){ flag = 0; c = getchar (); } while (c >= '0' && c <= '9'){ x = x * 10 + c - '0'; c = getchar (); } if (!flag) x = -x;}void RII (int& x, int& y){RI (x), RI (y);}void RIII (int& x, int& y, int& z){RI (x), RI (y), RI (z);}/************************************END DEFINE*********************************************/const int maxn = 500010;const int kind = 26;int idx(char c){return c-'a';}int chd[maxn][kind],sz,val[maxn],fail[maxn],last[maxn];void init(){ sz=1; memset(chd[0],0,sizeof(chd[0])); memset(val,0,sizeof(val)); memset(fail,0,sizeof(fail)); memset(last,0,sizeof(last));}void add(char *p){ int u=0; int len = strlen(p); for(int i=0;i<len;i++){ int c = idx(p[i]); if(chd[u][c]==0){ memset(chd[sz],0,sizeof(chd[sz])); chd[u][c]=sz++; } u=chd[u][c]; } val[u]++;}int getFail(){ queue<int> q; fail[0]=0; for(int c=0;c<kind;c++){ int u=chd[0][c]; if(u){ fail[u]=0;q.push(u);last[u]=0; } } while(!q.empty()){ int r=q.front(); q.pop(); for(int c=0;c<kind;c++){ int u=chd[r][c]; if(!u){ chd[r][c]=chd[fail[r]][c]; continue; } q.push(u); int v=fail[r]; while(v&&!chd[v][c]) v=fail[v]; fail[u]=chd[v][c]; last[u]=(val[fail[u]])?fail[u]:last[fail[u]]; } }}int query(char *T){ int len = strlen(T), j = 0,ans = 0; for(int i = 0; i < len; i++){ int c = idx(T[i]); while(j && chd[j][c]==0) j = fail[j]; j = chd[j][c]; int temp = j; while(temp && val[temp]){ ans+=val[temp]; val[temp]=0; temp = fail[temp]; } } return ans;}char t[55];char s[1000005];int main(){ int T; RI(T); while(T--){ int n; RI(n); init(); while(n--){ scanf("%s",t); add(t); } getFail(); scanf("%s",s); printf("%d\n",query(s)); } return 0;}
0 0
- hdu 2222 AC自动机模板题(指针版+数组版)
- AC自动机模板(数组+指针)hdu2222
- AC自动机模板(数组实现版)
- hdu 2222 AC自动机模板(非指针)
- hdu 2896 AC自动机(指针+数组)
- hdu 3065 AC自动机(指针+数组)
- hdu 2222 AC自动机模板题
- hdu 2222 AC自动机模板题
- hdu 2222 ac自动机模板题
- HDU 2222 ac自动机入门模板题
- HDU 2222 AC自动机模板题
- HDU - 2222 AC自动机模板题
- hdu 2222 ac自动机模板题
- hdu 2222AC自动机模板题
- HDU 2222 ac自动机模板题
- AC自动机入门+模板 (HDU 2222)
- HDU 2222 (AC自动机模板)
- hdu 2222 Keywords Search(AC自动机模板题)
- (四) HTTP/2的流状态
- WPF中使用Image控件显示图片
- linux常用命令(11):nl命令
- WR703N TTL刷机过程
- python 中文编码 #-*-coding:utf-8-*-
- hdu 2222 AC自动机模板题(指针版+数组版)
- 分析rails日志,计算响应率
- eclipse中outline中图标含义小结
- MySQL主从异步, 半同步配置
- heat ResourceGroup资源使用介绍
- 改进PHP的var_dump()方法使之适应显示从数据库中查出来的数据
- Hidden Number
- C/C++中各种类型int、long、double、char表示范围(最大最小值)
- linux中vmstat命令详解