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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
| #include <iostream> #include <ctime> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <climits> #include <queue> #include <set> #include <cmath> #include <map> #include <bitset> #include <fstream> #include <tr1/unordered_map> #include <assert.h>
#define x first #define y second #define y0 Y0 #define y1 Y1 #define mp make_pair #define pb push_back #define DEBUG(x) cout << #x << " = " << x << endl;
using namespace std;
typedef long long LL; typedef pair <int, int> pii;
template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; } template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; } template <typename T> inline T read () { T sum = 0, fl = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1; for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0'; return sum * fl; }
inline void proc_status () { ifstream t ("/proc/self/status"); cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) << endl; }
const int MAXN = 1e5; const int LIM = sqrt (MAXN);
int N, M, Q, K; char S[MAXN + 5];
namespace SAM { const int MAXN = ::MAXN * 2;
struct info { int fa, ch[26], maxlen, size; } node[MAXN + 5];
int node_cnt = 1, lst = 1;
inline int new_node (int pre) { int o = ++node_cnt; node[o].maxlen = node[pre].maxlen + 1; node[o].size = 1; return o; }
inline void extend (int c) { int o = new_node (lst), pre = lst; lst = o;
for (; pre && !node[pre].ch[c]; pre = node[pre].fa) node[pre].ch[c] = o; if (!pre) node[o].fa = 1; else { int x = node[pre].ch[c]; if (node[x].maxlen == node[pre].maxlen + 1) node[o].fa = x; else { int y = ++node_cnt; node[y] = node[x], node[y].maxlen = node[pre].maxlen + 1; node[y].size = 0, node[x].fa = node[o].fa = y; for (; node[pre].ch[c] == x; pre = node[pre].fa) node[pre].ch[c] = y; } } }
vector <int> G[MAXN + 5];
const int MAX_LOG = log2(MAXN); int anc[MAX_LOG + 1][MAXN + 5];
inline void dfs (int x) { for (int i = 1; 1 << i <= N; ++i) anc[i][x] = anc[i - 1][anc[i - 1][x]];
for (int i = 0; i < G[x].size(); ++i) { int y = G[x][i]; dfs (y); node[x].size += node[y].size; } } inline void build () { for (int i = 1; i <= N; ++i) extend (S[i] - 'a'); for (int i = 1; i <= node_cnt; ++i) G[node[i].fa].pb (i), anc[0][i] = node[i].fa; dfs (1); }
inline void match (char *S, int *O, int *Len) { int o = 1, len = 0; for (int i = 1; i <= K; ++i) { int c = S[i] - 'a'; while (o > 1 && !node[o].ch[c]) o = node[o].fa, len = node[o].maxlen;
if (node[o].ch[c]) o = node[o].ch[c], O[i] = o, ++len; else O[i] = 0;
Len[i] = len; } }
inline int get_anc (int o, int len) { for (int i = MAX_LOG; i >= 0; --i) if (node[anc[i][o]].maxlen >= len) o = anc[i][o]; return o; } }
namespace S1 { vector <int> vec[LIM + 5][LIM + 5];
inline void main () { for (int i = 1; i <= M; ++i) { int l = read<int>() + 1, r = read<int>() + 1; vec[l][r].pb (i); }
while (Q--) { static char S[LIM + 5]; scanf("%s", S + 1); int l = read<int>() + 1, r = read<int>() + 1; LL ans = 0; for (int i = 1; i <= K; ++i) for (int j = i, o = 1; j <= K; ++j) { o = SAM :: node[o].ch[S[j] - 'a']; if (!o) break; int x = lower_bound (vec[i][j].begin(), vec[i][j].end(), l) - vec[i][j].begin(); int y = upper_bound (vec[i][j].begin(), vec[i][j].end(), r) - vec[i][j].begin() - 1; ans += (LL) SAM :: node[o].size * max (0, y - x + 1); } printf("%lld\n", ans); } } }
namespace S2 { int L[MAXN + 5], R[MAXN + 5];
inline void main () { for (int i = 1; i <= M; ++i) L[i] = read<int>() + 1, R[i] = read<int>() + 1; while (Q--) { static char S[MAXN + 5]; scanf("%s", S + 1); int x = read<int>() + 1, y = read<int>() + 1;
static int O[MAXN + 5], Len[MAXN + 5]; SAM :: match (S, O, Len);
LL ans = 0; for (int i = x; i <= y; ++i) { int l = L[i], r = R[i]; if (r - l + 1 > Len[r]) continue; int o = SAM :: get_anc (O[r], r - l + 1);
ans += SAM :: node[o].size; }
printf("%lld\n", ans); } } }
inline void Solve () { SAM :: build (); if (K <= LIM) S1 :: main (); else S2 :: main (); }
inline void Input () { N = read<int>(), M = read<int>(), Q = read<int>(), K = read<int>(); scanf("%s", S + 1); }
int main () {
#ifdef hk_cnyali freopen("A.in", "r", stdin); freopen("A.out", "w", stdout); #endif
Input (); Solve ();
return 0; }
|