zoukankan      html  css  js  c++  java
  • 后缀自动机模板

    代码

    //确认maxn大小,一般是原字符串长度最大值的2倍,然后是init(),最后是一个一个的字符插入,
    //转化成对应的数字,调用Insert函数,可支持在线。
    struct SAM
    {
        int ch[maxn][26];
        int pre[maxn],step[maxn];
        int last,id;
        void init()
        {
            last=id=0;
            memset(ch[0],-1,sizeof(ch[0]));
            pre[0]=-1; step[0]=0;
        }
        void Insert(int c) 
        {
            int p=last,np=++id;
            step[np]=step[p]+1;
            memset(ch[np],-1,sizeof(ch[np]));
            while(p!=-1&&ch[p][c]==-1)  ch[p][c]=np,p=pre[p];
            if(p==-1) pre[np]=0;
            else
            {
                int q=ch[p][c];
                if(step[q]!=step[p]+1)
                {
                    int nq=++id;
                    memcpy(ch[nq],ch[q],sizeof(ch[q]));
                    step[nq]=step[p]+1;
                    pre[nq]=pre[q];
                    pre[np]=pre[q]=nq;
                    while(p!=-1&&ch[p][c]==q) ch[p][c]=nq,p=pre[p];
                }
                else pre[np]=q;
            }
            last=np;
            /* 
            //查询出现至少K次的子串数量
            while(np!=-1&&num[np]<K) //没有达到K次的就加1
            {
                num[np]++;
                if(num[np]>=K) ans+=step[np]-step[pre[np]]; //加上答案
                np=pre[np];
            }
            */
        }
        /*
        int GetCnt() //可查询有多少种不同的子串
        {
            int ret=0;
            for(int i=id;i>=1;i--) ret+=step[i]-step[pre[i]];
            return ret;
        }
        */
    
    }sam;
    View Code
  • 相关阅读:
    struts2 的特征
    The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
    web.xml 404 500 配置
    重大发现 springmvc Controller 高级接收参数用法
    struts2 file
    struts2-core-2.3.20.jar
    系统设计工具
    Java基础
    IAM
    秒杀系统
  • 原文地址:https://www.cnblogs.com/wust-ouyangli/p/5802993.html
Copyright © 2011-2022 走看看