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)] = '\n'; }
	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 << '\n';
	return 0;
}
上一篇:linux下Nginx403错误


下一篇:bootstarp风格的toggle效果分享