#include #include "testlib.h" using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < int(n); i++) #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") void validator1(string &X, string &Y, int &Q) { X = inf.readString("[a-z]{1,100000}"); Y = inf.readString("[a-z]{1,100000}"); Q = inf.readInt(1, 50000); inf.readEoln(); } void validator2(int &l, int &r, char &c) { const int MAX = int(1e9); l = inf.readInt(1, MAX); inf.readSpace(); r = inf.readInt(1, MAX); inf.readSpace(); c = inf.readChar(); inf.readEoln(); assert(l <= r); assert(islower(c)); } int n, m; // X, Y の長さ ll len[40]; // F_k の長さ ll F_all[40][26]; // F_k 全体に含まれる c の個数 ll X_L[100010][26], Y_L[100010][26]; // X, Y の先頭 i 文字に含まれる c の個数 // F_k の [0, r) に含まれる c の個数 ll f(int k, int r, int c) { if (k == 0) return X_L[r][c]; assert(r <= len[k]); // F_k = F_{k-1} + Y + rev(F_{k-1}) if (r <= len[k - 1]) return f(k - 1, r, c); else if (r <= len[k - 1] + m) return F_all[k - 1][c] + Y_L[r - len[k - 1]][c]; else return F_all[k - 1][c] + Y_L[m][c] + F_all[k - 1][c] - f(k - 1, len[k] - r, c); } int main(int argc, char *argv[]) { registerValidation(argc, argv); string X, Y; int Q; validator1(X, Y, Q); n = X.length(), m = Y.length(); rep(j, 26) X_L[0][j] = Y_L[0][j] = 0; rep(i, n) rep(j, 26) { X_L[i + 1][j] += X_L[i][j] + (X[i] == j + 'a'); } rep(i, m) rep(j, 26) { Y_L[i + 1][j] += Y_L[i][j] + (Y[i] == j + 'a'); } int k = -1; rep(i, 40) { if (i == 0) { len[i] = n; rep(j, 26) F_all[0][j] = X_L[n][j]; } else { len[i] = len[i - 1] * 2 + m; rep(j, 26) F_all[i][j] = F_all[i - 1][j] * 2 + Y_L[m][j]; } if (len[i] >= 1000000010) { k = i; break; } } assert(k != -1); rep(i, Q) { int l, r; char c; validator2(l, r, c); cout << f(k, r, c - 'a') - f(k, l - 1, c - 'a') << "\n"; } inf.readEof(); }