声明

本人在理解kuangbin的模板之后写下注释,如有错请谅解,可留言告诉我错误谢谢;
附上kuangbin的一些网站:
kuangbin的ACM模板:https://kuangbin.github.io/2018/08/01/ACM-template/
kuangbin的博客:https://kuangbin.github.io
kuangbin的博客园:https://www.cnblogs.com/kuangbin/
kuangbin的GitHub:https://github.com/kuangbin

1、字符串处理

1.1、KMP

  1. #include<stdio.h>
  2. #include<string.h>
  3. /*
  4. * man[] 主串
  5. * pat[] 模式串
  6. * next[i] 失配数组,数组值为模式串前 i 项构成的子串前缀后缀相同部分长度的最大值
  7. *
  8. * kmpPre(pat[],patlen,next[])
  9. * 以 i 和 j 为指针,模式串跟自己进行匹配,构建失配数组以方便模式串在主串中配对时进行跳转
  10. * kmpCount(pat[],patlen,man[],manlen)
  11. * 以 i 和 j 为指针,模式串在失配数组的辅助下在主串中进行匹配,输出模式串在主串中出现的次数 ans
  12. */
  13. int next[10010];
  14. void kmpPre(char pat[], int patLen, int next[]){
  15. int i,j;
  16. i=0; j=next[0]=-1;
  17. while(i<patLen){
  18. while(j!=-1 && pat[i]!=pat[j]) j=next[j];
  19. i++; j++;
  20. next[i]=j;
  21. }
  22. }
  23. int kmpCount(char pat[], int patLen, char man[], int manLen){
  24. int i,j;
  25. int ans=0;
  26. kmpPre(pat,patLen,next);
  27. i=j=0;
  28. while(i<manLen){
  29. while(j!=-1 && man[i]!=pat[j]) j=next[j];
  30. i++; j++;
  31. if(j>=patLen){
  32. ans++;
  33. j=next[j];
  34. }
  35. }
  36. return ans;
  37. }
  38. int main(){
  39. char man[]={"ababababca"};
  40. char pat[]= {"abababca"};
  41. int ans=kmpCount(pat,strlen(pat),man,strlen(man));
  42. printf("%d\n", ans);
  43. return 0;
  44. }

1.3、manacher

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. using namespace std;
  5. /*
  6. * MAXN 原字符串长度
  7. * str[] 原字符串
  8. * ma[] 加工串,填充处理后的字符串
  9. * mp[i] 以 ma[] 为模板,当前位置 i 为子回文串中心,值为该回文串半径长度
  10. * '$' 开头占位符
  11. * '#' 字符间占位符
  12. * mx 代表当前<已经匹配完毕的结尾最远的回文串>到达了ma[]数组的第mx位
  13. * id 代表当前<已经匹配完毕的结尾最远的回文串>中心为ma[]数组的第id位
  14. *
  15. * Mp[i]=1,说明Mx没有覆盖超过i,那么Mx的値在这一步执行后一定会增加
  16. * Mp[i]=Mx-i,说明以Ma[i]为中心的回文串至少到达了Mx,那么Mx的値在这之后不会减少
  17. * Mp[i]=Mp[ID*2-i],说明Ma[i]已经匹配不下去了
  18. *
  19. * str[i]: a b a a b a
  20. * i: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
  21. * ma[i]: $ # a # b # a # a # b # a # /0
  22. * mp[i]: 1 1 2 1 4 1 2 7 2 1 4 1 2 1
  23. */
  24. const int MAXN=100;
  25. char str[MAXN], ma[MAXN*2];
  26. int mp[MAXN*2];
  27. void manacher(int sLen){
  28. int maCount=0;
  29. ma[maCount++]='$';
  30. ma[maCount++]='#';
  31. for(int i=0; i<sLen; i++){
  32. ma[maCount++]=str[i];
  33. ma[maCount++]='#';
  34. }
  35. ma[maCount]=0;
  36. int mx=0;
  37. int id=0;
  38. for(int i=0; i<maCount; i++){
  39. mp[i]=mx>i?min(mp[2*id-i],mx-i):1;
  40. while(ma[i+mp[i]]==ma[i-mp[i]])
  41. mp[i]++;
  42. if(i+mp[i]>mx){
  43. mx=i+mp[i];
  44. id=i;
  45. }
  46. }
  47. }
  48. int main(){
  49. while (scanf("%s", str) == 1){
  50. int sLen=strlen(str);
  51. manacher(sLen);
  52. int ans=0;
  53. for (int i=0; i<2*sLen+2; i++)
  54. ans=max(ans,mp[i]-1);
  55. printf("%d\n", ans);
  56. }
  57. return 0;
  58. }

1.4、AC自动机

  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <string.h>
  5. #include <queue>
  6. using namespace std;
  7. /*
  8. * MAXN 字典树节点序号总数
  9. * root 根节点序号,一直为0
  10. * L 为当前节点序号
  11. * buf[] 临时存储字符串(模式串和文章)
  12. * next[i][j]
  13. * 节点连接数组,i 为字典树当前节点序号,j 为该节点字母序号,数组值为相对应下一个字典树节点的序号(靠左节点优先赋值)
  14. * fail[i]
  15. * 失配数组,i 为字典树当前节点序号,数组值为当前节点相对应下一个字典树节点失配后跳转到的字典树节点序号{a->a)
  16. * end[i]
  17. * 单词尾部加数,字典树当前节点序号为 i,数组值为以当前节点为结尾的单词数(大多数时候为1)
  18. *
  19. * init() 初始化字典树建立根
  20. * newnode() 建立新节点
  21. * insert(buf[]) now 为当前节点序号,向字典树中装入模式串
  22. * build() 用队列 Q 依次存节点序号,构建失配数组方便查询
  23. * query(buf[]) 输入文章,输出 n 个模式串在文章中出现的个数(?/n个)
  24. */
  25. const int MAXN=500010;
  26. struct Trie{
  27. int next[MAXN][26], fail[MAXN], end[MAXN];
  28. int root, L;
  29. void init(){
  30. L = 0;
  31. root = newnode();
  32. }
  33. int newnode(){
  34. for (int i = 0; i < 26; i++)
  35. next[L][i] = -1;
  36. end[L++] = 0;
  37. return L - 1;
  38. }
  39. void insert(char buf[]){
  40. int len = strlen(buf);
  41. int now = root;
  42. for (int i = 0; i < len; i++){
  43. if (next[now][buf[i] - 'a'] == -1)
  44. next[now][buf[i] - 'a'] = newnode();
  45. now = next[now][buf[i] - 'a'];
  46. }
  47. end[now]++;
  48. }
  49. void build(){
  50. queue<int> Q;
  51. fail[root] = root;
  52. for (int i = 0; i < 26; i++)
  53. if (next[root][i] == -1)
  54. next[root][i] = root;
  55. else{
  56. fail[next[root][i]] = root;
  57. Q.push(next[root][i]);
  58. }
  59. while (!Q.empty()){
  60. int now = Q.front();
  61. Q.pop();
  62. for (int i = 0; i < 26; i++)
  63. if (next[now][i] == -1)
  64. next[now][i] = next[fail[now]][i];
  65. else{
  66. fail[next[now][i]] = next[fail[now]][i];
  67. Q.push(next[now][i]);
  68. }
  69. }
  70. }
  71. int query(char buf[]){
  72. int len = strlen(buf);
  73. int now = root;
  74. int res = 0;
  75. for (int i = 0; i < len; i++){
  76. now = next[now][buf[i] - 'a'];
  77. int temp = now;
  78. while (temp != root){
  79. res += end[temp];
  80. end[temp] = 0;
  81. temp = fail[temp];
  82. }
  83. }
  84. return res;
  85. }
  86. void debug(){
  87. for (int i = 0; i < L; i++){
  88. printf("id = %3d,fail = %3d,end = %3d,chi = [", i, fail[i], end[i]);
  89. for (int j = 0; j < 26; j++)
  90. printf("%2d", next[i][j]);
  91. printf("]\n");
  92. }
  93. }
  94. };
  95. char buf[1000010];
  96. Trie ac;
  97. int main(){
  98. int T;
  99. int n;
  100. scanf("%d", &T);
  101. while (T--){
  102. scanf("%d", &n);
  103. ac.init();
  104. for (int i = 0; i < n; i++){
  105. scanf("%s", buf);
  106. ac.insert(buf);
  107. }
  108. ac.build();
  109. scanf("%s", buf);
  110. printf("%d\n", ac.query(buf));
  111. }
  112. return 0;
  113. }

2、数学

2.1、素数

2.1.1 素数筛选(判断 <MAXN 的数是否素数)

  1. /*
  2. * MAXN 判断是否为素数的最大值
  3. * notprime[i] i 为所指数字,其值为 false 表示为素数,true 表示不为素数
  4. *
  5. * 素数:指在大于1的自然数中,只有1和它本身能整除的数
  6. * 存储小于 MAXN 的数是不是素数这一状态
  7. */
  8. #include<cstring>
  9. const int MAXN = 1000010;
  10. bool notprime[MAXN];
  11. void init(){
  12. memset(notprime, false, sizeof(notprime));
  13. notprime[0] = notprime[1] = true;
  14. for (int i = 2; i < MAXN; i++)
  15. if (!notprime[i]){
  16. //防止后面 i*i 溢出 (或者 i,j 用 long long)
  17. if (i > MAXN / i)
  18. continue;
  19. //直接从 i*i 开始就可以,小于 i 倍的已经筛选过了, 注意是 j+=i
  20. for (int j = i * i; j < MAXN; j += i)
  21. notprime[j] = true;
  22. }
  23. }