Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 46554 | Accepted: 19489 |
Description
Input
Output
Sample Input
abcd aaaa ababab .
Sample Output
1 4 3
Hint
Source
题意:给你一个串,该串可以由其一个子串重复k'次形成,让你求的k的最大值
思路:首先得思想是枚举k然后变为判断性问题,接下来就是如何确定成立条件了
1.k必须要能被串长整除,即len%k==0成立
2.sa[0],sa[k]代表的两个串的最长公共前缀的长度应该==len-k,
3.***还有人说需要满足rank[k]-rank[0]=1;但我感觉此条件是k成立的必要条件而已,不要也可以
还有需要注意的一点是,该题的数据量太大,如果用倍增法的话就要tle了,所以要用时间复杂度更低的DC3算法
ac代码:
#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 = 10000000 + 5;
int wa[MAXN], wb[MAXN], wv[MAXN], WS[MAXN];
int c0(int *r, int a, int b)
{
return r[a] == r[b] && r[a + 1] == r[b + 1] && r[a + 2] == r[b + 2];
}
int c12(int k, int *r, int a, int b)
{
if (k == 2) return r[a]<r[b] || r[a] == r[b] && c12(1, r, a + 1, b + 1);
else return r[a]<r[b] || r[a] == r[b] && wv[a + 1]<wv[b + 1];
}
void sort(int *r, int *a, int *b, int n, int m)
{
int i;
for (i = 0; i<n; i++) wv[i] = r[a[i]];
for (i = 0; i<m; i++) WS[i] = 0;
for (i = 0; i<n; i++) WS[wv[i]]++;
for (i = 1; i<m; i++) WS[i] += WS[i - 1];
for (i = n - 1; i >= 0; 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 = 0, tb = (n + 1) / 3, tbc = 0, p;
r[n] = r[n + 1] = 0;
for (i = 0; i<n; i++) if (i % 3 != 0) wa[tbc++] = i;
sort(r + 2, wa, wb, tbc, m);
sort(r + 1, wb, wa, tbc, m);
sort(r, wa, wb, tbc, m);
for (p = 1, rn[F(wb[0])] = 0, i = 1; i<tbc; i++)
rn[F(wb[i])] = c0(r, wb[i - 1], wb[i]) ? p - 1 : p++;
if (p<tbc) dc3(rn, san, tbc, p);
else for (i = 0; i<tbc; i++) san[rn[i]] = i;
for (i = 0; i<tbc; i++) if (san[i]<tb) wb[ta++] = san[i] * 3;
if (n % 3 == 1) wb[ta++] = n - 1;
sort(r, wb, wa, ta, m);
for (i = 0; i<tbc; i++) wv[wb[i] = G(san[i])] = i;
for (i = 0, j = 0, p = 0; i<ta && j<tbc; p++)
sa[p] = c12(wb[j] % 3, 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 = 0;
for (i = 1; i <= n; i++) Rank[sa[i]] = i;
for (i = 0; i < n; height[Rank[i++]] = k)
for (k ? k-- : 0, j = sa[Rank[i] - 1]; 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[0] - 1, lcpval = INF; i > 0; i--){
lcpval = min(lcpval, height[i + 1]);
LCP[sa[i]] = lcpval;
}
for (int i = Rank[0] + 1, lcpval = INF; i <= len; i++){
lcpval = min(lcpval, height[i]);
LCP[sa[i]] = lcpval;
}
int ans = 1;
for (int k = 1; k <= len; k++){
if (len%k != 0){ continue; }
if (LCP[k] == len - k){ //第一个找到一定是最优解
ans = len / k;
break;
}
}
printf("%d\n", ans);
}
int main(){
while (scanf("%s", str) && str[0] != '.'){
len = strlen(str);
for (int i = 0; i <= len; i++){
if (i == len){ r[i] = 0; continue; }
r[i] = (int)str[i];
}
dc3(r, sa, len + 1, 256);
calheight(r, sa, len);
int k=len;
for(int i=1;i<=len;i++)
{
if(len%i==0&&height[Rank[0]]==len-i&&Rank[0]-Rank[i]==1)
{
k=i;
break;
}
}
printf("%d\n",len/k);
}
return 0;
}
其实本题用kmp轻而易举,这里只是练一下后缀数组罢了