hdu 2222 AC自动机模板题(指针版+数组版)
2015-08-27 17:48
225 查看
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.
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
指针版
#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; }
相关文章推荐
- C/C++中各种类型int、long、double、char表示范围(最大最小值)
- linux中vmstat命令详解
- eclipse中outline中图标含义小结
- WR703N TTL刷机过程
- 用Python实现基本排序算法02——选择排序
- Android framework完整源码下载
- SQL数据库的升级过程—解决WMI服务无法链接过程
- rqnoj 389 心灵的抚慰 (floyd求解环的最小权值)
- 通过实例让你真正明白mapreduce---填空式、分布(分割)编程
- leetcode: Symmetric Tree
- N 的阶乘的另一种解法
- linux系统硬盘分区挂载及常用查看命令
- WPF中使用Image控件显示图片
- 详解 CSS 属性 - 伪类和伪元素的区别(再也不用概念盲了!!!)
- 分享MyEclipse安装及安装报错的解决方法
- LintCode-删除排序链表中的重复元素
- uploadPreview 上传图片预览
- Ajax实现跨域请求
- Storm-0.9.0.1安装部署 指导
- 初识Git