poj2406 Power Strings 字尾陣列 DC3演算法
Time Limit: 3000MS | Memory Limit: 65536K |
Total Submissions: 46554 | Accepted: 19489 |
Description
Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).Input
Output
For each s you should print the largest n such that s = a^n for some string a.Sample Input
abcd aaaa ababab .
Sample Output
1 4 3
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.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輕而易舉,這裡只是練一下字尾陣列罷了