HDU4622--Reincarnation

Problem Description
Now you are back,and have a task to do:
Given you a string s consist of lower-case English letters only,denote f(s) as the number of distinct sub-string of s.
And you have some query,each time you should calculate f(s[l...r]), s[l...r] means the sub-string of s start from l end at r.

Input
The first line contains integer T(1<=T<=5), denote the number of the test cases.
For each test cases,the first line contains a string s(1 <= length of s <= 2000).
Denote the length of s by n.
The second line contains an integer Q(1 <= Q <= 10000),denote the number of queries.
Then Q lines follows,each lines contains two integer l, r(1 <= l <= r <= n), denote a query.

Output
For each test cases,for each query,print the answer in one line.

Sample Input
  
  
2 bbaba 5 3 4 2 2 2 5 2 4 1 4 baaba 5 3 3 3 4 1 4 3 5 5 5

Sample Output
3
1
7
5
8
1
3
8
5
1

 

 

别人的代码:

#include<iostream>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>

using namespace std;
const int N=5010;

struct State
{
    State *pre,*go[26];
    int step;
    void clear()///clear()函数,显然这个很容易懂
    {
        pre=0;
        step=0;
        memset(go,0,sizeof(go));
    }
    int calc()//返回上一个可接后缀状态的,从根到该状态的最多步数。
    {
        if(pre==0) return 0;
        return step-pre->step;///step - pre -> step 就是添加这个字符所增加的新的以该字符为后缀的字符串。。。但是这个性质为什么成立。
    }
}*root,*last;

State statePool[N*2],*cur;

void init()
{
    cur=statePool;
    root=last=cur++;
    root->clear();
}

int tot;
void Insert(int w)
{
    State *p=last;
    State *np=cur++;
    np->clear();
    np->step=p->step+1;
    while(p && !p->go[w])
        p->go[w] = np,p = p->pre;
	//这时,P接收了后缀字符X,目前已经不可以接收
	//新的后缀字符了,然后,就要处理有X儿子的节点了。
    if(p == 0)
    {
        np->pre = root;
        tot += np->calc();
    }
    else
    {
        State *q = p->go[w];
        if(p->step + 1 == q->step)
        {
            np->pre = q;///用np去顶替q节点,现在q节点是可以接收后缀了。因为np这个节点可以接后缀,因为p可接后缀且和np的父亲有公共的后缀,q和np有公共后缀。。。
            tot += np->calc();/// 现在也就可以理解了。
        }
        else
        {
            State *nq = cur++;
            nq->clear();
            memcpy(nq->go,q->go,sizeof(q->go));
            tot -= p->calc() + q->calc();
            nq->step = p->step + 1;
            nq->pre = q->pre;
            q->pre = nq;
            np->pre = nq;
            tot += p->calc() + q->calc() + np->calc() + nq->calc();因为 子串的计数是 len[x] - len[pre[x]], 因为 q 的 pre[]变化了,所以要先减去之前q的计数,在加上 np 和 nq的计数 
            while(p && p->go[w] == q)
                p->go[w] = nq, p = p->pre;
        }
    }
    last = np;
}

int ans[N][N];
char s[N];

void work()
{
    scanf("%s",s);
    int n=strlen(s);
    for(int i=0;i<n;++i)
    {
        init();
        tot=0;
        for(int j=i;j<n;++j)
        {
            Insert(s[j]-'a');
            ans[i][j]=tot;
        }
    }
    int nQ;
    scanf("%d", &nQ);
    while (nQ--)
    {
        int l, r;
        scanf("%d%d", &l, &r);
        --l,--r;
        printf("%d\n", ans[l][r]);
    }
}

int main()
{
    int T;
    cin>>T;
    while(T--)
        work();
    return 0;
}

 

 

自己的代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 5108
char str[maxn];
struct SAM
{
    SAM *pre,*son[26];
    int len,g;
    int calc()
    {
        return len - pre->len;
    }
}que[maxn<<1],*root,*tail,*b[maxn<<1];
int tot,num;
int ans[maxn][maxn];
void add(int c,int l)
{
    SAM *p=tail,*np=&que[tot++];
    np->len=l;tail=np;
    while(p&&p->son[c]==NULL) p->son[c]=np,p=p->pre;
    if(p==NULL) 
    {
        np->pre=root;
        num += np -> calc();
    }
    else
    {
        SAM *q=p->son[c];
        if(p->len+1==q->len) 
        {
            np->pre=q;
            num += np -> calc();
        }
        else
        {
            SAM *nq=&que[tot++];
            *nq=*q;
            nq->len=p->len+1;
            np->pre=q->pre=nq;
            num += np -> calc();
            while(p&&p->son[c]==q) p->son[c]=nq,p=p->pre;
        }
    }
}

void init(int n)
{
    tot = 0;
    for(int i = 0;i < n;i++)
    {
        que[i].g = 0;    que[i].pre = NULL;
        memset(que[i].son,0,sizeof(que[i].son));
    }
    root = tail = &que[tot++];
}

inline int max(int a,int b)
{
    return a>b?a:b;
}

int main()
{
    //freopen("in.txt","r",stdin);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",str);
        int len = strlen(str);
        for(int i = 0;i < len;i++)
        {
            init(len<<1);
            num = 0;
            for(int j = i;j < len;j++)
            {
                add(str[j] - 'a',j-i+1);
                ans[i][j] = num;
            }
        }
        int q;
        scanf("%d",&q);
        for(int i = 0;i < q;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            u--;v--;
            printf("%d\n",ans[u][v]);
        }
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值