题目:

给定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···代码:

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cmath>
  5. #include<ctime>
  6. #include<cctype>
  7. #include<cstring>
  8. #include<string>
  9. #include<algorithm>
  10. #include<queue>
  11. using namespace std;
  12. queue<int>que;
  13. const int N=;
  14. struct node
  15. {
  16. int son[],cnt,fail;
  17. void clear()
  18. {
  19. memset(son,,sizeof(son));
  20. cnt=fail=;
  21. }
  22. }tree[N];
  23. int n,m,T,tot,maxx=;
  24. char s[N],t[];
  25. inline int R()
  26. {
  27. char c;int f=;
  28. for(c=getchar();c<''||c>'';c=getchar());
  29. for(;c<=''&&c>='';c=getchar()) f=(f<<)+(f<<)+c-'';
  30. return f;
  31. }
  32. int buf[];
  33. inline void W(int x){
  34. if(!x){putchar('');return ;}
  35. if(x<){putchar('-');x=-x;}
  36. while(x) buf[++buf[]]=x%,x/=;
  37. while(buf[]) putchar(buf[buf[]--]+);
  38. return ;
  39. }
  40.  
  41. inline void build()
  42. {
  43. int po=;
  44. int len=strlen(s);maxx=max(maxx,len);
  45. for(int i=;i<len;i++)
  46. {
  47. if(!tree[po].son[s[i]-'a'])
  48. tree[tree[po].son[s[i]-'a']=++tot].clear();
  49. po=tree[po].son[s[i]-'a'];
  50. }
  51. tree[po].cnt++;
  52. }
  53. inline void buildfail()
  54. {
  55. que.push();
  56. while(!que.empty())
  57. {
  58. int u=que.front();que.pop();
  59. for(int i=;i<;i++)
  60. {
  61. int v=tree[u].fail;
  62. while(!tree[v].son[i]) v=tree[v].fail;
  63. v=tree[v].son[i];int w=tree[u].son[i];
  64. if(w) tree[w].fail=v,que.push(w),tree[w].cnt+=tree[v].cnt;
  65. else tree[u].son[i]=v;
  66. }
  67. }
  68. }
  69. int main()
  70. {
  71. //freopen("string.in","r",stdin);
  72. //freopen("string.out","w",stdout);
  73. n=R(),m=R();
  74. for(int i=;i<;i++)
  75. tree[].son[i]=;
  76. tree[tot=].clear();
  77. for(int i=;i<=n;i++)
  78. {
  79. scanf("%s",s);
  80. build();
  81. }
  82. buildfail();
  83. scanf("%s",s);
  84. int len=strlen(s),a;
  85. int now=,ans=;
  86. for(int i=;i<len;i++)
  87. {
  88. now=tree[now].son[s[i]-'a'];
  89. ans+=tree[now].cnt;
  90. }
  91. W(ans),putchar('\n');
  92. while(m--)
  93. {
  94. a=R();scanf("%s",t);
  95. int now=,ans1=,ans2=;
  96. for(int i=max(,a-maxx);i<=min(a+maxx-,len-);i++)
  97. {
  98. now=tree[now].son[s[i]-'a'];
  99. ans1+=tree[now].cnt;
  100. }
  101. ans-=ans1;
  102. s[a-]=t[];now=;
  103. for(int i=max(,a-maxx);i<=min(a+maxx-,len-);i++)
  104. {
  105. now=tree[now].son[s[i]-'a'];
  106. ans2+=tree[now].cnt;
  107. }
  108. ans+=ans2;
  109. W(ans),putchar('\n');
  110. }
  111. return ;
  112. }

刷题总结——字符串(ssoj)的更多相关文章

  1. LeetCode刷题总结-字符串篇

    本文梳理对LeetCode上有关字符串习题的知识点,并给出对应的刷题建议.本文建议刷题的总数为32题.具体知识点如下图: 1.回文问题 题号:5. 最长回文子串,难度中等 题号:214. 最短回文串, ...

  2. leetcode刷题记录——字符串

    242.有效地字母异位词 由于本题的字符串只包含 26 个小写字符,因此可以使用长度为 26 的整型数组对字符串出现的字符进行统计,并对比字母出现的次数是否一致.不再使用 HashMap. toCha ...

  3. leetcode刷题-43字符串相乘

    题目 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 思路 字符串转数字:从字符串第一位开始取,每次取出的值转换为数字 ...

  4. LeetCode随缘刷题之字符串转换整数

    package leetcode.day_01_29; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 请你 ...

  5. leetCode刷题(将字符串转成W形状的字符串再以Z形字符串输出)

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  6. 刷题总结——date(ssoj)

    题目: 题目背景 SOURCE:NOIP2015-SHY-9 题目描述 小Y和小Z好不容易有机会相见啦,可是邪恶的小H却不想让他们相见.现在有一些城市,城市之间有双向路径相连,有路径相连的城市之间可以 ...

  7. LeetCode刷题指南(字符串)

    作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...

  8. leecode刷题(16)-- 字符串转换整数

    leecode刷题(16)-- 字符串转换整数 字符串转换整数 描述: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格 ...

  9. <LC刷题二>回文字符串判断之leetcode125&234

    其他刷题记录见博客首页 1,leecode125 验证回文串 原题: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. ...

随机推荐

  1. 【转】iOS学习笔记(十七)——文件操作(NSFileManager)

    iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容.iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内.默认 ...

  2. MySQL基础教程——创建数据库并插入数据

    本节将介绍 MySQL 新建数据库,新建表,插入数据以及基本数据类型的相关知识.本节实验将创建一个名为 mysql_shiyan 的数据库,其中有两张表 employee和 department. 1 ...

  3. a survey for RL

    • A finite set of states St summarizing the information the agent senses from the environment at eve ...

  4. 理解AttributeUsage类

    类定义: // 摘要: // 指定另一特性类的用法. 此类不能被继承. [Serializable] [AttributeUsage(AttributeTargets.Class, Inherited ...

  5. 2_分布式计算框架MapReduce

    一.mr介绍 1.MapReduce设计理念是移动计算而不是移动数据,就是把分析计算的程序,分别拷贝一份到不同的机器上,而不是移动数据. 2.计算框架有很多,不是谁替换谁的问题,是谁更适合的问题.mr ...

  6. SpringBoot引入监听器

    方法一: 实现ServletContextListener ,并添加@WebListener注解 因为ServletContextListener 是由servlet容器管理,游离于spring容器之 ...

  7. Golang 简单静态web服务器

    直接使用 net.http 包,非常方便 // staticWeb package main import ( "fmt" "net/http" "s ...

  8. Docker 在容器中部署静态网站

    Docker 在容器中部署静态网站 在容器中部署静态网站 设置容器的端口映射 run -P``--publish-all=true|false:容器暴露的所有端口进行映射 -p``--publish= ...

  9. k8s Pod的自动水平伸缩(HPA)

    我们知道,当访问量或资源需求过高时,使用:kubectl scale命令可以实现对pod的快速伸缩功能 但是我们平时工作中我们并不能提前预知访问量有多少,资源需求多少. 这就很麻烦了,总不能为了需求总 ...

  10. 解决iPhone滑动不流畅问题

    前段时间在做一个手机端的页面时遇到了iOS上滑动不流畅的问题,后来才发现安卓上没有问题,才意识到这是兼容性问题引起的,所以遇到问题后快速定位到问题根源非常重要.在网上一搜就找到了解决方案.以后遇到类似 ...