ZOJ 3228 Searching the String (AC自己主动机)
题目链接:Searching the String
解析:给一个长串。给n个不同种类的短串。问分别在能重叠下或者不能重叠下短串在长串中出现的次数。
能重叠的已经是最简单的AC自己主动机模板题了。
不能重叠的记录一下每一个匹配的串的起始位置保证不重叠就可以。
AC代码:
#include <bits/stdc++.h>
using namespace std; struct Trie{
int next[600010][26], fail[600010], deep[600010];
int root, L; int newnode(){
for(int i = 0; i < 26; i++) next[L][i] = -1;
L ++;
return L-1;
} void init(){
L = 0;
root = newnode();
deep[root] = 0;
} int insert(char buf[]){
int len = strlen(buf);
int now = root;
for(int i = 0; i < len; i++){
if(next[now][ buf[i] - 'a' ] == -1){
next[now][ buf[i] - 'a' ] = newnode();
deep[next[now][buf[i] - 'a']] = i+1;
}
now = next[now][ buf[i] - 'a' ];
}
return now;
} void build(){
queue<int> Q;
fail[root] = root;
for(int i = 0; i < 26; 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 < 26; 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 cnt[600010][2]; //cnt[][0]表示能重叠的,cnt[][1]表示不能重叠的
int last[600010]; //上次匹配的字符
void query(char buf[]){
memset(cnt, 0, sizeof(cnt));
memset(last, -1, sizeof(last));
int len = strlen(buf);
int now = root;
for(int i = 0; i < len; i++){
now = next[now][ buf[i] - 'a' ];
int temp = now;
while(temp != root){
cnt[temp][0] ++;
if(i - last[temp] >= deep[temp]){ //保证不重叠
last[temp] = i;
cnt[temp][1] ++;
}
temp = fail[temp];
}
}
}
}; char buf[20];
char str[100010];
int typ[100010], pos[100010];
Trie ac; int main(){
#ifdef sxk
freopen("in.txt", "r", stdin);
#endif //sxk int n;
int kase = 0;
while(scanf("%s", str) == 1){
scanf("%d", &n);
ac.init();
for(int i = 0; i < n; i++){
scanf("%d%s", &typ[i], buf);
pos[i] = ac.insert(buf);
}
ac.build();
ac.query(str);
printf("Case %d\n", ++kase);
for(int i=0; i<n; i++)
printf("%d\n", ac.cnt[pos[i]][typ[i]]);
printf("\n");
}
return 0;
}
ZOJ 3228 Searching the String (AC自己主动机)的更多相关文章
- ZOJ - 3228 Searching the String (AC自己主动机)
Description Little jay really hates to deal with string. But moondy likes it very much, and she's so ...
- ZOJ 3228 Searching the String(AC自动机)
Searching the String Time Limit: 7 Seconds Memory Limit: 129872 KB Little jay really hates to d ...
- ZOJ 3494 BCD Code (AC自己主动机 + 数位DP)
题目链接:BCD Code 解析:n个病毒串.问给定区间上有多少个转换成BCD码后不包括病毒串的数. 很奇妙的题目. . 经典的 AC自己主动机 + 数位DP 的题目. 首先使用AC自己主动机,得到b ...
- zoj 3228:Searching the String
Description Little jay really hates to deal with string. But moondy likes it very much, and she's so ...
- Zoj 3545 Rescue the Rabbit(ac自己主动机+dp)
标题效果: 鉴于DNA有一个正确的顺序值.请构造一个长度I的DNA在这个序列使DNA正确的顺序值极大.它被认为是负的输出噼啪. .. IDEAS: 施工顺序是,ac己主动机上走,求最大要用到dp dp ...
- zoj 3430 Detect the Virus(AC自己主动机)
题目连接:zoj 3430 Detect the Virus 题目大意:给定一个编码完的串,将每个字符相应着表的数值转换成6位二进制.然后以8为一个数值,又一次形成字符 串,推断给定询问串是否含有字符 ...
- zoj 3430 Detect the Virus(AC自己主动机)
Detect the Virus Time Limit: 2 Seconds Memory Limit: 65536 KB One day, Nobita found that his co ...
- AC自己主动机
AC自己主动机 AC自己主动机是KMP和Trie的结合,主要处理多模板串匹配问题.以下推荐一个博客,有助于学习AC自己主动机. NOTONLYSUCCESS 这里另一个Kuangbin开的比赛,大家 ...
- POJ 2778 DNA Sequence (AC自己主动机 + dp)
DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...
随机推荐
- Ubuntu中python链接本地数据库
由于python链接数据库需要下载DB API模块:例如你需要访问Mysql数据,你需要MySQL数据库模块. DB-API是一个规范. 以便为不同的底层数据库系统和数据库接口程序提供一致的访问接口. ...
- python计算圆面积
#coding=gbk #coding=utf-8 #-*- coding: UTF-8 -*- #调用math包处理相关的运算 import math #圆半径 r = 2 #计算圆面积π*r*r与 ...
- Python之微信-微信好友头像合成
仔细看下图,你的头像就藏在里面哦!!! 有没有犯密集恐惧症?这并不震撼,如果你有 5000 位好友的话,做出来的图看着会更刺激些. 看完了图,你可能想知道这个图咋做出来的,不会是我闲着无聊把把好友头像 ...
- LeetCode (17)Letter Combinations of a Phone Number
题目 Given a digit string, return all possible letter combinations that the number could represent. A ...
- 电源模块PCB设计
电源模块的PCB设计 电源电路是一个电子产品的重要组成部分,电源电路设计的好坏,直接牵连产品性能的好坏.我们电子产品的电源电路主要有线性电源和高频开关电源.从理论上讲,线性电源是用户需要多少电流,输入 ...
- pip安装requests库失败
pip install 安装第三方插件是出现Could not fetch URL https://pypi.python.org/simple/pool/: There was a problem ...
- BigTable
Bigtable发布于2006年,启发了无数的NoSQL数据库,比如:Cassandra.HBase等等. Cassandra架构中有一半是模仿Bigtable,包括了数据模型.SSTables以及提 ...
- [Go]接口的运用
在Go语言中,不能通过调用new函数或make函数创建初一个接口类型的值,也无法用字面量来表示一个接口类型的值.可以通过关键字type和interface声明接口类型,接口类型的类型字面量与结构体类型 ...
- 【UTR #2】[UOJ#278]题目排列顺序 [UOJ#279]题目交流通道 [UOJ#280]题目难度提升
[UOJ#278][UTR #2]题目排列顺序 试题描述 “又要出题了.” 宇宙出题中心主任 —— 吉米多出题斯基,坐在办公桌前策划即将到来的 UOI. 这场比赛有 n 道题,吉米多出题斯基需要决定这 ...
- Codeforces 892 D.Gluttony
D. Gluttony time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...