咳咳花似雪拿我做铺垫,题解
2023-07-17 21:00:09
发布于:广东
29阅读
0回复
0点赞
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 1e6 + 10;
char s[N], p[N], ans[N];
int n, m, top, Next[N], f[N];
void get_Next() {
for (int i = 2, j = 0; i <= m; i++) {
while (j && p[i] != p[j + 1]) j = Next[j];
if (p[i] == p[j + 1]) j++;
Next[i] = j;
}
}
void Kmp() {
for (int i = 1, j = 0; i <= n; i++) {
ans[top] = s[i];
while (j && (j == m || s[i] != p[j + 1])) j = Next[j];
if (s[i] == p[j + 1]) j;
f[top] = j;
if (j == m) {
top = top - m;
j = f[top];
}
}
ans[++top] = '\0';
printf("%s\n", ans + 1);
}
int main() {
scanf("%s%s", s + 1, p + 1);
n = strlen(s + 1);
m = strlen(p + 1);
get_Next();
Kmp();
return 0;
}
这里空空如也
有帮助,赞一个