zoukankan      html  css  js  c++  java
  • BZOJ.4516.[SDOI2016]生成魔咒(后缀自动机 map)

    题目链接

    后缀数组做法见这

    直接SAM+map。对于每个节点其产生的不同子串数为len[i]-len[fa[i]]。

    //15932kb	676ms
    #include <map>
    #include <cstdio>
    #include <cctype>
    #include <algorithm>
    //#define gc() getchar()
    #define MAXIN 1500000
    #define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
    const int N=2e5+5;
    
    char IN[MAXIN],*SS=IN,*TT=IN;
    struct Suffix_Automaton
    {
    	int tot,las,fa[N],len[N];
    	long long ans;
    	std::map<int,int> son[N];
    	void Insert(int c)
    	{
    		int np=++tot,p=las; len[las=np]=len[p]+1;
    		for(; p&&!son[p][c]; p=fa[p]) son[p][c]=np;
    		if(!p) fa[np]=1;
    		else
    		{
    			int q=son[p][c];
    			if(len[q]==len[p]+1) fa[np]=q;
    			else
    			{
    				int nq=++tot;
    				len[nq]=len[p]+1, son[nq]=son[q];
    				fa[nq]=fa[q], fa[q]=fa[np]=nq;
    				for(; son[p][c]==q; p=fa[p]) son[p][c]=nq;
    			}
    		}
    		printf("%lld
    ",ans+=(long long)(len[np]-len[fa[np]]));
    	}
    }sam;
    
    inline int read()
    {
    	int now=0;register char c=gc();
    	for(;!isdigit(c);c=gc());
    	for(;isdigit(c);now=now*10+c-'0',c=gc());
    	return now;
    }
    
    int main()
    {
    	int n=read(); sam.las=sam.tot=1;
    	for(int i=1; i<=n; ++i) sam.Insert(read());
    	return 0;
    }
    
  • 相关阅读:
    JavaScript对象与数组大全
    矛盾后……
    信息化及信息系统课程相关网络资源
    有雪的日子..
    Gmail下蛋!!
    OS由XP换用WIN2003,问题以及解决
    软件考试
    又是生我的气..
    不得不说的无奈
    2005新年新气象..
  • 原文地址:https://www.cnblogs.com/SovietPower/p/9241663.html
Copyright © 2011-2022 走看看