/**
题目: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自动机查询目标串中模式串出现次数(分可覆盖,不可覆盖两种情况)的更多相关文章

  1. ZOJ3228 Searching the String —— AC自动机 + 可重叠/不可重叠

    题目链接:https://vjudge.net/problem/ZOJ-3228 Searching the String Time Limit: 7 Seconds      Memory Limi ...

  2. ZOJ3228 - Searching the String(AC自动机)

    题目大意 给定一个文本串,接下来有n个模式串,每次查询模式串出现的次数,查询分两种,可重叠和不可重叠 题解 第一次是把AC自动机构造好,跑n次,统计出每个模式串出现的次数,交上去果断TLE...后来想 ...

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

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

  4. ZOJ3228 Searching the String (AC自动机)

    Searching the String Time Limit: 7 Seconds                                      Memory Limit: 129872 ...

  5. 2017多校第6场 HDU 6096 String AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6096 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...

  6. HDU - 2222,HDU - 2896,HDU - 3065,ZOJ - 3430 AC自动机求文本串和模式串信息(模板题)

    最近正在学AC自动机,按照惯例需要刷一套kuangbin的AC自动机专题巩固 在网上看过很多模板,感觉kuangbin大神的模板最为简洁,于是就选择了用kuangbin大神的模板. AC自动机其实就是 ...

  7. sql 查询目标数据库中所有的表以其关键信息

    1.查询目标库中的所有表 SELECT obj.name tablename, ---表名 schem.name schemname, ---表所属的方案 idx.rows, ---一共有几行数组 C ...

  8. Searching the String ZOJ - 3228 AC自动机查询升级版

    题意:先给你一个不超过1000000长度的大串s:接下来输入一个n代表接下来输入的小串个数,小串长度不超过6. 小串分两种类型0和1类型. 0类型表示小串在大串中的最大匹配个数就是常规的AC自动机的做 ...

  9. 【AC自动机】zoj3228 Searching the String

    对所有模式串建立AC自动机. 每个单词结点要记录该单词长度. 然后在跑匹配的时候,对每个单词结点再处理3个值,代表可重叠的匹配次数,不可重叠的匹配次数,以及“上一次不可重叠的匹配位置”,这样结合单词长 ...

随机推荐

  1. Bugtags 让你的 APP 测试轻松、上线安心

    Bug 管理系统再进化 Bugtags 的创业团队,在过去几年,做了很多方向的尝试——没错,是开发了很多 APP. 每一轮迭代,都会被繁琐的 APP 测试困扰:无休止的截屏上传电脑,无数次的开发与测试 ...

  2. 手机web——自适应网页设计(html/css控制)(转)

    一. 允许网页宽度自动调整: "自适应网页设计"到底是怎么做到的?其实并不难. 首先,在网页代码的头部,加入一行viewport元标签. <meta name="v ...

  3. Eclipse折叠代码 coffee bytes code folding

    提供一个插件下载地址,博客园的: http://files.cnblogs.com/wucg/com.cb.eclipse.folding_1.0.6.jar.zip  将下载的zip文件解压出来的j ...

  4. X-Forwarded-For的一些理解

    X-Forwarded-For 是一个 HTTP 扩展头部,主要是为了让 Web 服务器获取访问用户的真实 IP 地址(其实这个真实未必是真实的,后面会说到). 那为什么 Web 服务器只有通过 X- ...

  5. OGG_GoldenGate安装和环境搭建(案例)

    2014-03-02 Created By BaoXinjian

  6. RCU介绍

    RCU原理: RCU(Read-Copy Update),顾名思义就是读-拷贝修改,它是基于其原理命名的.对于被RCU保护的共享数据结构,读者不需要获得任何锁就可以访问它,但写者在访问它时首先拷贝一个 ...

  7. Go TCP网路程序编写

    client和server程序编写 面向长连接的编程 http://files.cnblogs.com/files/yyx1-1/Go_TCP.7z

  8. 锻造完美U盘小偷:活用消息机制

    锻造完美U盘小偷:活用消息机制作者:灰狐来源:灰狐's Blog 注:本文已发表在<黑客防线>2008年第1期,转载请注明出处. 以前经常看到有人做出一些蛮有意思的小工具,其中最多的似乎就 ...

  9. POJ 3670 Eating Together 二分解法O(nlgn)和O(n)算法

    本题就是一题LIS(最长递增子序列)的问题.本题要求求最长递增子序列和最长递减子序列. dp的解法是O(n*n),这个应该大家都知道.只是本题应该超时了. 由于有O(nlgn)的解法. 可是因为本题的 ...

  10. 从错误中学python(4)——最小公约数与辗转相除法

    题目 给你两个正整数a和b, 输出它们的最大公约数 辗转相除法 辗转相除法的步骤 def gcd(b,a): b,a=a,b%a if a==0: return b else: return gcd( ...