HDU 4622(后缀自动机)

(http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105904#problem/B)
题意:串s,|s|<=2000,Q组查询,查询s(l,r)中有多少不想同子串。
解法:O(n^2)做法。分别从0~len-1为起点作后缀自动机。因为后缀自动机是支持 增 量的。每次加入一个点np,step[np] - step[pre[np]]即为增加的字符串数。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <queue>
//#include <tr1/unordered_set>
//#include <tr1/unordered_map>
#include <bitset>
//#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define inf 1e9
#define debug(a) cout << #a" = " << (a) << endl;
#define debugarry(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }
#define clr(x, y) memset(x, y, sizeof x)
#define ll long long
#define ull unsigned long long
#define FOR(i,a,b) \
    for(i=a;a<b?i<=b:i>=b;a<b?i++:i--)

const int maxn =  2000*4+100;

struct suffix_automaton{
    int son[maxn][26],pre[maxn],step[maxn];
    int last,si;
    void pushback(int v)
    {
        step[++si]=v;
        pre[si]=-1;
        clr(son[si],-1);
    }
    int Extend(int ch)
    {
        pushback(step[last]+1);
        int p=last,np=si;
        while(p!=-1&&son[p][ch]==-1)
            son[p][ch]=np,
            p=pre[p];
        if(p==-1) pre[np]=0;
        else{
            int q=son[p][ch];
            if( step[q]==step[p]+1 ) pre[np]=q;
            else{
                pushback(step[p]+1);
                int nq=si;
                memcpy(son[nq],son[q],sizeof son[q]);
                pre[nq]=pre[q];
                pre[q]=pre[np]=nq;
                while(p!=-1&&son[p][ch]==q)
                    son[p][ch]=nq,
                    p=pre[p];
            }
        }
        last=np;
        if(pre[np]==-1) return step[np];
        else return step[np]-step[pre[np]];
    }
    void init()
    {
        si=last=0;
        clr(son[0],-1);
        pre[0]=-1;
        step[0]=0;
    }
}su;

char s[maxn];
int ans[2000+30][2000+30];

int main()
{
    //freopen("input.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%s",s);
        int len=strlen(s);
        for(int i=0;i<len;i++){
            su.init();
            int t=0;
            for(int j=i;j<len;j++)
            {
                t += su.Extend(s[j]-'a');
                ans[i][j]=t;
            }
        }
        int q,l,r;
        scanf("%d",&q);
        while(q--){
            scanf("%d%d",&l,&r);
            printf("%d\n",ans[l-1][r-1]);
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值