hdu2896 病毒侵袭 AC自动机入门题 N(N <= 500)个长度不大于200的模式串(保证所有的模式串都不相同), M(M <= 1000)个长度不大于10000的待匹配串,问待匹配串中有哪几个模式串,
2017-09-12 11:28
399 查看
/** 题目:hdu2896 病毒侵袭 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896 题意:N(N <= 500)个长度不大于200的模式串(保证所有的模式串都不相同), M(M <= 1000)个长度不大于10000的待匹配串,问待匹配串中有哪几个模式串, 题目保证每个待匹配串中最多有三个模式串。 思路:ac自动机做法,字符为可见字符,那么直接就是他们的ascii值作为每一个字符的标志。最多128; 由于不超过三个,所以找到3个就可以return ;节约时间。 AC自动机好文章:http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html */ #include<bits/stdc++.h> using namespace std; #define P pair<int,int> #define ms(x,y) memset(x,y,sizeof x) #define LL long long const int maxn = 22; const int mod = 1e9+7; const int maxnode = 500*200+10; const int sigma_size = 128; vector<int> ans; struct AhoCorasickAutomata { int ch[maxnode][sigma_size]; int val[maxnode]; int sz; int f[maxnode]; int last[maxnode]; void clear(){sz = 1; memset(ch[0],0,sizeof ch[0]); } int idx(char c){return c-'a'; } void insert(char *s,int x) { int u = 0, n = strlen(s); for(int i = 0; i < n; i++){ //int c = idx(s[i]); int c = s[i]; if(!ch[u][c]){ memset(ch[sz], 0, sizeof ch[sz]); val[sz] = 0; ch[u][c] = sz++; } u = ch[u][c]; } val[u] = x; } void find(char *T){ int n = strlen(T); int j = 0; for(int i = 0; i < n; i++){ int c = T[i]; //while(j&&!ch[j][c]) j = f[j]; j = ch[j][c]; if(val[j]) print(j); else if(last[j]) print(last[j]); if(ans.size()==3) return ; } } void print(int j) { if(j){ ans.push_back(val[j]); if(ans.size()==3) return ; print(last[j]); } } void getFail(){ queue<int> q; f[0] = 0; for(int c = 0; c < sigma_size; c++){ int u = ch[0][c]; if(u){f[u] = 0; q.push(u); last[u] = 0;} } while(!q.empty()){ int r = q.front(); q.pop(); for(int c = 0; c < sigma_size; c++){ int u = ch[r][c]; if(!u){ ch[r][c] = ch[f[r]][c]; continue; }//if(!u) continue; q.push(u); int v = f[r]; while(v&&!ch[v][c]) v = f[v]; f[u] = ch[v][c]; last[u] = val[f[u]] ? f[u] : last[f[u]]; } } } } ac ; char s[10005]; int main() { int n, m; while(scanf("%d",&n)==1) { ac.clear(); for(int i = 1; i <= n; i++){ scanf("%s",s); ac.insert(s,i); } ac.getFail(); scanf("%d",&m); int cnt = 0; for(int i= 1; i <= m; i++){ ans.clear(); scanf("%s",s); ac.find(s); if(ans.size()!=0){ cnt++; printf("web %d:",i); sort(ans.begin(),ans.end()); for(int j = 0; j < (int)ans.size(); j++){ printf(" %d",ans[j]); } printf("\n"); } } printf("total: %d\n",cnt); } return 0; } /* 3 aaa bbb ccc 2 aaabbbccc bbaacc */
相关文章推荐
- hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。
- 数据结构实例<三>(匹配链表所有元素删除后并返回新的链表)入门
- 一个大小为N(0<N<1000)的整数数组, 求该数组的子数组(长度大于2)为等差数列的个数?
- 一个大小为N(0<N<1000)的整数数组, 求该数组的子数组(长度大于2)为等差数列的个数?
- hdu2896 病毒侵袭 AC自动机入门题
- 模式匹配(pattern matching)问题:判断一个长为n的字符串X中是否包含常为m的字串Y(m<=n)
- AC自动机 - 多模式串的匹配 --- HDU 3695 Computer Virus on Planet Pandora
- 【HDU2896】病毒侵袭 AC自动机
- scala 开发入门(8)-- 模式匹配
- 给你一个字符串s和一个长度相同的单词列表。 找出s中所有子字符串的起始索引,这些字符串中的每个单词只是一个字符串,没有任何中介字符。
- 假设一个大小为100亿个数据的数组,该数组是从小到大排好序的,现在该数组分成若干段,每个段的数据长度小于20「也就是说:题目并没有说每段数据的size 相同,只是说每个段的 size < 20 而已」
- hdu3065 病毒侵袭持续中 AC自动机入门
- C++第二次实验-输出1000-10000之间,个位和千位相等的所有质数
- 从长度为n的数组中(元素互不相同)任意选择m个数的所有组合
- 一串首尾相连的珠子(m 个),有N 种颜色(N<=10),设计一个算法,取出其中一段,要求包含所有N 中颜色,并使长度最短。并分析时间复杂度与空间复杂度。
- AC自动机模板及对多模式匹配的理解
- 一串首尾相连的珠子(m个),有N种颜色(N<=10),设计一个算法,取出其中一段,要求包含所有N中颜色,并使长度最短
- Scala入门到精通——第十五节 Case Class与模式匹配(二)
- scala 开发入门(8)-- 模式匹配
- 给出N个正整数,其中只有一个数出现了奇数次,其余的数都出现偶数次。求那个出现了奇数次的数。1<=N<=1000,N肯定是奇数。所有出现的整数都不超过1000。