hdu5685

A - Problem A

 

 
Input
多组测试数据,每组测试数据第一行是一个正整数 N N,代表询问的次数,第二行一个字符串,代表题目中的大字符串,接下来 N N行,每行包含两个正整数 a a b b,代表询问的起始位置以及终止位置。 

1N1,000 1≤N≤1,000 

1len(string)100,000 1≤len(string)≤100,000 

1a,blen(string) 1≤a,b≤len(string) 
Output
对于每一个询问,输出一个整数值,代表大字符串从  a a 位到  b b 位的子串的哈希值。
Sample Input
2
ACMlove2015
1 11
8 10
1
testMessage
1 1
Sample Output
6891
9240
88
 
   
AC代码:
    
    
#include<stdio.h>
#include<cstdio>
#include<string.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>
#include<vector>
using namespace std;
const int N=1e5+10;
const int INF=0x3f3f3f3f;
typedef long long ll;
#define lson l,mid,ro<<1
#define rson mid+1,r,ro<<1|1
const int mod=9973;
int tr[N*4];
char s[N];
void Pushup(int ro)
{
        tr[ro]=tr[ro<<1]*tr[ro<<1|1]%mod;
}
void build(int l,int r,int ro)
{
        if(l==r)
        {
                tr[ro]=s[l-1]-28;
                return ;
        }
        int mid=(l+r)/2;
        build(lson);
        build(rson);
        Pushup(ro);
}
int query(int a,int b,int l,int r,int ro)
{
        if(a<=l&&b>=r)
                return tr[ro];
        int mid=(l+r)/2;
        if(b<=mid)
                return query(a,b,lson);
        else if(a>mid)
                return query(a,b,rson);
        else
                return query(a,mid,lson)*query(mid+1,b,rson)%mod;
}
int main()
{
        int m;
        while(scanf("%d",&m)!=EOF)
        {
                scanf("%s",s);
                int n=strlen(s);
                build(1,n,1);
                while(m--)
                {
                        int a,b;
                        scanf("%d%d",&a,&b);
                        printf("%d\n",query(a,b,1,n,1));
                }
        }
        return 0;
}

      
      

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值