題目鏈接
題意:給出一個字符串s,問在[0, i]區間是否有完整的循環節,若有,輸出i並輸出循環次數
分析:跟這道題中求循環節一樣,這次只要把 i 到 m 的區間全部跑一遍就行。
注意不要用memset 不然會 T
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstring>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <cstdio>
#include <algorithm>
#define INF 0x3f3f3f3f
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef pair<string, string> P;
const int mod=1e9+7;
const int maxn=1e6+10;
int nx[maxn];
int m;
void getnx(string x){
//int m=x.size();
int i,j;
j=nx[0]=-1;
i=0;
while (i<m){
if (j==-1 || x[i]==x[j]){
i++,j++;
nx[i]=j;
}
else j=nx[j];
}
}
string s;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T=1;
while (cin>>m && m){
for (int i=0; i<=m; i++) nx[i]=0;
cin>>s;
getnx(s);
cout<<"Test case #"<<T++<<endl;
for (int i=2; i<=m; i++){
if (i%(i-nx[i])==0 && i/(i-nx[i])>1)
cout<<i<<" "<<i/(i-nx[i])<<endl;
}
cout<<endl;
}
return 0;
}