hihocoder-1419 后缀数组四·重复旋律4 求连续重复次数最多的子串

对于重复次数,如果确定了重复子串的长度len,那重复次数k=lcp(start,start+len)/len+1。而暴力枚举start和len的复杂度是O(n^2),不能接受。而有一个规律,若我们只枚举len的整数倍作为起始,如果将它向前移动小于len位可以构成重复次数更长的串,那么那个位置p=start-len+lcp%len。所以每次我们计算两者并求max再与ans做max即可。

#include <cstdio>
#include <string>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cstring>
#include <queue>
#define LL int
using namespace std;
const LL N = ; class SF
{
//N:数组大小
public:
int x[N], y[N], c[N];
int Height[N], str[N], SA[N], Rank[N];//Height数组从2开始,SA记录Rank=i的下标
int slen;
int m = ;//字符集处理大小(传入如果不是数字,需要做位移转换)
bool cmp(int* r, int a, int b, int l) {
return r[a] == r[b] && r[a + l] == r[b + l];
}
void Suffix(int n) {
++n;
int i, j, p;
for (i = ; i < m; ++i) c[i] = ;
for (i = ; i < n; ++i) c[x[i] = str[i]]++;
for (i = ; i < m; ++i) c[i] += c[i - ];
for (i = n - ; i >= ; --i) SA[--c[x[i]]] = i;
for (j = ; j <= n; j <<= ) {
p = ;
for (i = n - j; i < n; ++i) y[p++] = i;
for (i = ; i < n; ++i) if (SA[i] >= j) y[p++] = SA[i] - j;
for (i = ; i < m; ++i) c[i] = ;
for (i = ; i < n; ++i) c[x[y[i]]]++; for (i = ; i < m; ++i) c[i] += c[i - ];
for (i = n - ; i >= ; --i) SA[--c[x[y[i]]]] = y[i]; swap(x, y);
p = ; x[SA[]] = ;
for (i = ; i < n; ++i) {
x[SA[i]] = cmp(y, SA[i - ], SA[i], j) ? p - : p++;
}
if (p >= n)break;
m = p;
} int k = ;
n--;
for (i = ; i <= n; ++i) Rank[SA[i]] = i;
for (i = ; i < n; ++i) {
if (k)--k;
j = SA[Rank[i] - ];
while (str[i + k] == str[j + k])++k;
Height[Rank[i]] = k;
//cout << k << endl;
}
}
static const int bitlen = ;
LL lg2(LL p)//计算log2(n)
{
return (LL)(log(p) / log());
}
LL dp[bitlen][N];
LL bit[bitlen];
void initRMQ()//初始化
{
bit[] = ;
for (int i = ; i < bitlen; i++) bit[i] = * bit[i - ];
for (int i = ; i <= slen; i++)
dp[][i] = Height[i];
dp[][] = dp[][] = ;
for (LL i = ; bit[i] < slen + ; i++)
for (LL j = ; j + bit[i] <= slen + ; j++)
dp[i][j] = min(dp[i - ][j], dp[i - ][j + bit[i - ]]);
}
LL query(LL l, LL r)//查询两个Rank之间的lcp
{
if (r == l) return slen - SA[l];
if (l > r) swap(l, r);
l++;
LL mig = lg2(r - l + 1.0);
return min(dp[mig][l], dp[mig][r - bit[mig] + ]);
}
void init(string &s)
{
slen = s.size();
for (int i = ; i < slen; i++)
str[i] = s[i] - 'a' + ;//如果是字符,映射成从1开始的序列
str[slen] = ;//1作为结束符,防止越界
Suffix(slen);
initRMQ();
}
int solve()
{
int ans = ;
for (int len = ; len <= slen; len++)
{
for (int i = ; i+len < slen; i+=len)
{
int r1 = Rank[i], r2 = Rank[i+ len];
int lcp = query(r1, r2);
ans = max(ans, lcp / len + );
if (i - len + lcp%len >= )
{
lcp = query(Rank[i - len + lcp%len], Rank[i + lcp%len]);
ans = max(ans, lcp / len + );
}
}
}
return ans;
}
}sf;
int main()
{
cin.sync_with_stdio(false);
string s;
while (cin >> s)
{
sf.init(s);
cout << sf.solve() << endl;
}
return ;
}
上一篇:HDU_1457_后缀自动机四·重复旋律7


下一篇:iOS设计模式解析(六)代理模式