acwing 138. The Hare and the Rabbit

Questions surface:

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
题解:

String hash, most notably BKDRHash, that is, turned into a string value, and finally turned into a P values are hexadecimal numbers, in general, the best P is a prime number.
Then the reason why we need the prefix and , This question is because we are seeking a string section, and because the hash table, we have calculated the hash and interval, and because the interval and is, and so we have to use a prefix requirements O (n) pretreatment to achieve general and hash of O (1) constant level query.
this hash common, and the collision probability is very low, offer an essential, indispensable while competition OI party, is an excellent algorithm, easily refined simple to understand.

Code:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define ull unsigned long long
const int base=131;
ull p[1000010],h[1000010];
char str[1000010];
ull get(ull l,ull r)
{
    return h[r]-h[l-1]*p[r-l+1];
}
int main()
{
    int m;
    scanf("%s",str+1);cin>>m;
    int len=strlen(str+1);p[0]=1;
    for(int i=1;i<=len;i++)
    {
        h[i]=h[i-1]*base+(str[i]-'a'+1);
        p[i]=p[i-1]*base;
    }
    ull l1,r1,l2,r2;
    while(m--)
    {
        
        cin>>l1>>r1>>l2>>r2;
        if(get(l1,r1)!=get(l2,r2))
        printf("No\n");
        else
        printf("Yes\n");
    }
}

 

Guess you like

Origin www.cnblogs.com/flyljz/p/11648832.html
138