AcWing:. 138 rabbit and rabbit (the Hash)

Once upon a time, there lived a group of forest rabbit.

One day, the rabbit who want to study their DNA sequence.

We first choose a good long DNA sequence (Little Rabbit is alien, DNA sequences may contain 26 lowercase letters).

Then we each choose two intervals, ask if production in two sections of DNA sequences out two rabbits, two rabbits if exactly the same.

Note that two identical rabbits may only be their DNA sequence identical.

Input Format

A first line of input DNA string S.

A second row of numbers m, m th interrogation.

Subsequently m rows of four numbers  L 1 , R & lt 1 , L 2 , R & lt 2 L1, R1, L2, R2, respectively, the two sections of the query, note the location of the string are numbered starting from one.

Output Format

For each inquiry, one line of output shows the result.

If two rabbits exactly the same output Yes, otherwise output No (note the capitalization).

data range

1length(S),m10000001≤length(S),m≤1000000

Sample input:

aabbaabb
3
1 3 5 7 1 3 6 8 1 2 1 2 

Sample output:

Yes
No
Yes

 

Algorithm: Hash (string)

#include <the iostream> 
#include <cstdio> 
#include <CString> the using namespace STD; 
typedef Long Long LL; const int MAXN = 1e6 + . 7 ;
 const int P = 13 is ;        // choose a fixed binary number P char STR [MAXN]; 
LL F [MAXN]; 
LL P [MAXN]; int main () { 
    Scanf ( " % S " , + STR . 1 ); 
    P [ 0 ] = . 1 ;
     int len = strlen (STR +

 
 

  



1);
    for(int i = 1; i < len; i++) {
        f[i] = f[i - 1] * P + str[i] - 'a' + 1;
        p[i] = p[i - 1] * P;
    }
    int T;
    scanf("%d", &T);
    while(T--) {
        int l1, r1, l2, r2;
        scanf("%d %d %d %d", &l1, &r1, &l2, &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");
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/buhuiflydepig/p/11291439.html