HDU5685 Problem A 线段树

    xiaoxiao2025-04-16  4

    Problem A

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 838    Accepted Submission(s): 361 Problem Description 度熊手上有一本字典存储了大量的单词,有一次,他把所有单词组成了一个很长很长的字符串。现在麻烦来了,他忘记了原来的字符串都是什么,神奇的是他竟然记得原来那些字符串的哈希值。一个字符串的哈希值,由以下公式计算得到: H(s)=ilen(s)i=1(Si28) (mod 9973) Si 代表 S[i] 字符的 ASCII 码。 请帮助度熊计算大字符串中任意一段的哈希值是多少。   Input 多组测试数据,每组测试数据第一行是一个正整数 N ,代表询问的次数,第二行一个字符串,代表题目中的大字符串,接下来 N 行,每行包含两个正整数 a b ,代表询问的起始位置以及终止位置。 1N1,000 1len(string)100,000 1a,blen(string)   Output 对于每一个询问,输出一个整数值,代表大字符串从 a 位到 b 位的子串的哈希值。   Sample Input 2 ACMlove2015 1 11 8 10 1 testMessage 1 1   Sample Output 6891 9240 88   Source

    2016"百度之星" - 资格赛(Astar Round1) 

    http://acm.hdu.edu.cn/showproblem.php?pid=5685

    // 一看就是线段树 

    // (a*b)%c=(a%c*b%c)%c;

    //没什么好说的直接上代码

    #include<stdio.h> #include<string.h> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 char str[100010]; int tree[100010*4]; void pushup(int rt) { tree[rt]=(tree[rt<<1]*tree[rt<<1|1])%9973; } void build(int l,int r,int rt) { tree[rt]=1; if(l==r) {tree[rt]=str[l];return;} int m=(l+r)>>1; build(lson); build(rson); pushup(rt); } int query(int L,int R,int l,int r,int rt) { if(L<=l&&R>=r) return tree[rt]; int m=(l+r)>>1; int ret=1; if(L<=m) ret*=query(L,R,lson); if(R>m) ret*=query(L,R,rson); return ret%9973; } int main() { int t; while(~scanf("%d",&t)) { int n=t; scanf("%s",str+1); int len=0; for(int i=1;str[i]!='\0';i++) len++; for(int i=1;str[i]!='\0';i++) str[i]=(str[i]-28)%9973; build(1,len,1); int a,b; while(n--) { scanf("%d%d",&a,&b); printf("%d\n",query(a,b,1,len,1)); } } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-1298110.html
    最新回复(0)