码迷,mamicode.com
首页 > 其他好文 > 详细

CF873F Forbidden Indices [后缀自动机]

时间:2020-05-02 19:14:16      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:mes   sam   struct   str   efi   memcpy   cpp   ++   long   

没啥意思的后缀自动机系列,但是难度就很高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;
}

CF873F Forbidden Indices [后缀自动机]

标签:mes   sam   struct   str   efi   memcpy   cpp   ++   long   

原文地址:https://www.cnblogs.com/Isaunoya/p/12818883.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!