HDU 5384 Danganronpa【AC自动机】
2015-08-13 21:08
232 查看
AC自动机模板题
和HDU 2222不同的是,HDU 2222每次匹配之后就要把end清空,他求的是,模式串中有多少个出现在文本串中。
和HDU 2222不同的是,HDU 2222每次匹配之后就要把end清空,他求的是,模式串中有多少个出现在文本串中。
[code]// whn6325689 // Mr.Phoebe // http://blog.csdn.net/u013007900 #include <algorithm> #include <iostream> #include <iomanip> #include <cstring> #include <climits> #include <complex> #include <fstream> #include <cassert> #include <cstdio> #include <bitset> #include <vector> #include <deque> #include <queue> #include <stack> #include <ctime> #include <set> #include <map> #include <cmath> #include <functional> #include <numeric> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; #define eps 1e-9 #define PI acos(-1.0) #define INF 0x3f3f3f3f #define LLINF 1LL<<62 #define speed std::ios::sync_with_stdio(false); typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<ll, ll> pll; typedef complex<ld> point; typedef pair<int, int> pii; typedef pair<pii, int> piii; typedef vector<int> vi; #define CLR(x,y) memset(x,y,sizeof(x)) #define CPY(x,y) memcpy(x,y,sizeof(x)) #define clr(a,x,size) memset(a,x,sizeof(a[0])*(size)) #define cpy(a,x,size) memcpy(a,x,sizeof(a[0])*(size)) #define mp(x,y) make_pair(x,y) #define pb(x) push_back(x) #define lowbit(x) (x&(-x)) #define MID(x,y) (x+((y-x)>>1)) template<class T> inline bool read(T &n) { T x = 0, tmp = 1; char c = getchar(); while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar(); if(c == EOF) return false; if(c == '-') c = getchar(), tmp = -1; while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar(); n = x*tmp; return true; } template <class T> inline void write(T n) { if(n < 0) { putchar('-'); n = -n; } int len = 0,data[20]; while(n) { data[len++] = n%10; n /= 10; } if(!len) data[len++] = 0; while(len--) putchar(data[len]+48); } //----------------------------------- const int maxw = 100000 + 10; const int sigma_size = 27; string str[100010]; struct Trie { int next[maxw][sigma_size],fail[maxw],end[maxw]; int root,L; int newnode() { for(int i=0; i<sigma_size; i++) next[L][i]=-1; end[L++]=-1; return L-1; } void init() { L=0; root=newnode(); } void insert(const char *s,int id) { int now=root,len=strlen(s); for(int i=0; i<len; i++) { if(next[now][s[i]-'a']==-1) next[now][s[i]-'a']=newnode(); now=next[now][s[i]-'a']; } if(end[now]!=-1) end[now]++; else end[now]=1; } void build() { queue<int>Q; fail[root]=root; for(int i=0; i<sigma_size; i++) if(next[root][i]==-1) next[root][i]=root; else { fail[next[root][i]]=root; Q.push(next[root][i]); } while(!Q.empty()) { int now=Q.front(); Q.pop(); for(int i=0; i<sigma_size; i++) if(next[now][i]==-1) next[now][i]=next[fail[now]][i]; else { fail[next[now][i]]=next[fail[now]][i]; Q.push(next[now][i]); } } } int query(const string &buf) { int num=0; int now=root,len=buf.length(); for(int i=0; i<len; i++) { now=next[now][buf[i]-'a']; int tmp=now; while(tmp!=root) { if(end[tmp]!=-1) num+=end[tmp]; tmp=fail[tmp]; } } return num; } }; char st[10010]; Trie ac; int main() { // freopen("data.txt","r",stdin); int n,m; int T; scanf("%d",&T); while(T--) { scanf("%d %d",&n,&m); ac.init(); for(int i=1; i<=n; i++) cin>>str[i]; for(int i=1; i<=m; i++) { scanf("%s",st); ac.insert(st,i); } ac.build(); int ans=0; for(int i=1; i<=n; i++) { ans=ac.query(str[i]); printf("%d\n",ans); } } return 0; }
相关文章推荐
- 黑马程序员-----------C语言基础-----------函数
- 谭浩强C语言程序设计(第三版)部分课后题答案,自己整理的
- IIS 一个服务器下不同站点操作共享文件夹
- 编写高质量代码改善C#程序的157个建议——建议17:多数情况下使用foreach进行循环遍历
- Eclipse中的常用快捷键
- [leetcode] Reverse Integer 反转一个整数
- 让UILabel 或者 UIButton 的大小和它的内容一样大
- final方法和final类,内部类
- 基本组件之列表视图
- MARK
- hdoj1285 确定比赛名次 (经典拓扑排序) 初学拓扑排序.做
- PLY文件
- coco2dx新建项目报错,ld: -pie can only be used when targeting iOS 4.2 or later clang: error: linker command
- HDU--5389(01背包)
- CodeForces-556A Case of the Zeros and Ones
- 【初级】VS2015/MFC动态创建工具栏1(使用图标创建工具栏)
- hdu oj 2177 取(2堆)石子游戏
- 链接测试工具xenu link sleuth的使用
- coj 1111:三家人
- POJ 1458 Common Subsequence LCS