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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#include <string> #include <cstdio> #include <random> #include <cassert> #include <iostream> #include <algorithm>
using namespace std;
mt19937_64 mt(random_device{}()); const unsigned long long mod1 = 998244853, mod2 = 1000000009; const unsigned long long b1 = uniform_int_distribution<int>(2, mod1-1)(mt), b2 = uniform_int_distribution<int>(2, mod2-1)(mt); constexpr long long qpow(long long x, long long y, long long m) { long long res = 1; do { if(y & 1) res = res * x % m; x = x * x % m; } while(y >>= 1); return res; } const unsigned long long r1 = qpow(b1, mod1-2, mod1), r2 = qpow(b2, mod2-2, mod2);
unsigned long long h1x[2000005], h2x[2000005], v2ray[2000005]; unsigned long long rsum[100005][30]; unsigned long long vrs[30];
class hasher { public: unsigned long long h1, h2; int len; hasher() : h1(0), h2(0), len(0) {} hasher(const hasher &r) : h1(r.h1), h2(r.h2), len(r.len) {} hasher(const string &s) { h1 = 0; h2 = 0; len = s.size(); for(int i=0;i<s.size();i++) { h1 = (h1 * b1 + (s[i] - 'a')) % mod1; h2 = (h2 * b2 + (s[i] - 'a')) % mod2; } } void push_back(char ch) { h1 = (h1 * b1 + ch - 'a') % mod1; h2 = (h2 * b2 + ch - 'a') % mod2; len++; } void push_front(char ch) { h1 = (h1 + h1x[len] * (ch - 'a')) % mod1; h2 = (h2 + h2x[len] * (ch - 'a')) % mod2; len++; } void pop_back(char ch) { len--; h1 = (h1 - (ch - 'a') + mod1) % mod1 * r1 % mod1; h2 = (h2 - (ch - 'a') + mod2) % mod2 * r2 % mod2; } void pop_front(char ch) { len--; h1 = (h1 - h1x[len] * (ch - 'a') % mod1 + mod1) % mod1; h2 = (h2 - h2x[len] * (ch - 'a') % mod2 + mod2) % mod2; } friend bool operator==(const hasher &x, const hasher &y) { return x.h1 == y.h1 && x.h2 == y.h2 && x.len == y.len; } };
int main() { h1x[0] = h2x[0] = v2ray[0] = 1; for(int i=1;i<=2000000;i++) h1x[i] = h1x[i-1] * b1 % mod1; for(int i=1;i<=2000000;i++) h2x[i] = h2x[i-1] * b2 % mod2; for(int i=1;i<=2000000;i++) v2ray[i] = v2ray[i-1] * 2 % 1000000007; int n, q; scanf("%d%d", &n, &q); string s0, t; cin >> s0 >> t; t = ' ' + t; for(int i=1;i<=n;i++) { for(int j=0;j<26;j++) { rsum[i][j] = rsum[i-1][j] * 2 % 1000000007; } rsum[i][t[i] - 'a'] = (rsum[i][t[i] - 'a'] + 1) % 1000000007; } while(q--) { int k; scanf("%d", &k); string str; cin >> str; string vbase = s0; int vc = 0; while(vbase.size() < str.size() && vc < k) { vbase = vbase + t[++vc] + vbase; } if(vbase.size() < str.size()) { printf("0\n"); continue; } hasher tgt = str; hasher lx = vbase.substr(0, str.size()); long long ans = 0; if(tgt == lx) ans = v2ray[k - vc]; for(int i=str.size();i<vbase.size();i++) { lx.push_back(vbase[i]); lx.pop_front(vbase[i-str.size()]); if(tgt == lx) ans = (ans + v2ray[k - vc]) % 1000000007; } for(int i=0;i<26;i++) { vrs[i] = (rsum[k][i] - rsum[vc][i] * v2ray[k - vc] % 1000000007 + 1000000007) % 1000000007; } lx.pop_front(vbase[vbase.size() - str.size()]); hasher rx; hasher ly = str.substr(0, str.size() - 1), ry; if(lx == ly && rx == ry) ans = (ans + vrs[str.back() - 'a']) % 1000000007; for(int i=1;i<str.size();i++) { rx.push_back(vbase[i-1]); lx.pop_front(vbase[vbase.size() - str.size() + i]); ly.pop_back(str[str.size() - i - 1]); ry.push_front(str[str.size() - i]); if(lx == ly && rx == ry) ans = (ans + vrs[str[str.size()-i-1] - 'a']) % 1000000007; } printf("%lld\n", ans); } return 0; }
|