zoukankan      html  css  js  c++  java
  • HDU4622 Reincarnation

    题目链接:戳我

    因为对应的很多询问,所以我们一定要将每一种询问先处理出来,然后O(1)查询。

    至于怎么处理出来子串的子串呢?

    我们固定左端点,然后依次加入子串即可。然后统计的时候直接统计last那一个类的即可(因为只有last是真正新建出来的节点,多出来的本质不同的子串也是出现在这里面的),我们直接(t[i].longest-t[i].shortest+1=t[i].len-t[t[i].ff].len)即可。

    代码如下:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #define MAXN 2010
    using namespace std;
    int last=1,tot=1,T,len,n,m;
    int ans[MAXN][MAXN];
    char s[MAXN];
    struct Node{int len,ff,ch[26];}t[MAXN<<1];
    inline void extend(int c)
    {
        int p=last,np=++tot;last=np;
        t[np].len=t[p].len+1;
        while(p&&!t[p].ch[c]) t[p].ch[c]=np,p=t[p].ff;
        if(!p) t[np].ff=1;
        else
        {
            int q=t[p].ch[c];
            if(t[q].len==t[p].len+1) t[np].ff=q;
            else
            {
                int nq=++tot;
                t[nq]=t[q],t[nq].len=t[p].len+1;
                t[np].ff=t[q].ff=nq;
                while(p&&t[p].ch[c]==q) t[p].ch[c]=nq,p=t[p].ff;
            }
        }
    }
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("ce.in","r",stdin);
        #endif
        scanf("%d",&T);
        while(T--)
        {
            memset(ans,0,sizeof(ans));
            last=tot=1;
            scanf("%s",s+1);
            len=strlen(s+1);
            for(int i=1;i<=len;i++)
            {
                memset(t,0,sizeof(t));
                tot=last=1;
                for(int j=i;j<=len;j++)
                {
                    extend(s[j]-'a'+1);
                    ans[i][j]=ans[i][j-1]+t[last].len-t[t[last].ff].len;
                }
            }
            scanf("%d",&m);
            for(int i=1;i<=m;i++)
            {
                int l,r;
                scanf("%d%d",&l,&r);
                printf("%d
    ",ans[l][r]);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    ASP.NET Core 个人新闻项目
    C# 检查字符串中是否有HTML标签、返回过滤掉所有的HTML标签后的字符串
    VueCLI 页面加载进度条效果
    replace() 方法使用
    CentOS 7.9安装教程
    在Windows中安装MySQL
    linux安装consul
    jenkins Skywalking安装部署文档总结
    CentOS 7.x安装.NET运行时
    Apollo部署文档
  • 原文地址:https://www.cnblogs.com/fengxunling/p/10749999.html
Copyright © 2011-2022 走看看