POJ1743 Musical Theme
Musical Theme
题目大意
给出一串数字(每个数字范围1...88),问是否存在两个长度相等的不重叠子串,两串每两个同位数字的差值为定制。长度小于5则输出0,否则输出最大长度。
题解
巧妙而常见的转化:差分后差分串的两个长度为l的子串相等,则原串对应的两个长度为l+1的子串相等。
而后变成求解最长不重叠子串问题。
巧妙而常见的SA套路:二分分组。长度可二分,设二分长度为x,检验其是否合法。把height >= x的分一组,组内的前缀都有>=x的长度是相同的。看开始的位置sa,是否存在两个差值大于x,存在则合题意。可以用ST表求sa最大最小作差进行比较。
充分性显然,必要性想一想也就明白了(doge
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <iostream>
void swap(int &a, int &b){int tmp = a;a = b, b = tmp;}
void swap(int* &a, int* &b){int* tmp = a;a = b;b = tmp;}
int max(int a, int b){return a > b ? a : b;}
int min(int a, int b){return a < b ? a : b;}
int lowbit(int x){return x & (-x);}
void read(int &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}
const int INF = 0x3f3f3f3f;
const int MAXN = 1000000 + 10;
struct SuffixArray
{
int s[MAXN], sa[MAXN], rank[MAXN], height[MAXN];
int t[MAXN], t2[MAXN], c[MAXN];
int n;
void clear()
{
n = 0;
memset(sa, 0, sizeof(sa));
}
void build_sa(int m)
{
++ n;
int i,*x = t,*y = t2;
for(i = 0;i < m;++ i) c[i] = 0;
for(i = 0;i < n;++ i) x[i] = s[i];
for(i = 0;i < n;++ i) ++ c[x[i]];
for(i = 1;i < m;++ i) c[i] += c[i-1];
for(i = n - 1;i >= 0;-- i) sa[-- c[x[i]]] = i;
for(int k = 1;k <= n;k <<= 1)
{
int p = 0;
for(i = n - k;i < n;++ i) y[p ++] = i;
for(i = 0;i < n;++ i) if(sa[i] >= k) y[p ++] = sa[i] - k;
for(i = 0;i < m;++ i) c[i] = 0;
for(i = 0;i < n;++ i) c[x[i]] ++;
for(i = 1;i < m;++ i) c[i] += c[i - 1];
for(i = n - 1;i>=0;--i) sa[-- c[x[y[i]]]] = y[i];
swap(x,y);
p = 1;x[sa[0]] = 0;
for(i = 1;i < n;++ i)
x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) ? p-1 : p++;
if(p >= n) break;
m = p;
}
-- n;
}
void build_height()
{
int i, j, k = 0;
for(i = 1;i <= n;++ i) rank[sa[i]] = i;
for(i = 0;i < n;++ i)
{
if(k) k --;
j = sa[rank[i]-1];
while(s[i + k] == s[j + k]) k ++;
height[rank[i]] = k;
}
}
}A;
int n, num[MAXN], l[MAXN], r[MAXN], tot;
int ma[25][MAXN], mi[25][MAXN], M, lg2[MAXN], pow2[30];
//ma[i][j] [j, j + 2^i - 1]
//ma[i][j] = max(ma[i - 1][j], ma[i - 1][j + 2^(i - 1)]
void init()
{
for(M = 0;(1 << M) <= n;++ M); -- M;
pow2[0] = 1;
for(int i = 1;i <= 20;++ i) pow2[i] = (pow2[i - 1] << 1);
lg2[1] = 0;
for(int i = 2;i < n;++ i) lg2[i] = lg2[i >> 1] + 1;
for(int i = 1;i <= n;++ i) ma[0][i] = mi[0][i] = A.sa[i];
for(int i = 1;i <= M;++ i)
for(int j = 1;j <= n;++ j)
ma[i][j] = max(ma[i - 1][j], ma[i - 1][j + pow2[i - 1]]),
mi[i][j] = min(mi[i - 1][j], mi[i - 1][j + pow2[i - 1]]);
}
int getma(int l, int r)
{
return max(ma[lg2[r - l + 1]][l], ma[lg2[r - l + 1]][r - pow2[lg2[r - l + 1]] + 1]);
}
int getmi(int l, int r)
{
return min(mi[lg2[r - l + 1]][l], mi[lg2[r - l + 1]][r - pow2[lg2[r - l + 1]] + 1]);
}
bool check(int x)
{
tot = 0;
l[++ tot] = 1;
for(int i = 2;i < n;++ i)
{
while(A.height[i] >= x)
++ i;
r[tot] = i - 1;
l[++ tot] = i;
}
r[tot] = n - 1;
for(int i = 1;i <= tot;++ i)
if(getma(l[i], r[i]) - getmi(l[i], r[i]) > x)
return true;
return false;
}
int main()
{
while(scanf("%d", &n) != EOF && n)
{
A.clear();
for(int i = 0;i < n;++ i) read(num[i]);
for(int i = n - 1;i >= 1;-- i) num[i] -= num[i - 1], num[i] += 90;
for(int i = 0;i < n;++ i) A.s[i] = num[i];
A.n = n + 1;
A.build_sa(200);
A.build_height();
init();
int l = 0, r = n, mid, ans = 0;
while(l <= r)
{
mid = (l + r) >> 1;
if(check(mid)) ans = mid, l = mid + 1;
else r = mid - 1;
}
if(ans < 4) printf("0\n");
else printf("%d\n", ans + 1);
}
return 0;
}