-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathA.cpp
50 lines (37 loc) · 968 Bytes
/
A.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
using namespace std;
size_t m;
string s;
const long long PRIME = 1000000009;
vector<unsigned long long> primePows;
vector<unsigned long long> hashes;
unsigned long long getHash(int l, int r) {
return hashes[r + 1] - hashes[l] * primePows[r - l + 1];
}
bool checkTwoSubs(int a, int b, int c, int d) {
return getHash(a, b) == getHash(c, d);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> s;
cin >> m;
hashes.resize(s.length() + 1);
primePows.resize(s.length() + 1);
hashes[0] = 0;
primePows[0] = 1;
for (int i = 0; i < s.length(); ++i) {
hashes[i + 1] = hashes[i] * PRIME + s[i];
primePows[i + 1] = primePows[i] * PRIME;
}
for (int i = 0; i < m; ++i) {
int a, b, c, d;
cin >> a >> b >> c >> d;
a--;
b--;
c--;
d--;
cout << (checkTwoSubs(a, b, c, d) ? "Yes\n" : "No\n");
}
return 0;
}