• 【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;
    }
    
  • 相关阅读:
    博客收藏
    日常开发工具收藏
    2016工作总结
    REST设计规则
    spring boot 学习笔记(二) 构建web支持jsp
    数据库事务中的隔离级别和锁+spring Transactional注解
    springboot学习笔记(一)
    Python爬虫实例(四)网站模拟登陆
    Python爬虫实例(三)代理的使用
    Python爬虫实例(二)使用selenium抓取斗鱼直播平台数据
  • 原文地址:https://www.cnblogs.com/luoyibujue/p/10612613.html
Copyright © 2020-2023  润新知