关于如何在KMP模板中跑的比KMP还快(雾)
  • 板块题目总版
  • 楼主Timmy_Zhong
  • 当前回复3
  • 已保存回复3
  • 发布时间2021/1/25 07:37
  • 上次更新2023/11/5 04:26:35
查看原帖
关于如何在KMP模板中跑的比KMP还快(雾)
224757
Timmy_Zhong楼主2021/1/25 07:37
# #define c(u,s)for(;s[++i];j+=!q,u)for(;q=s[i]^t[j],j*q;)j=f[j];
q, f[1 << 20], i, j, x;
main() {
   char s[q = 1E6], t[q];
   scanf("%s%s", s, t);
   c(f[i + 1] = j, t)i = -1, j = 0;
   c(x += !t[j], s)printf("%d", x);
}
#include <bits/stdc++.h>
char x = 233, a[1000005], b[1000005];
long long n, m, i, A, T = 1, B, ans;
int main() {
    for (gets(a), gets(b), n = strlen(a), m = strlen(b); i < m; ++i)
        A = A * x + a[i], B = B * x + b[i], T = T * x;

    for (ans = A == B; i < n; ++i)
        A = A * x + a[i] - T * a[i - m], ans += A == B;

    printf("%d\n", ans);
}
2021/1/25 07:37
加载中...