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;
    }
    
  • 相关阅读:
    sfs2x 连接 mongodb
    java websocket
    webstorm 4.0 注册码
    解决 sfs2 admin tool 找不到扩展
    window 注册表五大类
    opengl 学习第二日
    java google Protobuf
    扩展 java sencha touch PhonegapPlugin
    sencha touch2 kryonet socket phonegap 通信 作者:围城
    sencha touch2 layout 笔记
  • 原文地址:https://www.cnblogs.com/luoyibujue/p/10612613.html
Copyright © 2011-2022 走看看