CF873F Forbidden Indices [后缀自动机]

Posted isaunoya

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF873F Forbidden Indices [后缀自动机]相关的知识,希望对你有一定的参考价值。

没啥意思的后缀自动机系列,但是难度就很高2333

// by Isaunoya
#include<bits/stdc++.h>
#define int long long
using namespace std;
struct io {
	char buf[1 << 25 | 3], *s;
	int f;
	io() { f = 0, buf[fread(s = buf, 1, 1 << 25, stdin)] = ‘
‘; }
	io& operator >> (int&x) {
		for(x = f = 0; !isdigit(*s); ++s) f |= *s  == ‘-‘;
		while(isdigit(*s)) x = x * 10 + (*s++ ^ 48);
		return x = f ? -x : x, *this;
	}
};

int n;
const int maxn = 4e5 + 54;
char s[maxn];

struct sam {
	int ch[maxn][26], fa[maxn], len[maxn];
	
	int las, cnt;
	sam() { las = cnt = 1; }
	
	void ins(int c) {
		int p = las, np = ++ cnt;
		las = np;
		len[np] = len[p] + 1;
		for(; p && !ch[p][c]; p = fa[p]) { ch[p][c] = np; }
		if(!p) { fa[np] = 1; }
		else {
			int q = ch[p][c];
			if(len[q] == len[p] + 1) { fa[np] = q; }
			else {
				int nq = ++ cnt;
				fa[nq] = fa[q], fa[q] = fa[np] = nq;
				memcpy(ch[nq], ch[q], sizeof(ch[q]));
				len[nq] = len[p] + 1;
				for(; p && ch[p][c] == q; p = fa[p])
					ch[p][c] = nq;
			}
		}
	}
} sam;

int sz[maxn];
vector <int> g[maxn];
int ans = 0;
void dfs(int u) {
	for(int v: g[u]) {
		dfs(v);
		sz[u] += sz[v];
	}
	ans = max(ans, sz[u] * sam.len[u]);
}

signed main() {
#ifdef LOCAL
	freopen("testdata.in", "r", stdin);
#endif
	ios :: sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cin >> n;
	for(int i = 1 ; i <= n ; i ++)
		cin >> s[i];
	for(int i = 1 ; i <= n ; i ++) {
		char x; cin >> x;
		sam.ins(s[i] - ‘a‘);
		if(x == ‘0‘) sz[sam.las] = 1;
	}
	for(int i = 2; i <= sam.cnt; i ++)
		g[sam.fa[i]].push_back(i);
	dfs(1);
	cout << ans << ‘
‘;
	return 0;
}

以上是关于CF873F Forbidden Indices [后缀自动机]的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 873F Forbidden Indices 后缀数组

CF873FForbidden Indices 后缀自动机

cf873f(xjb+二分)

CF1009F Dominant Indices 解题报告

CF1009F Dominant Indices [长链剖分]

cf1009F. Dominant Indices