You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
430 B

#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n;
int ne[N];
char p[N];
/**
测试数据:
5 ABABC
输出:
0 0 1 2 0
*/
int main() {
cin >> n >> p + 1;
for (int i = 2, j = 0; i <= n; i++) {
while (j && p[i] != p[j + 1]) j = ne[j];
if (p[i] == p[j + 1]) j++;
ne[i] = j;
}
for (int i = 1; i <= n; i++) printf("%d ", ne[i]);
return 0;
}