其他分享
首页 > 其他分享> > Period UVA - 1328

Period UVA - 1328

作者:互联网

思路

注意重复字符串可以重叠!!!比如 \(aba\) 重复两次就可以是 \(ababa\) 。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1000010;
int test_case = 1;
int n;
string s;
int ne[N];
int main () {
	while (cin >> n,n) {
		memset (ne,0,sizeof (ne));
		cin >> s;
		s = ' ' + s;
		for (int i = 2,j = 0;i <= n;i++) {
			while (j && s[i] != s[j + 1]) j = ne[j];
			if (s[i] == s[j + 1]) j++;
			ne[i] = j;
		}
		cout << "Test case #" << test_case++ << endl;
		for (int i = 2;i <= n;i++) {
			if (ne[i] > 0 && i % (i - ne[i]) == 0) cout << i << ' ' << i / (i - ne[i]) << endl;
		}
		puts ("");
	}
    return 0;
}

标签:cout,cin,int,1328,Period,ne,重复,UVA,include
来源: https://www.cnblogs.com/incra/p/16640137.html