Period
Mean: php
給你一個字符串,讓你從第二個字符開始判斷當前長度的字符串是不是重複串,若是是,輸出當前位置,並輸出重複串的週期。ios
analyse: 數組
仍是next數組的運用,詳見上一篇博客。this
Time complexity: O(N) spa
Source code: code
/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-07-28-07.12
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define LL long long
#define ULL unsigned long long
using
namespace
std;
const
int
MAXN
=
1000010;
int n;
char s
[
MAXN
];
int
Next
[
MAXN
];
void
getNext()
{
Next
[
0
]
=
0;
for(
int
i
=
1
,
k
=
0;
i
<n;
++
i)
{
while(s
[
i
]
!=s
[
k
]
&&
k)
k
=
Next
[
k
-
1
];
if(s
[
i
]
==s
[
k
])
++
k;
Next
[
i
]
=
k;
}
}
int
main()
{
ios_base
::
sync_with_stdio(
false);
cin
.
tie(
0);
int
cas
=
1;
while(
~
scanf(
"%d"
,
&n)
&&n)
{
scanf(
"%s"
,s);
getNext();
printf(
"Test case #%d
\n
"
,
cas
++);
for(
int
i
=
1;
i
<n;
++
i)
{
int
now_cycle
=(
i
+
1)
-
Next
[
i
];
if((
now_cycle
!=
i
+
1)
&& (
i
+
1)
%
now_cycle
==
0)
{
printf(
"%d %d
\n
"
,
i
+
1
,(
i
+
1)
/
now_cycle);
}
}
puts(
"");
}
return
0;
}
/*
*/