题目链接: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. 并发3-Volatile

    Volatile关键字实现原理 1.认识volatile关键字 程序举例 用一个线程读数据,一个线程改数据 存在数据的不一致性 2.机器硬件CPU与JMM (1)CPU Cache模 (2)CPU缓存 ...

  2. 微信小程序工具真机调试提示page "xxx/xxx/xxx" is not found

    解决方法: pages对象添加该页面

  3. 在ios中使用FMDB

    SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库.iOS SDK很早就支持了SQLite,在使用时,只需要加入 libsqlite3.dyli ...

  4. 使用Eclipse中的反编译插件jadClipse查看Class源码

    功安装完插件jadClipse 之后便可以查看源码class文件了 但是对于自己代码的class文件,直接复制过来却看不到,需要以下操作. 将此文件以及文件夹直接拷贝到Eclipse中发现 右击项目- ...

  5. json_decode 解析带BOM头文件错误

    //取前三个字符 并转化为ASCII 判断是否为BOM文件 $charset[1] = substr($result, 0, 1); $charset[2] = substr($result, 1, ...

  6. 《算法导论》— Chapter 6 堆排序

    序 本文主要介绍堆排序算法(HeapSort),堆排序像合并排序而不像插入排序,堆排序的运行时间为O(nlgn):像插入排序而不像合并排序,它是一种原地(in place)排序算法.在任何时候,数组中 ...

  7. [WPF自定义控件]使用WindowChrome自定义Window Style

    1. 为什么要自定义Window 对稍微有点规模的桌面软件来说自定义的Window几乎是标配了,一来设计师总是克制不住自己想想软件更个性化,为了UI的和谐修改Window也是必要的:二来多一行的空间可 ...

  8. 【01】魔芋使用MDN的一点点经验

    [01]魔芋使用MDN的一点点经验     1,MDN地址: https://developer.mozilla.org/en-US/(下图)   2,建议看英文原文.因为中文翻译落后,并且有些翻译并 ...

  9. BeautifulSoup实例

    Beautiful Soup 4.4.0 中文文档:http://beautifulsoup.readthedocs.io/zh_CN/latest/ #coding:utf-8from bs4 im ...

  10. Jmeter接口测试实战-Cookies

    场景: 接口测试时常都需要登录,请求方式(post), 登录常用的方法有通过获取token, 获取session, 获取cookie, 等等. 这几种都有一个共同的特点, 有效期(expires). ...