刷题总结——字符串(ssoj)
题目:
给定n个小的字符串T和一个大的字符串S,先输出T总共再S中出现了多少次
然后q个询问···每次修改S上的一个字母,然后再次输出上述答案···
n小于1000,q<200000,T的总长度和S的长度都小于100000;
题解:
我们可以发现,每次修改位置P,那么S影响的范围只有在P-MAX到P+MAX的范围,其中MAX为T的最长长度····所以将这一范围的S跑一边AC自动机··然后更新答案即可··
这道题也让我发现我做AC自动机以来一直有的一个不好的习惯··每次求出现次数都是暴力跳fail指针,这样再这道题里直接T····
其实再我们bfs构建fail指针时,设u的指针为v,我们可以直接tree[u].cnt+=tree[v].cnt,这样每次加的时候直接加上一个节点cnt就可以了··不用暴力跳fail···代码:
代码:
- #include<iostream>
- #include<cstdio>
- #include<cstdlib>
- #include<cmath>
- #include<ctime>
- #include<cctype>
- #include<cstring>
- #include<string>
- #include<algorithm>
- #include<queue>
- using namespace std;
- queue<int>que;
- const int N=;
- struct node
- {
- int son[],cnt,fail;
- void clear()
- {
- memset(son,,sizeof(son));
- cnt=fail=;
- }
- }tree[N];
- int n,m,T,tot,maxx=;
- char s[N],t[];
- inline int R()
- {
- char c;int f=;
- for(c=getchar();c<''||c>'';c=getchar());
- for(;c<=''&&c>='';c=getchar()) f=(f<<)+(f<<)+c-'';
- return f;
- }
- int buf[];
- inline void W(int x){
- if(!x){putchar('');return ;}
- if(x<){putchar('-');x=-x;}
- while(x) buf[++buf[]]=x%,x/=;
- while(buf[]) putchar(buf[buf[]--]+);
- return ;
- }
- inline void build()
- {
- int po=;
- int len=strlen(s);maxx=max(maxx,len);
- for(int i=;i<len;i++)
- {
- if(!tree[po].son[s[i]-'a'])
- tree[tree[po].son[s[i]-'a']=++tot].clear();
- po=tree[po].son[s[i]-'a'];
- }
- tree[po].cnt++;
- }
- inline void buildfail()
- {
- que.push();
- while(!que.empty())
- {
- int u=que.front();que.pop();
- for(int i=;i<;i++)
- {
- int v=tree[u].fail;
- while(!tree[v].son[i]) v=tree[v].fail;
- v=tree[v].son[i];int w=tree[u].son[i];
- if(w) tree[w].fail=v,que.push(w),tree[w].cnt+=tree[v].cnt;
- else tree[u].son[i]=v;
- }
- }
- }
- int main()
- {
- //freopen("string.in","r",stdin);
- //freopen("string.out","w",stdout);
- n=R(),m=R();
- for(int i=;i<;i++)
- tree[].son[i]=;
- tree[tot=].clear();
- for(int i=;i<=n;i++)
- {
- scanf("%s",s);
- build();
- }
- buildfail();
- scanf("%s",s);
- int len=strlen(s),a;
- int now=,ans=;
- for(int i=;i<len;i++)
- {
- now=tree[now].son[s[i]-'a'];
- ans+=tree[now].cnt;
- }
- W(ans),putchar('\n');
- while(m--)
- {
- a=R();scanf("%s",t);
- int now=,ans1=,ans2=;
- for(int i=max(,a-maxx);i<=min(a+maxx-,len-);i++)
- {
- now=tree[now].son[s[i]-'a'];
- ans1+=tree[now].cnt;
- }
- ans-=ans1;
- s[a-]=t[];now=;
- for(int i=max(,a-maxx);i<=min(a+maxx-,len-);i++)
- {
- now=tree[now].son[s[i]-'a'];
- ans2+=tree[now].cnt;
- }
- ans+=ans2;
- W(ans),putchar('\n');
- }
- return ;
- }
刷题总结——字符串(ssoj)的更多相关文章
- LeetCode刷题总结-字符串篇
本文梳理对LeetCode上有关字符串习题的知识点,并给出对应的刷题建议.本文建议刷题的总数为32题.具体知识点如下图: 1.回文问题 题号:5. 最长回文子串,难度中等 题号:214. 最短回文串, ...
- leetcode刷题记录——字符串
242.有效地字母异位词 由于本题的字符串只包含 26 个小写字符,因此可以使用长度为 26 的整型数组对字符串出现的字符进行统计,并对比字母出现的次数是否一致.不再使用 HashMap. toCha ...
- leetcode刷题-43字符串相乘
题目 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 思路 字符串转数字:从字符串第一位开始取,每次取出的值转换为数字 ...
- LeetCode随缘刷题之字符串转换整数
package leetcode.day_01_29; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 请你 ...
- leetCode刷题(将字符串转成W形状的字符串再以Z形字符串输出)
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 刷题总结——date(ssoj)
题目: 题目背景 SOURCE:NOIP2015-SHY-9 题目描述 小Y和小Z好不容易有机会相见啦,可是邪恶的小H却不想让他们相见.现在有一些城市,城市之间有双向路径相连,有路径相连的城市之间可以 ...
- LeetCode刷题指南(字符串)
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...
- leecode刷题(16)-- 字符串转换整数
leecode刷题(16)-- 字符串转换整数 字符串转换整数 描述: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格 ...
- <LC刷题二>回文字符串判断之leetcode125&234
其他刷题记录见博客首页 1,leecode125 验证回文串 原题: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. ...
随机推荐
- 【转】iOS学习笔记(十七)——文件操作(NSFileManager)
iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容.iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内.默认 ...
- MySQL基础教程——创建数据库并插入数据
本节将介绍 MySQL 新建数据库,新建表,插入数据以及基本数据类型的相关知识.本节实验将创建一个名为 mysql_shiyan 的数据库,其中有两张表 employee和 department. 1 ...
- a survey for RL
• A finite set of states St summarizing the information the agent senses from the environment at eve ...
- 理解AttributeUsage类
类定义: // 摘要: // 指定另一特性类的用法. 此类不能被继承. [Serializable] [AttributeUsage(AttributeTargets.Class, Inherited ...
- 2_分布式计算框架MapReduce
一.mr介绍 1.MapReduce设计理念是移动计算而不是移动数据,就是把分析计算的程序,分别拷贝一份到不同的机器上,而不是移动数据. 2.计算框架有很多,不是谁替换谁的问题,是谁更适合的问题.mr ...
- SpringBoot引入监听器
方法一: 实现ServletContextListener ,并添加@WebListener注解 因为ServletContextListener 是由servlet容器管理,游离于spring容器之 ...
- Golang 简单静态web服务器
直接使用 net.http 包,非常方便 // staticWeb package main import ( "fmt" "net/http" "s ...
- Docker 在容器中部署静态网站
Docker 在容器中部署静态网站 在容器中部署静态网站 设置容器的端口映射 run -P``--publish-all=true|false:容器暴露的所有端口进行映射 -p``--publish= ...
- k8s Pod的自动水平伸缩(HPA)
我们知道,当访问量或资源需求过高时,使用:kubectl scale命令可以实现对pod的快速伸缩功能 但是我们平时工作中我们并不能提前预知访问量有多少,资源需求多少. 这就很麻烦了,总不能为了需求总 ...
- 解决iPhone滑动不流畅问题
前段时间在做一个手机端的页面时遇到了iOS上滑动不流畅的问题,后来才发现安卓上没有问题,才意识到这是兼容性问题引起的,所以遇到问题后快速定位到问题根源非常重要.在网上一搜就找到了解决方案.以后遇到类似 ...