zoj3228 Searching the String AC自动机查询目标串中模式串出现次数(分可覆盖,不可覆盖两种情况)
/**
题目:zoj3228 Searching the String
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3441
题意:给定一个长度为N(N <= 105)的目标串,然后再给定M(M <= 105)个长度不大于6的字符串, 问这些字符串在目标串的出现次数(分可重叠和不可重叠两种)。 题解:可以覆盖情况下,直接建立自动机求次数。注意可能出现类型相同以及字符串相同。所以用map标记; 不可以覆盖情况下,直接建立自动机,查询的时候维护当前查到的字符串上一次找到的位置lastpos. 如果lastpos+该子串长度<=pos那么可以ans++,以及更新lastpos=pos; find(),find2()两个函数分别处理可覆盖,不可覆盖情况。先统一处理可覆盖,然后清空自动机重新构建不可覆盖情况下的自动机。 AC自动机好文章:http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html
*/ //#include<bits/stdc++.h>
#include<cstring>
#include<cstdio>
#include<iostream>
#include<map>
#include<algorithm>
#include<queue>
using namespace std;
#define P pair<int,int>
#define ms(x,y) memset(x,y,sizeof x)
#define LL long long
const int maxn = ;
const int mod = 1e9+;
const int maxnode = *+;
const int sigma_size = ;
map<string,int> mp1, mp2;
struct node
{
char s[];
int type;
int len;
int ans;
int lastpos;
}t[];
struct AhoCorasickAutomata
{
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
int f[maxnode];
int last[maxnode];
void clear(){sz = ; memset(ch[],,sizeof ch[]); }
int idx(char c){return c-'a'; } void insert(char *s,int x)
{
int u = , n = strlen(s);
for(int i = ; i < n; i++){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[sz], , sizeof ch[sz]);
val[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = x;
} void find(char* T){
int j = ;
for(int i = ; T[i]!='\0'; i++){
int c = idx(T[i]);
j = ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]);
}
} void print(int j)
{
if(j){
//cnt[val[j]]++;
t[val[j]].ans++;
print(last[j]);
}
} void find2(char* T){///不可覆盖情况下;
int j = ;
for(int i = ; T[i]!='\0'; i++){
int c = idx(T[i]);
j = ch[j][c];
if(val[j]) print2(j,i);
else if(last[j]) print2(last[j],i);
}
} void print2(int j,int pos)
{
if(j){
//cnt[val[j]]++;
if(t[val[j]].lastpos+t[val[j]].len<=pos){
t[val[j]].ans++;
t[val[j]].lastpos = pos;
}
print2(last[j],pos);
}
} void getFail(){
queue<int> q;
f[] = ;
for(int c = ; c < sigma_size; c++){
int u = ch[][c];
if(u){f[u] = ; q.push(u); last[u] = ;}
} while(!q.empty()){
int r = q.front(); q.pop();
for(int c = ; c < sigma_size; c++){
int u = ch[r][c];
if(!u){
ch[r][c] = ch[f[r]][c]; continue;
}//if(!u) continue;
q.push(u);
int v = f[r];
while(v&&!ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
} } ac;
char s[];
int main()
{
int cas = ;
while(scanf("%s",s)==)
{
int n;
scanf("%d",&n);
ac.clear();
mp1.clear();
mp2.clear();
for(int i = ; i <= n; i++){
scanf("%d%s",&t[i].type,t[i].s);
t[i].ans = ;
if(t[i].type==){
mp1[string(t[i].s)] = i;
ac.insert(t[i].s,i);
}
}
ac.getFail();
ac.find(s);
ac.clear();
mp2.clear();
for(int i = ; i <= n; i++){
if(t[i].type){
t[i].len = strlen(t[i].s);
t[i].lastpos = -;
mp2[string(t[i].s)] = i;
ac.insert(t[i].s,i);
}
}
ac.getFail();
ac.find2(s);
printf("Case %d\n",cas++);
for(int i = ; i <= n; i++){
if(t[i].type){
printf("%d\n",t[mp2[t[i].s]].ans);
}else
{
printf("%d\n",t[mp1[t[i].s]].ans);
}
}
printf("\n");
}
return ;
} /* */
zoj3228 Searching the String AC自动机查询目标串中模式串出现次数(分可覆盖,不可覆盖两种情况)的更多相关文章
- ZOJ3228 Searching the String —— AC自动机 + 可重叠/不可重叠
题目链接:https://vjudge.net/problem/ZOJ-3228 Searching the String Time Limit: 7 Seconds Memory Limi ...
- ZOJ3228 - Searching the String(AC自动机)
题目大意 给定一个文本串,接下来有n个模式串,每次查询模式串出现的次数,查询分两种,可重叠和不可重叠 题解 第一次是把AC自动机构造好,跑n次,统计出每个模式串出现的次数,交上去果断TLE...后来想 ...
- ZOJ 3228 Searching the String(AC自动机)
Searching the String Time Limit: 7 Seconds Memory Limit: 129872 KB Little jay really hates to d ...
- ZOJ3228 Searching the String (AC自动机)
Searching the String Time Limit: 7 Seconds Memory Limit: 129872 ...
- 2017多校第6场 HDU 6096 String AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6096 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...
- HDU - 2222,HDU - 2896,HDU - 3065,ZOJ - 3430 AC自动机求文本串和模式串信息(模板题)
最近正在学AC自动机,按照惯例需要刷一套kuangbin的AC自动机专题巩固 在网上看过很多模板,感觉kuangbin大神的模板最为简洁,于是就选择了用kuangbin大神的模板. AC自动机其实就是 ...
- sql 查询目标数据库中所有的表以其关键信息
1.查询目标库中的所有表 SELECT obj.name tablename, ---表名 schem.name schemname, ---表所属的方案 idx.rows, ---一共有几行数组 C ...
- Searching the String ZOJ - 3228 AC自动机查询升级版
题意:先给你一个不超过1000000长度的大串s:接下来输入一个n代表接下来输入的小串个数,小串长度不超过6. 小串分两种类型0和1类型. 0类型表示小串在大串中的最大匹配个数就是常规的AC自动机的做 ...
- 【AC自动机】zoj3228 Searching the String
对所有模式串建立AC自动机. 每个单词结点要记录该单词长度. 然后在跑匹配的时候,对每个单词结点再处理3个值,代表可重叠的匹配次数,不可重叠的匹配次数,以及“上一次不可重叠的匹配位置”,这样结合单词长 ...
随机推荐
- HDUOJ----1250 Hat's Fibonacci
Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- PAT 1086 Tree Traversals Again
PAT 1086 Tree Traversals Again 题目: An inorder binary tree traversal can be implemented in a non-recu ...
- Python 的 if __name__ == '__main__'
Python 文件 最后部分会有: if __name__ == '__main__': TestRLSO()……………… 1)首先,这是一个判断语句. 表示执行的是此代码所在的文件.如果这个文件是作 ...
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- Android开发学习之3大类菜单
在Android系统中,菜单可以分为三类:选项菜单(Option Menu),上下文菜单(Context Menu)以及子菜单(Sub Menu). 一.选项菜单(Option Menu) 创建选项菜 ...
- iOS - UnitTests 单元测试
1.UnitTests 在计算机编程中,单元测试(又称为模块测试, Unit Testing)是针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作.程序单元是应用的最小可测试部件.在过程化编 ...
- asp mvc @Html.CheckBox("sel",true) 往后台传值问题
@Html.CheckBox("sel",true) 生成2个输入,而不是一个,这是为什么呢? <input checked="checked" id=& ...
- 在linux下导入.sql文件,数据库中文乱码
现象描述 我是在aix下面导入如下SQL语句时,数据库中显示乱码. insert into CONFERENCE(CONFERENCEID,SUBCONFERENCEID,ACCESSNUMBER,A ...
- 机器学习(4): KNN 算法
1. 综述 1.1 Cover和Hart在1968年提出了最初的邻近算法 1.2 分类(classification)算法 1.3 输入基于实例的学习(instance- ...
- Android Spans介绍(转)
在做一个 TODO 类的软件,使用开源软件 MaterialCalendarView 的时候,发现了需要往日历中添加待办事宜的标记,用到了 MaterialCalendarView 中的 DotSpa ...