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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
#include <bits/stdc++.h>
namespace IO {
inline char read() { static const int IN_LEN = 1000000; static char buf[IN_LEN], *s, *t; s == t ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin) : 0; return s == t ? -1 : *s++; }
template <typename T> inline void read(T &x) { static char c; static bool iosig; for (c = read(), iosig = false; !isdigit(c); c = read()) { if (c == -1) return; c == '-' ? iosig = true : 0; } for (x = 0; isdigit(c); c = read()) x = (x + (x << 2) << 1) + (c ^ '0'); iosig ? x = -x : 0; }
inline int read(char *buf) { register int s = 0; register char c; while (c = read(), isspace(c) && c != -1) ; if (c == -1) { *buf = 0; return -1; } do buf[s++] = c; while (c = read(), !isspace(c) && c != -1); buf[s] = 0; return s; }
const int OUT_LEN = 1000000;
char obuf[OUT_LEN], *oh = obuf;
inline void print(char c) { oh == obuf + OUT_LEN ? (fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf) : 0; *oh++ = c; }
template <typename T> inline void print(T x) { static int buf[30], cnt; if (x == 0) { print('0'); } else { x < 0 ? (print('-'), x = -x) : 0; for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 | 48; while (cnt) print((char)buf[cnt--]); } }
inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); } }
namespace Task {
const int MAXN = 50000; const int MAXM = 100;
char s[MAXN + 1], t[MAXM + 1];
typedef unsigned int uint; typedef unsigned long long ulong;
const uint HASH_BASE = 31; uint hashS[MAXN + 1], hashT[MAXM + 1], pow31[MAXN + 1]; uint f[2][MAXM + 1][MAXN + 1];
ulong sumL[MAXN + 1], sumR[MAXN + 1], numL[MAXN + 1], numR[MAXN + 1];
inline uint hash(uint *h, int l, int r) { return h[r] - h[l - 1] * pow31[r - l + 1]; }
inline void initPow() { pow31[0] = 1, pow31[1] = HASH_BASE; for (register int i = 2; i <= MAXN; i++) pow31[i] = pow31[i - 1] * HASH_BASE; }
inline void init(int n, int m) { for (register int i = 1; i <= n; i++) hashS[i] = hashS[i - 1] * HASH_BASE + s[i]; for (register int i = 1; i <= m; i++) hashT[i] = hashT[i - 1] * HASH_BASE + t[i]; for (register int i = 1; i <= m - 1; i++) { for (register int j = 1; j + i - 1 <= n; j++) if (hash(hashT, 1, i) == hash(hashS, j, j + i - 1)) f[0][i][j + i - 1]++; for (register int j = 1; j + (m - i) - 1 <= n; j++) if (hash(hashT, i + 1, m) == hash(hashS, j, j + (m - i) - 1)) f[1][i][j]++; for (register int j = 1; j <= n; j++) f[0][i][j] += f[0][i][j - 1]; for (register int j = n; j >= 1; j--) f[1][i][j] += f[1][i][j + 1]; } for (register int i = 1; i + m - 1 <= n; i++) { if (hash(hashT, 1, m) == hash(hashS, i, i + m - 1)) { sumR[i + m - 1] += i + m - 1, sumL[i] += i; numR[i + m - 1]++, numL[i]++; } } for (register int i = 1; i <= n; i++) sumR[i] += sumR[i - 1], numR[i] += numR[i - 1]; for (register int i = n; i >= 1; i--) sumL[i] += sumL[i + 1], numL[i] += numL[i + 1]; }
inline void query(int l, int r, int n, int m) { using namespace IO; register ulong ret = (l + 1) * (n - r + 1) * numR[l] - (n - r + 1) * sumR[l] + (1 - r) * l * numL[r] + l * sumL[r];
for (register int j = 1; j < m; j++) ret += (ulong)f[0][j][l] * f[1][j][r]; print(ret), print('\n'); }
inline void solve() { using namespace IO; initPow(); register int T, n, m, k; for (read(T); T--;) { read(n), read(m), read(k), read(s + 1), read(t + 1), init(n, m); for (register int l, r; k; k--) read(l), read(r), query(l, r, n, m); memset(sumL, 0, sizeof(ulong) * (n + 1)); memset(sumR, 0, sizeof(ulong) * (n + 1)); memset(numL, 0, sizeof(ulong) * (n + 1)); memset(numR, 0, sizeof(ulong) * (n + 1)); memset(f, 0, sizeof(f)); } } }
int main() { freopen("lianhuan.in", "r", stdin); freopen("lianhuan.out", "w", stdout); Task::solve(); IO::flush(); return 0; }
|