题目链接:http://poj.org/problem?id=2406
题意:给定一个字符串,求由一个子串循环n次后可得到原串,输出n[即输出字符串的最大循环次数]
思路一:KMP求最小循环机,然后就能求出循环次数。
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<time.h>
#include<cmath>
using namespace std;
typedef long long int LL;
const int MAXN = + ;
char str[MAXN];
int Next[MAXN],len;
void getNext(){
int i=, k = -;
Next[] = -;
while (i < len){
if (k == - || str[i] == str[k]){
++i; ++k;
Next[i] = k;
}
else{
k = Next[k];
}
}
}
int main(){
//#ifdef kirito
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
//#endif
// int start = clock();
while (scanf("%s", str) && str[] != '.'){
len = strlen(str);
getNext();
if (Next[len] && (len % (len - Next[len])== )){
printf("%d\n", len / (len-Next[len]));
}
else{
printf("1\n");
}
}
//#ifdef LOCAL_TIME
// cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
return ;
}
思路二:后缀数组,直接根据09年oi论文<<后缀数组——出来字符串的有力工具>>的解法。
穷举字符串S 的长度k,然后判断是否满足。判断的时候,先看字符串L 的长度能否被k 整除,再看suffix(1)和suffix(k+1)的最长公共前缀是否等于n-k。在询问最长公共前缀的时候,suffix(1)是固定的,所以RMQ问题没有必要做所有的预处理, 只需求出height 数组中的每一个数到height[rank[1]]之间的最小值即可。整个做法的时间复杂度为O(n)。
补充:该题字符串长度比较大,达到1e7的上限,所以O(nlogn)的倍增会TLE,所以考虑O(n)的DC3。 但是还是要2500ms才能AC,对于KMP的125ms来说,该题还是KMP比较优而且代码了比较少,不过学习到求最小循环次数还可以用后缀数组来做。
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<time.h>
#include<cmath>
using namespace std;
typedef long long int LL;
#define INF 0x3f3f3f3f
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
const int MAXN = + ;
int wa[MAXN], wb[MAXN], wv[MAXN], WS[MAXN];
int c0(int *r, int a, int b)
{
return r[a] == r[b] && r[a + ] == r[b + ] && r[a + ] == r[b + ];
}
int c12(int k, int *r, int a, int b)
{
if (k == ) return r[a]<r[b] || r[a] == r[b] && c12(, r, a + , b + );
else return r[a]<r[b] || r[a] == r[b] && wv[a + ]<wv[b + ];
}
void sort(int *r, int *a, int *b, int n, int m)
{
int i;
for (i = ; i<n; i++) wv[i] = r[a[i]];
for (i = ; i<m; i++) WS[i] = ;
for (i = ; i<n; i++) WS[wv[i]]++;
for (i = ; i<m; i++) WS[i] += WS[i - ];
for (i = n - ; i >= ; i--) b[--WS[wv[i]]] = a[i];
return;
}
void dc3(int *r, int *sa, int n, int m)
{
int i, j, *rn = r + n, *san = sa + n, ta = , tb = (n + ) / , tbc = , p;
r[n] = r[n + ] = ;
for (i = ; i<n; i++) if (i % != ) wa[tbc++] = i;
sort(r + , wa, wb, tbc, m);
sort(r + , wb, wa, tbc, m);
sort(r, wa, wb, tbc, m);
for (p = , rn[F(wb[])] = , i = ; i<tbc; i++)
rn[F(wb[i])] = c0(r, wb[i - ], wb[i]) ? p - : p++;
if (p<tbc) dc3(rn, san, tbc, p);
else for (i = ; i<tbc; i++) san[rn[i]] = i;
for (i = ; i<tbc; i++) if (san[i]<tb) wb[ta++] = san[i] * ;
if (n % == ) wb[ta++] = n - ;
sort(r, wb, wa, ta, m);
for (i = ; i<tbc; i++) wv[wb[i] = G(san[i])] = i;
for (i = , j = , p = ; i<ta && j<tbc; p++)
sa[p] = c12(wb[j] % , r, wa[i], wb[j]) ? wa[i++] : wb[j++];
for (; i<ta; p++) sa[p] = wa[i++];
for (; j<tbc; p++) sa[p] = wb[j++];
return;
}
int Rank[MAXN], height[MAXN], sa[MAXN];
void calheight(int *r, int *sa, int n){
int i, j, k = ;
for (i = ; i <= n; i++) Rank[sa[i]] = i;
for (i = ; i < n; height[Rank[i++]] = k)
for (k ? k-- : , j = sa[Rank[i] - ]; r[i + k] == r[j + k]; k++);
return;
}
int len, r[MAXN], LCP[MAXN];
char str[MAXN];
void solve(){
//LCP[i]:suffix(0)和suffix(i)的最长公共前缀
for (int i = Rank[] - , lcpval = INF; i > ; i--){
lcpval = min(lcpval, height[i + ]);
LCP[sa[i]] = lcpval;
}
for (int i = Rank[] + , lcpval = INF; i <= len; i++){
lcpval = min(lcpval, height[i]);
LCP[sa[i]] = lcpval;
}
int ans = ;
for (int k = ; k <= len; k++){
if (len%k != ){ continue; }
if (LCP[k] == len - k){ //第一个找到一定是最优解
ans = len / k;
break;
}
}
printf("%d\n", ans);
}
int main(){
//#ifdef kirito
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
//#endif
// int start = clock();
while (scanf("%s", str) && str[] != '.'){
len = strlen(str);
for (int i = ; i <= len; i++){
if (i == len){ r[i] = ; continue; }
r[i] = (int)str[i];
}
dc3(r, sa, len + , );
calheight(r, sa, len);
solve();
}
//#ifdef LOCAL_TIME
// cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
return ;
}