1 条题解

  • 0
    @ 2023-10-31 19:18:38
    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 1e6 + 5;
    int n, nxt[maxn];
    char s[maxn];
    
    void cal_nxt() { 
    	nxt[1] = 0;
    	for (int i = 2, j = 0; i <= n; i++) {
    		while (j && s[j+1] != s[i])
    			j = nxt[j];
    		if (s[j+1] == s[i]) j++;
    		nxt[i] = j;
    	}
    }
    
    int main() {
    	scanf("%d%s", &n, s+1);
    	cal_nxt();
    	printf("%d\n", nxt[n]);
    	return 0;
    }
    
    
    • 1

    信息

    ID
    7
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    7
    已通过
    3
    上传者