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 ...
随机推荐
- ZOJ - 1655 Transport Goods(单源最长路+迪杰斯特拉算法)
题目: 有N-1个城市给首都(第N个城市)支援物资,有M条路,走每条路要耗费一定百分比(相对于这条路的起点的物资)的物资.问给定N-1个城市将要提供的物资,和每条路的消耗百分比.求能送到首都的最多的物 ...
- 提高CPU使用率
某些特殊时候,需要提升下cpu的利用率,此时……………………需要一个极其简单的脚本来完成! #!/bin/bash while (true);do { for i in $(seq 100000 10 ...
- 条款27:尽量少做转型动作(Minimize casting)
NOTE : 1.如果可以,尽量避免转型,特别是在注重效率的代码中避免dynamic_casts. 如果有个设计需要转型动作,试着发展无需转型的替代设计. 2.如果转型是必须要的,试着将它隐藏于某个函 ...
- MySQL账户管理和主从同步
账户管理 在生产环境下操作数据库时,绝对不可以使用root账户连接,而是创建特定的账户,授予这个账户特定 的操作权限,然后连接进行操作,主要的操作就是数据的CRUD(增删改查) MySQL账户体系:根 ...
- Spring入门(1)——搭建简单的环境
步骤: 1.下载spring框架开发包. 2.创建web项目并引入开发包. 3.创建java接口和相应的实现类. 4.编写spring的配置文件. 5.编写 测试类,并进行测试. 1.下载spring ...
- [bzoj1022][SHOI2008]小约翰的游戏John (反Nim游戏)
Description 小约翰经常和他的哥哥玩一个非常有趣的游戏:桌子上有n堆石子,小约翰和他的哥哥轮流取石子,每个人取 的时候,可以随意选择一堆石子,在这堆石子中取走任意多的石子,但不能一粒石子也不 ...
- Laravel(4.2)-->whereHas/ whereDoesntHave
在开发过程中,有时间需要用 wherehas 联合查询 出想要的结果,但是有的时候想搜索出不在关联表中出现的数据 whereDoesntHave(例:搜索出开卡的用户和没有开卡的用户)if($is_o ...
- mysql异常Incorrect string value: '\xE6\xB5\x8B\xE8\xAF\x95' for column 'region_name'
Incorrect string value: '\xE6\xB5\x8B\xE8\xAF\x95' for column 'region_name' insert语句加的该字段有汉字,乱码造成的 解 ...
- https://www.cnblogs.com/freeflying/p/9950374.html
https://www.cnblogs.com/freeflying/p/9950374.html
- Ubuntu 16.04安装JDK7/JDK8的两种方式
ubuntu 安装jdk 的两种方式:1:通过ppa(源) 方式安装. 2:通过官网下载安装包安装. 这里推荐第1种,因为可以通过 apt-get upgrade 方式方便获得jdk的升级 使用ppa ...