20240418
This commit is contained in:
28
ACMOJ-1507.cpp
Normal file
28
ACMOJ-1507.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
using namespace std;
|
||||
typedef long long LL;
|
||||
const int kMaxN = 1e6 + 10;
|
||||
char S[kMaxN], T[kMaxN];
|
||||
int nxt[kMaxN];
|
||||
int main() {
|
||||
#ifdef local
|
||||
freopen("pro.in", "r", stdin);
|
||||
#endif
|
||||
scanf("%s%s", S + 1, T + 1);
|
||||
int len_T = strlen(T + 1);
|
||||
for (int i = 2, j = 0; i <= len_T; i++) {
|
||||
while (j > 0 && T[j + 1] != T[i]) j = nxt[j];
|
||||
if (T[j + 1] == T[i]) j++;
|
||||
nxt[i] = j;
|
||||
}
|
||||
int len_S = strlen(S + 1);
|
||||
for (int i = 1, j = 0; i <= len_S; i++) {
|
||||
while (j > 0 && (j == len_T || T[j + 1] != S[i])) j = nxt[j];
|
||||
if (T[j + 1] == S[i]) j++;
|
||||
if (j == len_T) printf("%d\n", i - len_T + 1);
|
||||
}
|
||||
for (int i = 1; i <= len_T; i++) printf("%d ", nxt[i]);
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user