ZCMU — 2155

2155: F.ly's hamster

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 61   Solved: 18
[ Submit][ Status][ Web Board]

Description

After finishing the physical education class, Ly was very boring, so I was going to take a clinical course. Just today they were going to study some hamster genes. Due to some mistakes in the operation...ly got a very long gene chain (both It is composed of lowercase letters). As a person with strong curiosity, ly has a question. When taking two intervals each time, will the hamsters produced from the DNA of these two intervals be two identical hamsters?
Obviously Little Fatty ly has curiosity and does not have the ability to get results... only questions will not answer, so I have to trouble you to help her again.

Input

Enter two positive integers n and q on the first line, which represent the length of the DNA string and the number of queries.
In the second line, a string S represents the DNA sequence obtained by ly.
Enter four integers l1, r1, l2, and r2 in each line of the following q lines, respectively representing q queries. For each query, please judge two [l,r] Whether the DNA of the interval is the same.

Output

For each query, the same output YES is not the same output NO

Sample Input

10 5
abcaabasd
1 3 2 4
4 4 5 5
1 2 5 6
1 5 6 1 0
1 9 2 10

Sample Output

NO
YES
YES
NO
NO

HINT

Input data guarantee: 1<=length(S),q<=1000000

【analysis】

The original intent of the question was hashed, but maybe I didn’t expect the 10W^2 data to get stuck....Probably the OJ server of our school is a bit powerful....The data that needs to run for ten seconds of local violence hangs on OJ for 0.1 second. It's over....gg, although there may be a problem with time, you can still learn the idea

Get to the point is enough.... just do the calculations, just calculate the number, and then remember the unsigned type, otherwise the multiplication will explode...If the high-end is higher, then hang a high-precision hash... I'm just a little lazy. ..

【Code】

#include<map>
#include<list>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cctype>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define qread(x) x=read()
#define mes(x,y) memset(x,y,sizeof(x))
#define mpy(x,y) memcpy(x,y,sizeof(x))
#define Maxn 1000000
#define INF 2147483647
inline int read(){
    int f=1,x=0;char ch;
    while(!(ch>='0'&&ch<='9')){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return f*x;
}
char s[Maxn+1];
unsigned long long f[Maxn+1],p[Maxn+1];
int n,q,l1,r1,l2,r2;
int main(){
    qread(n),qread(q);
    scanf("%s",s+1);
    p[0]=1;
    for(int i=1;i<=n;i++){
        f[i]=f[i-1]*131+(s[i]-'a'+1);
        p[i]=p[i-1]*131;
    }
    for(int i=1;i<=q;i++){
        qread(l1),qread(r1),qread(l2),qread(r2);
        if((f[r1]-f[l1-1]*p[r1-l1+1])==(f[r2]-f[l2-1]*p[r2-l2+1]))printf("YES\n");
        else printf("NO\n");
    }
}


Guess you like

Origin blog.csdn.net/jnxxhzz/article/details/80691567