zoukankan      html  css  js  c++  java
  • SAM模板

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    #define R register
    #define LL long long
    const int inf = 0x3f3f3f3f;
    const int N = 2e6 + 10;
    
    inline int read() {
    	char a = getchar(); int x = 0,f = 1;
    	for(;a > '9' || a < '0';a = getchar()) if(a == '-') f = -1;
    	for(;a >= '0' && a <= '9'; a = getchar()) x = x * 10 + a - '0';
    	return x * f;
    }
    
    char s[N];
    int fa[N], ch[N][26], len[N], siz[N];
    int lst = 1, node = 1, n;
    LL ans;
    
    inline void insert(int c) {
    	int now = lst, p = ++ node; lst = p;
    	len[p] = len[now] + 1; siz[p] = 1;
    	while(now && !ch[now][c]) ch[now][c] = p, now = fa[now];
    	if(! now) { fa[p] = 1; return ; }
    	int x = ch[now][c];
    	if(len[now] + 1 == len[x]) { fa[p] = x; return ; }
    	int y = ++ node; len[y] = len[now] + 1; fa[y] = fa[x]; fa[x] = fa[p] = y;
    	memcpy(ch[y], ch[x], sizeof(ch[y]));
    	while(now && ch[now][c] == x) ch[now][c] = y, now = fa[now];
    }
    
    struct edge {
    	int to, next; 
    }e[N << 1];
    int head[N], cnt;
    inline void add(int x, int y) { e[++ cnt] = {y, head[x]}; head[x] = cnt; }
    
    inline void dfs(int x) {
    	for(R int i = head[x]; i; i = e[i].next) {
    		int y = e[i].to;
    		dfs(y); siz[x] += siz[y];
    	}
    	if(siz[x] > 1) ans = max(ans, 1LL * siz[x] * len[x]);
    }
    
    int main() {
    	scanf("%s", s + 1); n = strlen(s + 1);
    	for(R int i = 1; i <= n; i ++) insert(s[i] - 'a');
    	for(R int i = 1; i <= node; i ++) add(fa[i], i);
    	dfs(1); printf("%lld
    ", ans);
    	return 0;	
    }
    
  • 相关阅读:
    【NOIP2011】观光公交
    【NOIP2014】飞扬的小鸟
    HDU
    [Tyvj 1728]普通平衡树
    【NOIP2012】 疫情控制
    洛谷P1613 跑路
    [HNOI2002]营业额统计
    3486 ( Interviewe )RMQ
    poj2019 二维RMQ裸题
    动态规划专题
  • 原文地址:https://www.cnblogs.com/clover4/p/13358627.html
Copyright © 2011-2022 走看看