洛谷 P4391 [BOI2009]Radio Transmission 无线传输
题目传送门
解题思路:
本题所要求的循环节,其实就是最长公共前缀的前缀长度减去前后缀重叠的一部分
AC代码:
1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 int n,j,next[1000001]; 7 string l,p; 8 9 int main() { 10 scanf("%d",&n); 11 l = " "; 12 cin >> p; 13 l += p; 14 next[1] = 0; 15 for(int i = 2;i <= n; i++) { 16 while(j != 0 && l[i] != l[j+1]) j = next[j]; 17 if(l[i] == l[j+1]) j++; 18 next[i] = j; 19 } 20 printf("%d",n - next[n]); 21 return 0; 22 }