zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    Linux之wget命令
    Markdown语法
    Windows实时预览markdown
    Python基础教程,Python入门教程(非常详细)
    【转载】UNICODE与ASCII的区别
    Python之虚拟环境
    Linux文件系统管理
    Linux权限管理
    linux用户和用户组管理
    linux 软件安装
  • 原文地址:https://www.cnblogs.com/Isaunoya/p/12818883.html
Copyright © 2011-2022 走看看