Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int N = 1e6 + 5;
- const int M = 1002;
- const int prime1 = 13;
- const int mod1 = 1e9 + 7;
- int n;
- char text[N];
- char s[M];
- int binpow(int a, int n, int mod) {
- int res = 1;
- while (n) {
- if (n & 1)
- res = (1ll * res * a) % mod;
- a = (1ll * a * a) % mod;
- n >>= 1;
- }
- return res;
- }
- int main() {
- gets(text);
- int len = strlen(text);
- int n, l;
- scanf("%d%d", &n, &l);
- vector <int> vec;
- int hash1 = 0;
- for (int i = 0; i < l; i++) {
- hash1 = (1ll * text[i] * binpow(prime1, i + 1, mod1) + hash1) % mod1;
- }
- vec.push_back(hash1);
- for (int i = l; i <= len; i++) {
- hash1 = (1ll * hash1 * binpow(prime1, mod1 - 2, mod1)) % mod1;
- hash1 -= text[i - l];
- if (hash1 < 0) hash1 += mod1;
- hash1 = (1ll * text[i] * binpow(prime1, l, mod1) + hash1) % mod1;
- vec.push_back(hash1);
- }
- sort(vec.begin(), vec.end());
- for (int i = 0; i < n; i++) {
- getchar();
- int hs1 = 0;
- for (int j = 0; j < l; j++) {
- scanf("%c", &s[j]);
- hs1 = (1ll * s[j] * binpow(prime1, j + 1, mod1) + hs1) % mod1;
- }
- int lo = 0, hi = (int)vec.size() - 1;
- while (lo <= hi) {
- int md = (lo + hi) >> 1;
- if (vec[md] < hs1)
- lo = md + 1;
- else
- hi = md - 1;
- }
- if (vec[lo] == hs1) {
- puts("Yes");
- } else {
- puts("No");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement