zoukankan      html  css  js  c++  java
  • 【SPOJ】Substrings(后缀自动机)

    /*
    求right集合大小 
    然后后缀最大值 
    
    */
    #include<cstdio>
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<queue>
    #define ll long long 
    #define M 600010
    #define mmp make_pair
    using namespace std;
    int read()
    {
    	int nm = 0, f = 1;
    	char c = getchar();
    	for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
    	for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
    	return nm * f;
    }
    char s[M];
    int ch[M][26], sz[M], ans[M], tim[M], a[M], fa[M], len[M], cnt = 1, lst = 1; 
    
    void insert(int c)
    {
    	int p = ++cnt, f = lst;
    	lst = cnt;
    	len[p] = len[f] + 1;
    	sz[p] = 1;
    	while(f && !ch[f][c]) ch[f][c] = p, f = fa[f];
    	if(!f) fa[p] = 1;
    	else
    	{
    		int q = ch[f][c];
    		if(len[q] == len[f] + 1) fa[p] = q;
    		else
    		{
    			int nq = ++cnt;
    			fa[nq] = fa[q];
    			len[nq] = len[f] + 1;
    			memcpy(ch[nq], ch[q], sizeof(ch[nq]));
    			fa[p] = fa[q] = nq;
    			while(f && ch[f][c] == q) ch[f][c] = nq, f = fa[f];
    		}
    	}
    }
    	
    
    int main()
    {
    	scanf("%s", s + 1);
    	int l = strlen(s + 1);
    	for(int i = 1; i <= l; i++) insert(s[i] - 'a');
    	for(int i = 1; i <= cnt; i++) tim[len[i]]++;
    	for(int i = 1; i <= cnt; i++) tim[i] += tim[i - 1];
    	for(int i = 1; i <= cnt; i++) a[tim[len[i]]--] = i;
    	for(int i = cnt; i >= 1; i--)
    	{
    		sz[fa[a[i]]] += sz[a[i]];
    		ans[len[a[i]]] = max(ans[len[a[i]]], sz[a[i]]);
    	}
    	for(int i = cnt; i >= 1; i--) ans[i] = max(ans[i], ans[i + 1]);
    	for(int i = 1; i <= l; i++) cout << ans[i] << "
    "; 
    	return 0;
    }
    
  • 相关阅读:
    Reporting Services 配置工具
    管道符、重定向和环境变量
    靶机DC-2 rbash绕过+git提权
    单表查询
    数据库和表的基本操作(二)
    数据库和表的基本操作(一)
    MySQL的约束
    bugku-misc 9-16
    Linux基础命令
    时间-i春秋
  • 原文地址:https://www.cnblogs.com/luoyibujue/p/10612613.html
Copyright © 2011-2022 走看看