[AC自动机(模板题)] hdu2222 Keywords Search
2017-08-17 19:46
381 查看
@(ACM题目)[字符串,AC自动机]
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.
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.
5
she
he
say
shr
her
yasherhs
注意坑点:模式串中有重复,要重复计数。
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
15
she
he
say
shr
her
yasherhs
Sample Output
3题目分析
AC自动机的模板题,求有多少个模式串出现在文本串中。注意坑点:模式串中有重复,要重复计数。
代码
#include<bits/stdc++.h> using namespace std; const int maxn = 5e5+7;//number of letters const int maxm = 26;//size of lower case letters int res; //a Trie of lower case strings struct AhoCorasickAutomata //Aho–Corasick algorithm { int ch[maxn][maxm]; int val[maxn], fail[maxn], last[maxn];//assume that val is positive int tot;//节点总数 void init() { tot = 1; memset(ch[0], 0, sizeof ch[0]); } //insert an string s, whose value is v; note that v != 0. 0 stands for "not an end point" void add(char *s, int v) { int u = 0;//root int n = strlen(s); for(int i = 0; i < n; ++i) { int c = s[i] - 'a'; if(!ch[u][c])//the point does not exist { memset(ch[tot], 0, sizeof ch[tot]); val[tot] = 0;//the val of middle point is 0 ch[u][c] = tot++; } u = ch[u][c]; } val[u] += v; } void getFail() { queue<int> q; fail[0] = 0; //初始化队列 for(int c = 0; c < maxm; ++c) { int u = ch[0][c]; if(u) { fail[u] = last[u] = 0; q.push(u); } } //按照bfs的顺序计算fail while(!q.empty()) { int cur = q.front(); q.pop(); for(int c = 0; c < maxm; ++c) { int u = ch[cur][c]; if(!u) { ch[cur][c] = ch[fail[cur]][c]; continue; } q.push(u); int v = fail[cur]; while(v && !ch[v][c]) v = fail[v]; fail[u] = ch[v][c]; if(val[fail[u]]) last[u] = fail[u]; else last[u] = last[fail[u]]; } } } void print(int j)//找到一个与文本串匹配的模式串,该模式串的结尾位置为j { if(j) { //此处进行操作 if(val[j] > 0) { res += val[j]; val[j] = -1; } print(last[j]); } } int finda(char *T)//return -1 if not exists { int n = strlen(T); int j = 0;//j为当前结点编号,初始为根结点 for(int i = 0; i < n; ++i)//文本串T的当前指针 { int c = T[i] - 'a'; //while(j && !ch[j][c]) j = f[j]; //顺着失配边走,直到可以匹配 j = ch[j][c]; if(val[j]) print(j); //找到了 else if(last[j]) print(last[j]); } } }a; const int maxs = 1e6 + 5; char s[maxs]; int main() { int T; cin >> T; while(T--) { res = 0; a.init(); int n; scanf("%d", &n); for(int i = 0; i < n; ++i) { scanf("%s", s); a.add(s, 1); } a.getFail(); scanf("%s", s); a.finda(s); printf("%d\n", res); } }
相关文章推荐
- Keywords Search---hdu2222(AC自动机 模板)
- AC自动机 模板 【HDU2222】 Keywords Search
- 【HDU2222】【AC自动机模板 测烂为止】Keywords Search
- hdu2222-AC自动机&模板-Keywords Search
- HDOJ 2222.Keywords Search(AC自动机模板)
- AC自动机 hdu2222 Keywords Search
- AC自动机 HDU2222 Keywords Search
- AC自动机模板 hdu2222
- AC自动机 - hdu2222 Keywords Search
- 【字符串·AC自动机】hdu2222 Keywords Search
- HDU2222 Keywords Search(AC自动机模板)
- HDU2222 Keywords Search 【AC自动机模板题】
- hdu2222(ac自动机模板)
- hdu2222 ac自动机模板
- HDU2222 AC自动机 入门模板
- AC自动机 ( 动态建树模板 )——Keywords Search ( HDU 2222 )
- AC自动机(1)--hdu2222(基本模板)
- ac自动机模板(hdu2222)
- 【AC自动机】[HDU2222]Keywords Search
- HDU 2222 Keywords Search(我的第一道AC自动机,模板题)