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

Creo报许可证错误License request failed for feature的解决方案 有些朋友用了一段时间打开creo软件,出现icense request failed for feature”错误,这个是Creo许可证出现了问题,一般是许可证文件无效或是已过期,导致Creo软件通信出现故障,就会出现Creo License request failed for feature, Requsesting CreoPMA_SSQ ,Run ptcstatus.bat server status。第三步:键盘按win+R,在运行窗口里输入:cmd,然后点确定。第八步:点击系统,找到系统信息。 阅读详情

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;
}
SRT公网点对点传输及搭建SRT流媒体服务器 SRT是一种开源传输技术,它是基于UDP协议的,与RTMP不同的是,它具有高码率低延迟的传输特性,加强了在复杂不稳定网络环境中的传输可靠性,提升了不可预测的网络环境上的流传输性能。它不像NDI那样只适用于局域网传输,它更适用于公网环境下的信号传输。 S代表Secure,即安全加密的视频流,R代表Reliable,即具有从数据严重丢包中恢复的可靠性,T代表Transport,即动态适应变化的网络状况。 在音视频数据包传输的过程中,SRT会检测并适应两个传输点之间的实时网络状况,有助于补偿由于网络的拥塞而引起的 阅读详情

相关推荐

从标准到实战:详解IEC 61000-4-2下的ESD电路防护设计

本文深入解析IEC 61000-4-2标准下的ESD电路防护设计,从静电防护的基础原理到实战应用。详细介绍了接触放电与空气放电的区别、防护器件选型要点(如TVS管和压敏电阻)以及PCB布局的黄金法则,帮助工程师有效提升电子产品的抗静电能力,确保设备在严苛环境下的稳定运行。

weixin_33704234的博客 512

zoj 3228(ac自动机)

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

俯瞰风景 475

STM32F103循迹小车PID调参实战:手把手教你整定舵机PD与电机PI参数

本文详细介绍了基于STM32F103的循迹小车PID调参实战,重点讲解舵机PD控制与电机PI参数的整定技巧。通过硬件选型、基础测试、四步调参法和分段控制策略,帮助开发者解决小车蛇形走位等问题,提升循迹性能。文章还分享了高级调试技巧和典型问题排查方法,适用于智能车竞赛和机器人开发。

weixin_42524226的博客 361

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

告别ORB-SLAM2?手把手教你用OpenVSLAM在Ubuntu 20.04上跑通第一个SLAM Demo(含依赖库避坑指南)

本文详细介绍了如何在Ubuntu 20.04系统上从ORB-SLAM2迁移到OpenVSLAM,包括环境配置、依赖库安装、编译调整及性能优化。OpenVSLAM凭借其模块化设计和多相机支持,在SLAM领域展现出显著优势,适合机器人导航和AR应用开发。文章还提供了实用的避坑指南和进阶技巧,帮助开发者高效完成迁移。

weixin_42524499的博客 255

Git系列(一)、Git基础教程【建议收藏】

Git一、Git简介1.1、什么是Git? 一、Git简介 1.1、什么是Git? Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或大或小的项目。 Git是Linus Torvalds为了帮助管理Linux内核开发而开发的一个开放源代码的版本控制软件。 Git与常用的版本控制工具CVS、Subversion等不同,它采用了分布式版本库的方式,不用服务器端软件支持。 .........

一宿君 4万+

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

研究生课程笔记:软件包在流行病学中的应用(三)——STATA软件

关键词:STATA软件基本操作、STATA对于流行病学研究的实现、STATA对复杂抽样设计样本量计算的实现 一、STATA软件基本操作 这部分网上教程有很多,也可以直接根据stata自带的help功能进行自学,在此只记录课程中提及的功能项。 1. 变量编辑 1.1 赋值:label type(按精度区分) 1) 整型:int(32), short(16), long(32) 2) 实型:flo...

weixin_45463530的博客 2432

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

QGraphicsView通过鼠标操作来绘制图元

在Visio和MindManager等类似的图元操作软件中,很多时候我们会通过鼠标操作来添加自定义的图元。这里就介绍一下如何在QT的图形视图框架中通过鼠标绘制来添加图元。

yang1fei2的博客 3576

黑猫带你学UFS协议第13篇:UFS初始化详解(Boot Operation)

本文详细解析了UFS设备的初始化过程,包括部分初始化、加载启动代码(可选)和初始化完成三个阶段。重点介绍了如何通过设备描述符和逻辑单元描述符配置引导特性,以及如何从UFS设备加载启动代码。内容适用于嵌入式硬件和存储领域的开发者。

黑猫奥利奥的博客 3555

zoj3228

/* * AC自动机,每个节点 添加一个d表示节点代表的字符串的查询类型1表示只有查询0,2表示只有查询1,3表示有查询 * 0和查询1,len表示该节点代表的字符串的长度。 */ #include &lt;cstdio&gt; #include &lt;cstring&gt; #include &lt;map&gt; using namespace std; cons...

唐武的博客 257

【学习系列】SAP RAP 16:RAP应用部署集成至Fiori Launchpad 【On-Premise】

本文详细介绍了如何将开发完成的RAP应用部署到S4HANA 2023 On-Premise环境并集成到Fiori Launchpad的完整流程。主要内容包括:1)通过ADT工具创建Fiori项目并配置外部IDE(VSCode);2)使用npm命令部署应用到系统;3)通过事务代码创建语义对象、磁贴目录和目标映射;4)配置PFCG角色分配;5)补充了部署失败解决方案、翻译问题处理、自定义主题设置以及登录页面LOGO修改等内容。文章提供了step-by-step的操作指南,帮助开发者将预览模式的应用正式部署到生产

DeveloperMrMeng的博客 637

AC自动机 zoj3228 Searching the String

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

qwb的博客 965

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

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

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

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的博客 280

ZOJ 3228 Searching the String(AC自动机)

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

绝望的乐园 940

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

636

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

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的博客 250
上一篇: 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、付费专栏及课程。

余额充值