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

2026软件著作权申请新规:材料格式、线上申报与避坑要点 软件著作权登记是开发者保护原创代码与文档成果的法律手段,其审查本质是核对提交材料的规范性、完整性与一致性。随着登记流程全面线上化,系统自动校验源程序页数行数、文档篇幅、页眉版本号等硬性指标,任何格式瑕疵都可能触发补正,拉长出证周期。理解材料红线与审查原理,能显著提升一次过审率。无论是个人开发者、中小团队还是外包申报,掌握源代码分段规则、说明书编写规范、权属声明要求,都是降低退审风险的关键。本文结合2026年新规下的实操经验,梳理软著申请全流程中的高频退审原因与零补正清单,帮助你在线上申报时代高效拿下证书。 阅读详情

Little jay really hates to deal with string. But moondy likes it very much, and she’s so mischievous that she often gives jay some dull problems related to string. And one day, moondy gave jay another problem, poor jay finally broke out and cried, ” Who can help me? I’ll bg him! “

So what is the problem this time?

First, moondy gave jay a very long string A. Then she gave him a sequence of very short substrings, and asked him to find how many times each substring appeared in string A. What’s more, she would denote whether or not founded appearances of this substring are allowed to overlap.

At first, jay just read string A from begin to end to search all appearances of each given substring. But he soon felt exhausted and couldn’t go on any more, so he gave up and broke out this time.

I know you’re a good guy and will help with jay even without bg, won’t you?

Input

Input consists of multiple cases( <= 20 ) and terminates with end of file.

For each case, the first line contains string A ( length <= 10^5 ). The second line contains an integer N ( N <= 10^5 ), which denotes the number of queries. The next N lines, each with an integer type and a string a ( length <= 6 ), type = 0 denotes substring a is allowed to overlap and type = 1 denotes not. Note that all input characters are lowercase.

There is a blank line between two consecutive cases.

Output

For each case, output the case number first ( based on 1 , see Samples ).

Then for each query, output an integer in a single line denoting the maximum times you can find the substring under certain rules.

Output an empty line after each case.

Sample Input

ab
2
0 ab
1 ab

abababac
2
0 aba
1 aba

abcdefghijklmnopqrstuvwxyz
3
0 abc
1 def
1 jmn

Sample Output

Case 1
1
1

Case 2
3
2

Case 3
1
1
0

Hint

In Case 2,you can find the first substring starting in position (indexed from 0) 0,2,4, since they’re allowed to overlap. The second substring starts in position 0 and 4, since they’re not allowed to overlap.

For C++ users, kindly use scanf to avoid TLE for huge inputs.
Author: LI, Jie
Source: ZOJ Monthly, July 2009

对于那些可以重叠的串,就是模版了,对于不可重叠的,记录该节点上一次访问到时的位置即可,一开始用了vector记录每个节点结尾的串的id,匹配的时候遍历然后超时了,后来把每个串结束的节点记录下来就过了

/*************************************************************************
    > File Name: zoj3228.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年03月04日 星期三 16时44分33秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int MAX_NODE = 500100;
const int CHILD_NUM = 26;

int tag[MAX_NODE];
int num[MAX_NODE][2];
int pos[100100];
int ty[100100];

struct AC_Automation
{
    int next[MAX_NODE][CHILD_NUM];
    int fail[MAX_NODE];
    int pre[MAX_NODE];
    int size[MAX_NODE];
    int root, L;

    int newnode ()
    {
        for (int i = 0; i < CHILD_NUM; ++i)
        {
            next[L][i] = -1;
        }
        ++L;
        return L - 1;
    }

    void init ()
    {
        L = 0;
        memset (pre, -1, sizeof(pre));
        memset (num, 0, sizeof(num));
        memset (size, 0, sizeof(size));
        root = newnode();
        size[root] = 0;
    }

    int Build_Trie (char buf[])
    {
        int now = root;
        int len = strlen (buf);
        for (int i = 0; i < len; ++i)
        {
            if (next[now][buf[i] - 'a'] == -1)
            {
                next[now][buf[i] - 'a'] = newnode();
            }
            now = next[now][buf[i] - 'a'];
            size[now] = i + 1;
        }
        return now;
    }

    void Build_AC ()
    {
        queue <int> qu;
        fail[root] = root;
        for (int i = 0; i < CHILD_NUM; ++i)
        {
            if (next[root][i] == -1)
            {
                next[root][i] = root;
            }
            else
            {
                fail[next[root][i]] = root;
                qu.push (next[root][i]);
            }
        }
        while (!qu.empty())
        {
            int now = qu.front();
            qu.pop();
            for (int i = 0; i < CHILD_NUM; ++i)
            {
                if (next[now][i] == -1)
                {
                    next[now][i] = next[fail[now]][i];
                }
                else
                {
                    fail[next[now][i]] = next[fail[now]][i];
                    qu.push (next[now][i]);
                }
            }
        }
    }

    void Match (char buf[])
    {
        int len = strlen (buf);
        int now = root;
        for (int i = 0; i < len; ++i)
        {
            now = next[now][buf[i] - 'a'];
            int tmp = now;
            while (tmp != root)
            {
                ++num[tmp][0];
                if (i - pre[tmp] >= size[tmp])
                {
                    ++num[tmp][1];
                    pre[tmp] = i;
                }
                tmp = fail[tmp];
            }
        }
    }

}AC;

char buf[100010];
char str[10];

int main ()
{
    int icase = 1;
    while (~scanf("%s", buf))
    {
        AC.init();
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; ++i)
        {
            scanf("%d%s", &ty[i], str);
            pos[i] = AC.Build_Trie (str);
        }
        AC.Build_AC();
        AC.Match(buf);
        printf("Case %d\n", icase++);
        for (int i = 1; i <= n; ++i)
        {
            printf("%d\n", num[pos[i]][ty[i]]);
        }
        printf("\n");
    }
    return 0;
}
Chamfer Loss ChamferLoss是一种常用于3D点云(pointcloud)处理的损失函数,特别是在点云生成和对齐任务中。它的目的是度量两个点集之间的相似度,特别是在点云重建、配准或生成等任务中,常用于优化模型使得生成的点云更接近真实的点云。 阅读详情

相关推荐

MATLABGUI读取文件并将路径显示在文本框中

MATLABGUI读取文件并将路径显示在文本框中

m0_50305149的博客 2655

zoj 3228(ac自动机)

题意:给出一个目标串,然后有n个询问,0 模式串 是询问模式串可重叠在目标串中出现了多少次,1 模式串 是询问模式串不可重叠在目标串中出现了多少次。 题解:可重叠的就是普通的模板题,不可重叠。不可重叠意味着每个匹配模式串的开头位置一定在上一个匹配结束位置之后,也就是 当前目标串位置 - 上一个匹配模式串的结束位置 >= 当前字符在模式串中的位置。trie树的val[i]数组就可以存i节点在模式串中

俯瞰风景 475

GD32F470 DSP库移植避坑指南:从CMSIS版本冲突到AC6工程配置

本文详细解析了在GD32F470平台上移植CMSIS-DSP库的完整流程与关键避坑点。针对CMSIS版本冲突、AC6编译器下的浮点ABI配置、DSP库源文件添加等核心难题,提供了经过验证的解决方案,帮助开发者高效完成移植,确保硬件FPU正常工作并优化性能。

data3的博客 534

Searching the String(ZOJ-3228)

Problem Description Little jay really hates to deal with string. But moondy likes it very much, and she's so mischievous that she often gives jay some dull problems related to string. And one day, m...

Alex_McAvoy的博客 628

华为CCA架构软件栈VehicleStack解析:3层服务化设计与AOS/VOS双核OS

本文深入解析华为CCA架构下的VehicleStack软件平台,重点介绍其三层服务化设计(硬件抽象层、服务框架层、应用层)与AOS/VOS双核操作系统协同机制。该架构通过计算与通信的深度融合,推动汽车电子电气架构向集中式发展,为智能汽车提供灵活、高效的软件定义汽车解决方案。

weixin_33491377的博客 326

从理论到实践:基于Zemax的高分辨率生物显微镜光学系统仿真与优化指南

本文详细介绍了基于Zemax的高分辨率生物显微镜光学系统仿真与优化全流程。从阿贝分辨率公式、柯勒照明等理论基础出发,逐步讲解Zemax建模、像差优化、制造可行性分析等核心步骤,并提供常见问题解决方案。通过实际案例展示如何将理论设计转化为可制造的优化方案,帮助工程师掌握从仿真到实际应用的关键技术。

weixin_29229989的博客 494

zoj 3228 Searching the String 【AC自动机】

Searching the String Time Limit: 7 Seconds      Memory Limit: 129872 KB Little jay really hates to deal with string. But moondy likes it very much, and she's so mischievous that she often gives

世界很大 908

ICT调试技巧:从基础到实战的全面指南

本文提供了一份全面的ICT调试技巧指南,涵盖从基础认知到实战应用的全过程。详细解析了ICT设备硬件结构、程序文件构成,并重点讲解了调试界面的核心参数与高效快捷键。通过跳线、电阻、电容及二极管等常见元器件的实战调试案例,分享了参数设置要点与典型问题解决方案,帮助工程师快速掌握ICT调试核心技能,提升测试效率与准确性。

weixin_29202213的博客 349

zoj3228AC自动机

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/D0状态时就是模板,1时判断一下后匹配的位置减前面的距离是不是大于当前单词的距离#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <queue>

skymeteorite 517

YOLO26涨点改进 | 独家主干改进篇 | CVPR 2024 | YOLO26利用InceptionNeXt主干,将大核深度卷积分解为四个并行分支,在提升性能的同时显著降低计算效率

本文聚焦YOLO26的核心涨点改进——基于CVPR 2024收录的InceptionNeXt主干网络,深度拆解了其“大核深度卷积四分支拆解”的核心逻辑,通过实验验证了改进的有效性,同时提供了可快速复现的工程代码与进一步涨点的思路。

qq_36130719的博客 585

解锁Vscode:C/C++环境配置超详细指南

本文详细介绍如何在 VSCode 中配置 C/C++ 开发环境,帮助开发者快速搭建高效的编码调试环境。内容涵盖前期准备、软件下载安装、环境变量配置、工作区文件设置及测试运行全流程:首先需准备 VSCode 编辑器、MinGW-w64 编译器及 C/C++ 插件;接着完成工具下载与安装,配置系统环境变量以确保编译器可全局调用;然后通过创建.vscode工作区文件夹,配置核心文件,实现代码智能提示与调试功能;最后通过编写 Hello World 示例验证环境可用性,并附上常见问题排查方案。

邓邓子的博客 3052

ZOJ 3228 Searching the String AC自动机的不重复匹配

这个判断方法真的没想到。。。 对于在S中匹配M,如果M上一次的匹配位置pre与这一次的匹配位置now满足now-pre >= M.length,则加1。 这个判断太跳了233 。 #include #include #include #include #include #include #include #include #include #include #include #

自在飞花 971

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

AC自动机 zoj3228 Searching the String

传送门:点击打开链接 题意:告诉你原串,告诉你很多个子串,有两种匹配模式,一种是可以重叠的匹配,一种是不能重叠的,问每个子串出现的次数。子串可能有重复的出现。 思路:这题揭示了AC自动机的两种处理方法。 首先是当AC自动机中出现有串重复的时候,我们应该怎么处理,我这里用到了一个数组pre,如果这个节点结尾的子串是第一次出现,pre标记为自己,如果这个节点结尾的子串不是第一次出现,那么就把pr

qwb的博客 965

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

zoj3228 Searching the String AC自动机

Description Little jay really hates to deal with string. But moondy likes it very much, and she’s so mischievous that she often gives jay some dull problems related to string. And one day, moondy gave

xtulollipop 784

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Searching the String ZOJ - 3228 (AC自动机+思维)

题目链接 题目大意:给定一个文本串,n个模式串,其中模式串有两种,一种是同一模式串可以有重合的部分,如ababa 出现两次aba,另一种是相同的模式串不允许重合,如ababa 出现1次aba.问所有的模式串各自在文本串中出现了多少次. 思路:对于第一种,可以重合的模式串,就是朴素的ac自动机。 对于第二种,相同模式串不能重合,我们可以知道,相同模式串有重合当且仅当第二次出现的位置-第一次出现的位置<字符串长度, 我们可以开一个数组last[],last[i]记录以i号节点结尾的单词,最新匹配成功的位置

矮纸斜行闲作草 1031

zoj 3228 Searching the String(AC自动机)

题目连接:zoj 3228 Searching the String 题目大意:给定一个字符串,然后现在有N次询问,每次有一个type和一个子串,问说子串在字符串中出现几次,type 为0时为可重叠,为1时为不可重叠。 解题思路:不过没有type=1的限制,那么就是普通的AC自动机匹配问题,对于不可重叠问题,可以对于每个节点记录 一下上一次匹配到的pos,用当前匹配的i减掉

不慌不忙、不急不躁 1177

zoj 3228 Searching the String【ac自动机】

2896454 2012-05-18 16:35:19 Accepted 3228 C++ 2100 72084 zz_1215 2896441 2012-05-18 16:30:35 Compilation Error 3228 C++ 0 0 zz_1215 2896425 2

量子流逝 942

ZOJ 3228 Searching the String AC自动机

题目大意: 就是现在有N个( N

Gatevin的专栏 769

ZOJ 3228 Searching the String(AC自动机)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3228 解题思路: 给出一个长串,询问很多个模式串在这个长串中可重叠最大重复次数以及不可重叠最大重复次数。 首先KMP虽然处理可重叠和不可重叠很方便(不可重叠的情况完全匹配后j指针归0即可),但是无奈长串太长,模式串太多,所以肯定会T成狗 那么还是用AC自动...

shadiao.的博客 205

ZOJ 3228 Searching the String(AC自动机)

题意:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3228 思路:AC自动机,不过匹配时要要分可重复使用相同串,和不可重复。。就是再find后,要进行一个初始位置与长度的判断,不能重复使用的,就找到后,更新最小初始位置, 详见注释,,(直接用饶齐的代码分析了,实在不想自己敲,现在也不是很有做题的心情。。) 代码: ...

AAAAAAAC的博客 160

Searching the String ZOJ - 3228 (AC自动机)

Little jay really hates to deal with string. But moondy likes it very much, and she's so mischievous that she often gives jay some dull problems related to string. And one day, moondy gave jay anothe...

Top_Spirit的博客 282

ZOJ 3228 Searching the String(AC自动机)

题意:给出一个串A,再给出n个查询,每个查询为一个串,询问这个串在A中出现的次数,另外,每个串有个type,type为0时这个串允许覆盖,否则不允许覆盖。 思路:比较裸的AC自动机吧,对于不允许覆盖的单词,只要记录上一次查询到该串时的位置就行了。另外数据可能会有相同的串,因此用了map搞了下,时间感觉有点慢。。。 代码: #include #include #incl

绝望的乐园 942

ZOJ Problem Set - 3228 Searching the String AC自动机

Searching the String Time Limit: 7 Seconds      Memory Limit: 129872 KB Little jay really hates to deal with string. But moondy likes it very much, and she's so mischievous that she often g

637

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

题目: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3441 题意: 给定一个长串; 给定n个询问,每个询问给定询问类型和一个段串; 有两种询问类型: 0类型要求输出短串在长串中出现的次数(允许重叠); 1类型要求输出短串在长串中出现的次数(不允许重叠)。 分析: 允许重叠的容易,裸的AC自动机; 不允许重叠的只需这样处理:...

OI -> ACM -> AI 393

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

题目链接:https://vjudge.net/problem/ZOJ-3228 Searching the StringTime Limit:7 Seconds Memory Limit:129872 KB Little jay really hates to deal with string. But moondy likes it very much, an...

alince20008的博客 251

ZOJ3228 Searching the String (AC自动机)

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

weixin_30622181的博客 112
上一篇: hdu2296---Ring(AC自动机+dp)
下一篇: POJ3321---Apple Tree(树状数组)
tokers
博客等级 码龄12年 71粉丝 700原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值