【HDU - 4622】Reincarnation 【字符串HASH+dp 】

本文介绍了一种解决区间内不相同子字符串个数问题的高效算法,通过使用哈希映射和前缀和技巧,该算法能在多个查询中快速找到指定范围内的独特子串数量。

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

Hint
I won’t do anything against hash because I am nice.Of course this problem has a solution that don’t rely on hash.

题意 :给个字符串,q次询问,每次询问区间[l,r]中不相同的子字符串个数。
分析讲解链接

 #include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define  LL long long
#define ULL unsigned long long 
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 2000+11;
const int HASH = 10007;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

struct HASHMAP{
    int head[HASH],next[MAXN],size;
    ULL state[MAXN];int f[MAXN];
    void init(){
        memset(head,-1,sizeof(head));
        size=0;
    }
    int insert(ULL val,int id){// 返回上一个和当前字符串相同的左边界
        int h=val%HASH;
        for(int i=head[h];i!=-1;i=next[i]){
            if(val==state[i]) {
                int temp=f[i];
                f[i]=id;
                return temp;
            }
        }
        f[size]=id; state[size]=val; next[size]=head[h];
        head[h]=size++;
        return 0; //当没有相同的字符串时候,返回0
    }
}H;
const int SEED = 13331;  
ULL P[MAXN],S[MAXN];
char str[MAXN];
int ans[MAXN][MAXN];

int main(){
    CLOSE();
//  fread();
//  fwrite();
    P[0]=1;
    for(int i=1;i<MAXN;i++) P[i]=P[i-1]*SEED;
    int T;scanf("%d",&T);
    while(T--){
        scanf("%s",str); int n=strlen(str);
        S[0]=0;
        for(int i=1;i<=n;i++)
            S[i]=S[i-1]*SEED+str[i-1];

        memset(ans,0,sizeof(ans));
        for(int L=1;L<=n;L++){
            H.init();
            for(int i=1;i+L-1<=n;i++){
                int l=H.insert(S[i+L-1]-S[i-1]*P[L],i);
                ans[i][i+L-1]++;  //当前区间肯定加1
                ans[l][i+L-1]--;//从上一个重复的位置开始到当前的右边界一定重复了,所以要减一。
            }
        } 
        for(int i=n;i>=1;i--)
            for(int j=i+1;j<=n;j++)
                ans[i][j]+=ans[i+1][j]+ans[i][j-1]-ans[i+1][j-1];// 当前的值是由后面的值确定的,所以要倒序来dp更新
        int m,u,v;
        scanf("%d",&m);
        while(m--){
            scanf("%d%d",&u,&v);
            printf("%d\n",ans[u][v]);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值