后缀数组思路:先把字符串倒一下,不能以某点结尾就变成了不能以某点开头,假设现在没有限制,求 m a x ( ∣ a ∣ f ( a ) ) max(|a|f(a)) max(∣a∣f(a)),可以考虑 h e i g h t [ ] height[] height[]所展示的直方图,枚举枚举 h e i g t h t [ i ] heigtht[i] heigtht[i],找到直方图中左右一个小于当前 h e i g t h t [ i ] heigtht[i] heigtht[i]的位置分别记为 L [ i ] , R [ i ] L[i],R[i] L[i],R[i],这时的答案就是 m a x ( h e i g h t [ i ] ∗ ( R [ i ] − L [ i ] ) ) max(height[i]*(R[i]-L[i])) max(height[i]∗(R[i]−L[i])),再回头考虑前面的限制,发现不过就是维护一个后缀序下的前缀和。
后缀自动机思路:求出限制下的每个状态的endpos集合大小记为
n
u
m
[
i
]
num[i]
num[i],然后答案就是
m
a
x
(
n
u
m
[
i
]
∗
m
a
x
l
e
n
[
i
]
)
max(num[i]*maxlen[i])
max(num[i]∗maxlen[i])。
SA代码
const int N = 2e5 + 5, M = 22;
char s[N];
int sa[N], t[N << 1], t2[N << 1], c[N], height[N], rk[N];
int n, m;
void get_sa()
{
//s从1开始,排名从1开始
int i, *x = t, *y = t2, m = 256;
for (i = 1;i <= m;i++)c[i] = 0;
for (i = 1;i <= n;i++)c[x[i] = s[i]]++;
for (int i = 2;i <= m;i++)c[i] += c[i - 1];
for (i = n;i;i--)sa[c[x[i]]--] = i;
for (int k = 1;k <= n;k <<= 1)
{
int num = 0;
for (i = n - k + 1;i <= n;i++)y[++num] = i;
for (i = 1;i <= n;i++)if (sa[i] > k)y[++num] = sa[i] - k;
for (i = 1;i <= m;i++)c[i] = 0;
for (i = 1;i <= n;i++)c[x[i]]++;
for (i = 2;i <= m;i++)c[i] += c[i - 1];
for (i = n;i;i--)sa[c[x[y[i]]]--] = y[i];
swap(x, y);
x[sa[1]] = 1;num = 1;
for (int i = 2;i <= n;i++)
x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) ? num : ++num;
if (num == n)break;
m = num;
}
}
void get_height()
{
int k = 0;
for (int i = 1;i <= n;i++)rk[sa[i]] = i;
for (int i = 1;i <= n;i++)
{
if (rk[i] == 1)continue;
if (k)k--;
int j = sa[rk[i] - 1];
while (j + k <= n && i + k <= n && s[i + k] == s[j + k])k++;
height[rk[i]] = k;
}
}
char ju[N];
int L[N], R[N];
int sum[N];
int main()
{
//freopen("in.txt", "r", stdin);
n = in();
scanf("%s%s", s + 1, ju + 1);
reverse(s + 1, s + 1 + n);
reverse(ju + 1, ju + 1 + n);
get_sa();
get_height();
f(i, 1, n)
{
sum[i] += sum[i - 1];
sum[i] += (ju[sa[i]] == '1');
}
//考虑height(lcp(sa[i-1],sa[i]))所展示的直方图
//单调栈得到前序第一个小于当前height的,后序第一个小于当前height的
deque<pii> dq;
dq.emplace_back(pii{ 1,0 });//idx,w
f(i, 2, n)
{
if (height[i] == 0)
{
dq.clear();
dq.emplace_back(pii{ i,0 });
}
else
{
while (!dq.empty() && dq.back().second >= height[i])
{
int it = dq.back().first;
dq.pop_back();
}
L[i] = dq.back().first;
dq.emplace_back(pii{ i,height[i] });
}
}
dq.clear();
dq.emplace_back(pii{ n + 1,0 });
ff(i, n, 2)
{
if (height[i] == 0)
{
dq.clear();
dq.emplace_back(pii{ i,0 });
}
else
{
while (!dq.empty() && dq.back().second >= height[i])
{
int it = dq.back().first;
dq.pop_back();
}
R[i] = dq.back().first;
dq.emplace_back(pii{ i,height[i] });
}
}
ll ans = 0;
f(i, 1, n)if (ju[i] == '0') { ans = n - i + 1;break; }
f(i, 2, n)
{
//正确考虑如何删去禁止的
if (height[i] == 0)continue;
int forbidden = sum[R[i] - 1]- sum[L[i] - 1];
ans = max(ans, (ll)(R[i] - L[i] - forbidden)*height[i]);
}
cout << ans << endl;
return 0;
}
SAM代码
const int N = 2e5 + 5, M = 22;
struct Suffix_Automata {//minlen[i]=maxlen[link[i]]+1;
int maxlen[N << 1], trans[N << 1][130], link[N << 1], A[N << 1], tmp[N << 1],num[N<<1], Size, Last;
void init()
{
Size = Last = 1;
memset(trans, 0, sizeof trans);
}
void Qsort() {
//A[],排出一个按照 maxlen 从小到大的结点序列
//num[],每个endpos[st]集合的大小
for(int i = 1; i <= Size; i ++) tmp[maxlen[i]]++;
for(int i = 1; i <= Size; i ++) tmp[i] += tmp[i - 1];
for(int i = 1; i <= Size; i ++) A[tmp[maxlen[i]]--] = i;
for (int i =Size; i >= 1; i--) num[link[A[i]]] += num[A[i]];
}
inline void Extend(int id,int fg) {
int cur = (++Size), p;
num[cur] = fg;//如果是1则,当前endpos集合少一个元素
maxlen[cur] = maxlen[Last] + 1;
for (p = Last; p && !trans[p][id]; p = link[p]) trans[p][id] = cur;
//对suffix_path(u->s)所有满足条件的节点加上转移到当前状态的剪头
if (!p) link[cur] = 1;//情况1:suffixlink直接到初始化状态
else {
int q = trans[p][id];
if (maxlen[q] == maxlen[p] + 1) link[cur] = q;//特殊情况2
else {//一般情况3
int clone = (++Size);//拆一个状态出来
maxlen[clone] = maxlen[p] + 1;
memcpy(trans[clone], trans[q], sizeof trans[q]);
link[clone] = link[q];
for (; p && trans[p][id] == q; p = link[p]) trans[p][id] = clone;
link[cur] = link[q] = clone;
}
}
Last = cur;
}
}sam;
char s[N],t[N];
int main()
{
//freopen("in.txt", "r", stdin);
int len=in();
scanf("%s%s", s+1,t+1);
sam.init();
f(i, 1, len)sam.Extend(s[i] - 'a',t[i]!='1');
sam.Qsort();
ll ans = 0;
f(i, 1, sam.Size)ans = max(ans, (ll)sam.num[i] * sam.maxlen[i]);
cout << ans << endl;
return 0;
}