codeforces 710f String Set Queries ac自动机 二进制分组
来源:互联网 发布:php判断是否为素数 编辑:程序博客网 时间:2024/06/06 12:50
题意:维护一个字符串集合,支持插入一个串,删除一个串,询问集合中的串在给出的串中出现多少次,一个串出现多次算多次。并且不会有两次插入的串相同。强制在线。
考虑没有删除的情况,将插入串的个数二进制拆分,对于每
这样共有
。插入时如果当前ac自动机中串的个数和上一个个数相同,那么合并两个自动机。每个字符串只会被合并
有删除时同样维护一个删除串的这样的东西,查询时减一下就行了。
#include <bits/stdc++.h>using namespace std;#define N 310000int m,cnt;char s[N];struct acauto{ int ch[N][27],fail[N],en[N],en1[N],cnt; int num[31],root[31],top; queue<int>que; void acmatch(int x) { que.push(x); while(!que.empty()) { int tmp=que.front();que.pop(); for(int i=1;i<=26;i++) if(ch[tmp][i]) { int t=fail[tmp],t1=ch[tmp][i]; while(t&&!ch[t][i])t=fail[t]; fail[t1]=t ? ch[t][i]:x; que.push(t1); en1[t1]=en1[fail[t1]]+en[t1]; } } } int merge(int x,int y) { if(!x||!y)return x+y; en[x]=en[x]|en[y]; for(int i=1;i<=26;i++) ch[x][i]=merge(ch[x][i],ch[y][i]); return x; } void insert() { scanf("%s",s+1);int len=strlen(s+1); root[++top]=++cnt; int now=root[top]; for(int i=1,t;i<=len;i++) { if(!ch[now][t=s[i]-'a'+1]) ch[now][t]=++cnt; now=ch[now][t]; } en[now]=1;num[top]=1; acmatch(root[top]); while(top&&num[top]==num[top-1]) { merge(root[top-1],root[top]); acmatch(root[top-1]); num[top-1]+=num[top];top--; } } int query(int len) { int ret=0; for(int i=1;i<=top;i++) { for(int now=root[i],j=1,t;j<=len;j++) { t=s[j]-'a'+1; while(now&&!ch[now][t]) now=fail[now]; now= now ? ch[now][t]:root[i]; ret+=en1[now]; } } return ret; }}ac1,ac2;int main(){ //freopen("tt.in","r",stdin); scanf("%d",&m); for(int t;m--;) { scanf("%d",&t); if(t==1)ac1.insert(); else if(t==2)ac2.insert(); else { scanf("%s",s+1);int len=strlen(s+1); printf("%d\n",ac1.query(len)-ac2.query(len)); fflush(stdout); } } return 0;
0 0
- codeforces 710f String Set Queries ac自动机 二进制分组
- [ AC自动机 二进制分组/阈值 ] HDU4787
- [二进制分组+AC自动机] HDU4787 GRE Words Revenge
- [二进制分组 AC自动机] HDU 4787 GRE Words Revenge
- [阈值 二进制分组 && AC自动机]HDU4787. GRE Words Revenge
- 【二进制分组+AC自动机】HDU4787[GRE Words Revenge]题解
- CodeForces - 697F Legen... AC自动机+快速幂
- ZOJ Problem Set - 3228 Searching the String AC自动机
- AC自动机+dp(CodeForces
- hdu6096 String AC自动机
- hdu6096 String【AC自动机】
- [Codeforces 697F] Legen... (AC自动机+取max的矩阵快速幂)
- Codeforces Round #362 (Div. 2) F. Legen... (AC自动机 + 矩阵快速幂)
- Codeforces Round #362 (Div. 2) F. Legen...(AC自动机+矩阵快速幂)
- 【AC自动机】 CodeForces - 163E
- CodeForces 86C AC自动机
- HDU 6096 (String) AC自动机
- hdu 6096 String(AC自动机)
- ModelAndView返回json类型数据
- NYOJ3533D dungeon
- unity 计算两点的的角度
- VAD算法
- 图的重要性质
- codeforces 710f String Set Queries ac自动机 二进制分组
- JAVA List 移除元素
- 我很累,但我会坚持
- CodeForces 140DNew Year Contest
- 【算法学习】高斯模糊算法
- Ubuntu15.04交叉编译opencv到ARM全过程
- 冒泡排序(Bubble Sort)——排序算法
- shell awk命令
- Linux运行service iptables save报错