题目大意

给定一个文本串,接下来有n个模式串,每次查询模式串出现的次数,查询分两种,可重叠和不可重叠

题解

第一次是把AC自动机构造好,跑n次,统计出每个模式串出现的次数,交上去果断TLE。。。后来想想其实只要跑一次文本串即可。。。

这个题目主要的问题是要解决有可重叠和不可重叠两种情况,用一个数组记录下模式串上一次出现的位置就可以判断是否重叠了

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
#define MAXN 100005
const int maxnode=600005;
const int sigma_size=26;
char T[MAXN],s[7];
bool flag[MAXN];
int pos[MAXN];
struct Triegragh
{
int ch[maxnode][sigma_size];
int fail[maxnode];
int deep[maxnode];
int cnt[maxnode][2];
int end[maxnode];
int last[maxnode];
int val[maxnode];
int sz;
void init()
{
sz=1;
memset(ch[0],0,sizeof(ch[0]));
memset(cnt,0,sizeof(cnt));
memset(end,-1,sizeof(end));
memset(val,0,sizeof(val));
}
int idx(char c){return c-'a';}
int insert(char *s)
{
int u=0,n=strlen(s);
for(int i=0;i<n;i++)
{
int c=idx(s[i]);
if(!ch[u][c])
{
memset(ch[sz],0,sizeof(ch[sz]));
val[sz]=0;
deep[sz]=i+1;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]++;
return u;
}
void getfail()
{
queue<int>q;
fail[0]=0;
for(int c=0;c<sigma_size;c++)
{
int u=ch[0][c];
if(u){fail[u]=0;q.push(u);last[u]=0;}
}
while(!q.empty())
{
int r=q.front();q.pop();
for(int c=0;c<sigma_size;c++)
{
int u=ch[r][c];
if(!u){ch[r][c]=ch[fail[r]][c];continue;}
q.push(u);
fail[u]=ch[fail[r]][c];
last[u]=val[fail[u]]?fail[u]:last[fail[u]];
}
}
}
void count(int j,int i)
{
if(j)
{
cnt[j][0]++;
if(i-end[j]>=deep[j])
{
cnt[j][1]++;
end[j]=i;
}
count(last[j],i);
}
}
void find(char *T)
{
int j=0,n=strlen(T);
for(int i=0;i<n;i++)
{
int c=idx(T[i]);
j=ch[j][c];
if(val[j])
count(j,i);
else
if(last[j]) count(last[j],i);
}
}
};
Triegragh ac;
int main()
{
int kase=0;
while(scanf("%s",T)!=EOF)
{
printf("Case %d\n",++kase);
int n;
scanf("%d",&n);
ac.init();
for(int i=1;i<=n;i++)
{
scanf("%d%s",&flag[i],s);
pos[i]=ac.insert(s);
}
ac.getfail();
ac.find(T);
for(int i=1;i<=n;i++)
printf("%d\n",ac.cnt[pos[i]][flag[i]]);
printf("\n");
}
return 0;
}

ZOJ3228 - Searching the String(AC自动机)的更多相关文章

  1. zoj3228 Searching the String AC自动机查询目标串中模式串出现次数(分可覆盖,不可覆盖两种情况)

    /** 题目:zoj3228 Searching the String 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=34 ...

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

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

  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. 【AC自动机】zoj3228 Searching the String

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

  6. 【XSY3320】string AC自动机 哈希 点分治

    题目大意 给一棵树,每条边上有一个字符,求有多少对 \((x,y)(x<y)\),满足 \(x\) 到 \(y\) 路径上的边上的字符按顺序组成的字符串为回文串. \(1\leq n\leq 5 ...

  7. hdu 6086 -- Rikka with String(AC自动机 + 状压DP)

    题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...

  8. HDU 6096 String (AC自动机)

    题意:给出n个字符串和q个询问,每次询问给出两个串 p 和 s .要求统计所有字符串中前缀为 p 且后缀为 s (不可重叠)的字符串的数量. 析:真是觉得没有思路啊,看了官方题解,真是好复杂. 假设原 ...

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

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

随机推荐

  1. poj 1113 Mall

    Mall 水题:注意题目上面有一个至少离城堡的距离为L,其实思考一下就知道是指离凸包(凸多边形)的距离为L,这时很容易知道外围的圆的圆心角叠加之后就是一个整圆:和poj2187一样使用graham形成 ...

  2. WinForm小小应用

    制作日历计划任务 private void BeginTask() { Thread th = new Thread(//建立线程 (() =>//使用Lambda表达式 { while (tr ...

  3. C++练习题

    1. 用面向对象的程序描述员工拥有的股票,股票有公司,价格,数量属性,且拥有展现基本数据,更新价格,买进,卖出操作,并具有比较两个股票对象股值大小的比较方法. 2. 用面向对象的程序描述一个栈的操作, ...

  4. iphone下overflow失效问题的解决方法

    overflow-y: auto; -webkit-overflow-scrolling:touch; /*加上这个让浏览器支持touch和自动滚动这样界面就可以滚动了*/

  5. Mac下无法推出硬盘

    Q:刚刚mac使用移动硬盘,使用后推出时弹出无法推出,提示:Finder正在使用中. 解决:打开#活动监视器#(找不到可以在spotlight中搜索),找到Finder应用,双击,强制退出后再启动Fi ...

  6. CSS display 属性详解

    定义display 属性设置是否及如何显示元素. 继承性: No 说明 这个属性用于定义建立布局时元素生成的显示框类型.对于 HTML 等文档类型,如果使用 display 不 谨慎会很危险,因为可能 ...

  7. 30 个最棒的 jQuery 的拖放插件

    jQuery 允许用户为任意 DOM 元素添加可拖放的功能,通过 jQuery 的拖放插件你可以轻松实现网页上任意元素的拖拽操作.在本文中我们向你推荐 30 个最棒的 jQuery 的拖放插件. 点击 ...

  8. 转:Linux Shell编程入门

    http://www.cnblogs.com/suyang/archive/2008/05/18/1201990.html 从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来 ...

  9. 实用make最佳实践

    http://www.cnblogs.com/ggjucheng/archive/2011/12/14/2288055.html 一.前言 Make工具最主要也是最基本的功能就是通过makefile文 ...

  10. php数组遍历 使用foreach

    <?php $url = array ( '新浪' =>'www.sina.com' , '雅虎' =>'www.yahoo.com' , '网易' =>'www.163.co ...