题目链接: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自己主动机)的更多相关文章

  1. 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 ...

  2. ZOJ 3228 Searching the String(AC自动机)

    Searching the String Time Limit: 7 Seconds      Memory Limit: 129872 KB Little jay really hates to d ...

  3. ZOJ 3494 BCD Code (AC自己主动机 + 数位DP)

    题目链接:BCD Code 解析:n个病毒串.问给定区间上有多少个转换成BCD码后不包括病毒串的数. 很奇妙的题目. . 经典的 AC自己主动机 + 数位DP 的题目. 首先使用AC自己主动机,得到b ...

  4. zoj 3228:Searching the String

    Description Little jay really hates to deal with string. But moondy likes it very much, and she's so ...

  5. Zoj 3545 Rescue the Rabbit(ac自己主动机+dp)

    标题效果: 鉴于DNA有一个正确的顺序值.请构造一个长度I的DNA在这个序列使DNA正确的顺序值极大.它被认为是负的输出噼啪. .. IDEAS: 施工顺序是,ac己主动机上走,求最大要用到dp dp ...

  6. zoj 3430 Detect the Virus(AC自己主动机)

    题目连接:zoj 3430 Detect the Virus 题目大意:给定一个编码完的串,将每个字符相应着表的数值转换成6位二进制.然后以8为一个数值,又一次形成字符 串,推断给定询问串是否含有字符 ...

  7. zoj 3430 Detect the Virus(AC自己主动机)

    Detect the Virus Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita found that his co ...

  8. AC自己主动机

    AC自己主动机 AC自己主动机是KMP和Trie的结合,主要处理多模板串匹配问题.以下推荐一个博客,有助于学习AC自己主动机. NOTONLYSUCCESS  这里另一个Kuangbin开的比赛,大家 ...

  9. POJ 2778 DNA Sequence (AC自己主动机 + dp)

    DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...

随机推荐

  1. 从零开始--系统深入学习Android

    http://www.cnblogs.com/tianjian/category/354587.html

  2. 从多表连接后的select count(*)看待SQL优化

    从多表连接后的select count(*)看待SQL优化 一朋友问我,以下这SQL能直接改写成select count(*) from a吗? SELECT COUNT(*) FROM a LEFT ...

  3. docker-ce快速部署

    配置yum源wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repowget -O ...

  4. Linux文件查找find和locate

    目 录 第1章 locate文件查找    1 1.1 概述    1 1.2 locate文件查找的特性    1 第2章 文件查找概述    1 第3章    1 3.1 文件名查找    1 3 ...

  5. cxLookupCombobox的多字段模糊匹配

    查了网上很多资料,懒人输入:通过程序使用过滤对话达到自己的目的: 用到cxFilter单元: cbb_DoctorOrder.Properties.View.DataController.Filter ...

  6. tarjan 学习记

    1.强连通分量是什么 强连通分量指的是部分点的集合如果能够互相到达(例如 1→3,3→2,2→1(有向图)这种,132每个点都能互相抵达) 或者说,有一个环,环上点的集合就是一个强连通分量 2.那怎么 ...

  7. PHP加速之eaccelerator

    eaccelerator简介: eAccelerator是一个自由开放源码php加速器,优化和动态内容缓存,提高了php脚本的缓存性能,使得PHP脚本在编译的状态下,对服务器的开销几乎完全消除. 它还 ...

  8. 【二分贪心+精度问题】F. Pie

    https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/F [题意] 给定n个已知半径的披萨,有m个人要分这n个披萨 要求每个人分到的面积 ...

  9. poj 3678 XOR和OR和AND(简单2-sat问题)

    /* 题意:给你一些边,每条边有一个值和一个运算符XOR OR AND求是否存在一些点使得所有的边根据这些运算符 可以符合条件的权值. 建边方式参考:http://blog.csdn.net/shua ...

  10. hdu 4790 数学

    /* 题意:给你二个区间[a,b]和[c,d] 分别从中选一个数x和y使的(x+y)%p=m; 可以这样来求,先求出(0->b和0->d区间段的值)-(区间0->a-1和0-> ...