Searching the String ZOJ - 3228 AC自动机查询升级版
题意:
先给你一个不超过1000000长度的大串s;接下来输入一个n代表接下来输入的小串个数,小串长度不超过6。
小串分两种类型0和1类型。
0类型表示小串在大串中的最大匹配个数就是常规的AC自动机的做法。
1类型表示小串在大串中不能重合的最大匹配数。
依次输出结果.(所有的串只包含小写字母)
按样例输出,注意每组测试数据后有一个换行。
题意我不想写了抄的,抄这里的 (不好意思啦)
0 类型的就是最开始的模板题
1 类型的处理方式就是,在建立字典树的时候弄一个dep数组,记录每一个节点的深度
然后在query的过程中查询上一个单词最后一个字母出现的位置。
if (i - last[temp] >= dep[temp]) ans[temp][1]++, last[temp] = i;
这样就可以了。
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm> #define pi acos(-1.0)
#define eps 1e-9
#define fi first
#define se second
#define rtl rt<<1
#define rtr rt<<1|1
#define bug printf("******\n")
#define mem(a, b) memset(a,b,sizeof(a))
#define name2str(x) #x
#define fuck(x) cout<<#x" = "<<x<<endl
#define sfi(a) scanf("%d", &a)
#define sffi(a, b) scanf("%d %d", &a, &b)
#define sfffi(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define sffffi(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define sfL(a) scanf("%lld", &a)
#define sffL(a, b) scanf("%lld %lld", &a, &b)
#define sfffL(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define sffffL(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d)
#define sfs(a) scanf("%s", a)
#define sffs(a, b) scanf("%s %s", a, b)
#define sfffs(a, b, c) scanf("%s %s %s", a, b, c)
#define sffffs(a, b, c, d) scanf("%s %s %s %s", a, b,c, d)
#define FIN freopen("../date.txt","r",stdin)
#define gcd(a, b) __gcd(a,b)
#define lowbit(x) x&-x
#define IO iOS::sync_with_stdio(false) using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const ULL seed = ;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int maxn = 1e5 + ;
const int maxm = 8e6 + ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ; int n, vis[maxn], last[*maxn], ans[*maxn][], pos[maxn], dep[*maxn];
char buf[maxn], str[maxn][]; struct Aho_Corasick {
int next[][], fail[], End[];
int root, cnt; int newnode() {
for (int i = ; i < ; i++) next[cnt][i] = -;
End[cnt++] = ;
return cnt - ;
} void init() {
cnt = ;
root = newnode();
dep[root]=;
} int insert(char buf[]) {
int len = strlen(buf);
int now = root;
for (int i = ; i < len; i++) {
if (next[now][buf[i] - 'a'] == -) {
next[now][buf[i] - 'a'] = newnode();
dep[next[now][buf[i] - 'a']] = i + ;
}
now = next[now][buf[i] - 'a'];
}
End[now] = ;
return now;
} void build() {
queue<int> Q;
fail[root] = root;
for (int i = ; i < ; i++)
if (next[root][i] == -) 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 = ; i < ; i++)
if (next[now][i] == -) next[now][i] = next[fail[now]][i];
else {
fail[next[now][i]] = next[fail[now]][i];
Q.push(next[now][i]);
}
}
} void query(char buf[]) {
int len = strlen(buf);
int now = root;
mem(ans,);
mem(last, -);
for (int i = ; i < len; i++) {
now = next[now][buf[i] - 'a'];
int temp = now;
while (temp != root) {
ans[temp][]++;
if (i - last[temp] >= dep[temp]) ans[temp][]++, last[temp] = i;
temp = fail[temp];
}
}
} void debug() {
for (int i = ; i < cnt; i++) {
printf("id = %3d,fail = %3d,end = %3d,chi = [", i, fail[i], End[i]);
for (int j = ; j < ; j++) printf("%2d", next[i][j]);
printf("]\n");
}
}
} ac; int main() {
// FIN;
int cas = ;
while (~sfs(buf)) {
ac.init();
sfi(n);
for (int i = ; i < n; ++i) {
scanf("%d%s", &vis[i], str[i]);
pos[i] = ac.insert(str[i]);
}
ac.build();
ac.query(buf);
printf("Case %d\n", cas++);
for (int i = ; i < n; ++i) printf("%d\n", ans[pos[i]][vis[i]]);
printf("\n");
}
return ;
}
Searching the String ZOJ - 3228 AC自动机查询升级版的更多相关文章
- Searching the String - ZOJ 3228(ac自动机)
题目大意:首先给你一下母串,长度不超过10^5,然后有 N(10^5) 次查询,每次查询有两种命令,0或者1,然后加一个子串,询问母串里面有多少个子串,0表示可以重复,1表示不可以重复. 分析:发 ...
- zoj3228 Searching the String AC自动机查询目标串中模式串出现次数(分可覆盖,不可覆盖两种情况)
/** 题目:zoj3228 Searching the String 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=34 ...
- HDU - 2222,HDU - 2896,HDU - 3065,ZOJ - 3430 AC自动机求文本串和模式串信息(模板题)
最近正在学AC自动机,按照惯例需要刷一套kuangbin的AC自动机专题巩固 在网上看过很多模板,感觉kuangbin大神的模板最为简洁,于是就选择了用kuangbin大神的模板. AC自动机其实就是 ...
- ZOJ 3494 (AC自动机+高精度数位DP)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3494 题目大意:给定一些被禁止的BCD码.问指定范围内不含有 ...
- Detect the Virus ZOJ - 3430 AC自动机
One day, Nobita found that his computer is extremely slow. After several hours' work, he finally fou ...
- BCD Code ZOJ - 3494 AC自动机+数位DP
题意: 问A到B之间的所有整数,转换成BCD Code后, 有多少个不包含属于给定病毒串集合的子串,A,B <=10^200,病毒串总长度<= 2000. BCD码这个在数字电路课上讲了, ...
- ZOJ - 3430 ac自动机
这题主要就是解码过程很恶心,不能用char存,一共wa了20发 题意:先给n串加密后的字符,然后m串加密后的字符,解码之后求n对应每个m的匹配数,很显然的ac自动机 加密过程是先用对应ascii表的标 ...
- ZOJ3784 String of Infinity(AC自动机&&强连通分量)
题意:给你n个禁止串,然后你只能用字符表的前m个字符去写一个无限长的串,要求是不能包含禁止串,而且串在后面不能出现循环 比赛的时候想的是先建一个自动机,然后将自动机确定化,不能到达的状态全部弄出来.但 ...
- LA 4670 Dominating Patterns (AC自动机)
题意:给定n个字符串和一个文本串,查找哪个字符串出现的次数的最多. 析:一匹配多,很明显是AC自动机.只需要对原来的进行修改一下,就可以得到这个题的答案, 计算过程中,要更新次数,并且要映射字符串.如 ...
随机推荐
- 树形dp换根,求切断任意边形成的两个子树的直径——hdu6686
换根dp就是先任取一点为根,预处理出一些信息,然后在第二次dfs过程中进行状态的转移处理 本题难点在于任意割断一条边,求出剩下两棵子树的直径: 设割断的边为(u,v),设down[v]为以v为根的子树 ...
- python从入门到大神---1、初始化实例、类属性、方法
python从入门到大神---1.初始化实例.类属性.方法 一.总结 一句话总结: 方法不加括号是代码段:感觉python方法和js,php很类似,不加括号是代码段,加括号变成方法,比如f,f() 1 ...
- web应用本质
web应用的本质 在之前学习的socket网络编程中,是基于: 架构:C/S架构 协议:TCP/UDP协议 运行在OSI七层模型中的传输层 那么在web应用中,是基于: 架构:B/S架构 协议:Htt ...
- The packaging for this project did not assign a file to the build artifact
当进行mvn install时,遇到以下错误 The packaging for this project did not assign a file to the build artifact 在网 ...
- POJ 3761 Bubble Sort
题目链接:https://vjudge.net/problem/POJ-3761 转自:https://blog.csdn.net/cscj2010/article/details/7820906 题 ...
- java进行微信h5支付开发
最近在做微信支付开发用的框架是 srpingMVC mybatis spring 下面是开发流程图 我们只需要开发红色标记的模块就可以了. 具体参数详情可以查看微信开发者文档. 新手第一次写,写的不好 ...
- Opencv稍微高级点的鼠标事件-OpenCV步步精深
今天我们要来点稍微高级的东西.在我们按下鼠标时可以画矩形,而我们按下键盘m键时,切换到画圆的模式,再按下m键,回到画矩形模式. 一起来写下代码,首先当然还是调用库 import cv2 import ...
- mysql之MHA、Mycat综合分析
一.简介 MHA: 你可以把它看做是一个监控MySQL的工具,当master挂了之后,起一个slave作为master,另外一台slave重新作为新master的备库: 所以MHA的架构做好是三台数 ...
- 【第五周读书笔记】我是一只IT小小鸟
读了第一个同学的自述,我印象最深的就是一些高分同学,只是机械性地背诵知识点,然后不停刷题,只是为了拿一个高分,然而他们对学科的一些基本概念都没有掌握牢靠.高分,并不代表学的就好.学得好不仅仅要牢靠掌握 ...
- nodejs http ejs
// ejs-demo.jsvar http = require('http'); var ejs = require('ejs'); var url = require('url'); // 搭建 ...