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 ...
随机推荐
- pm2 start命令中的json格式详解
pm2 start npm -- start这条命令是pm2的万能命令,pm2 start <json>,就是这一系列命令中的最豪华命令.这个json我们可以理解为一个任务参数描述文件.通 ...
- Word转html并移植到web项目
1.打开对应word文件 建议使用web视图查看文档 这样可以提前预览转转成html样式 2.如果有图片修改图片大小及格式 在web视图下,把图片调制适当大小,不然导出的html可能图片较小 3.点击 ...
- bzoj 3555 企鹅QQ
https://www.lydsy.com/JudgeOnline/problem.php?id=3555 枚举每一位字符,计算字符两侧的哈希值,然后进行比较,用map或排序记录出与其相同的字符串数量 ...
- Linux组和提权
目 录 第1章 组命名管理** 1 1.1 group组信息和密码信息 1 1.1.1 /etc/group 组账户信息 1 1.1.2 /etc/gshadow 组密码信息 ...
- 基于tiny4412的u-boot移植(二)(转)
http://www.cnblogs.com/pengdonglin137/archive/2015/12/27/5080645.html
- 昨天去面试,这5个Python面试题都被考到了,Python面试题No6
第1题:字符串的拼接–如何高效的拼接两个字符串? 字符串拼接的几种方法 加号 逗号 直接连接 格式化 join 多行字符串拼接() 加号 print('Python' + 'Plus') 逗号 pri ...
- 【HIHOCODER 1163】 博弈游戏·Nim游戏
描述 今天我们要认识一对新朋友,Alice与Bob. Alice与Bob总是在进行各种各样的比试,今天他们在玩一个取石子的游戏. 在这个游戏中,Alice和Bob放置了N堆不同的石子,编号1..N,第 ...
- 【HDU 5934】Bomb(强连通缩点)
Problem Description There are N bombs needing exploding. Each bomb has three attributes: exploding r ...
- shell实现ftp命令示例
一.shell脚本示例: [plain] view plaincopy cd /PATH_YOU_WANT_TO_UPLOAD(DOWNLOAD) ftp -niv <<- EOF ope ...
- 集合框架学习之List接口
Java语言的java.util包中提供了一些集合类,这些集合类又被称为容器.用来完善数组的不足之处.集合类与数组的不同之处是,数组的长度是固定的,集合的长度是可变的:数组用来存放基本类型的数据,集合 ...