#include #include #include #include #include #include int main() { long long n; scanf("%lld", &n); std::vector> s(n); std::vector len(n); for (int i = 0; i < n; i++) { static char buf[800100]; scanf("%s", buf); len[i] = strlen(buf); s[i].resize((len[i] + 11) / 12); for (int j = 0; j < len[i]; j += 12) { long long val = 0; for (int k = 0; k < 12; k++) { val <<= 5; if (buf[j + k] != 0) { val |= buf[j + k] - 'a' + 1; } } s[i][j / 12] = val; } } std::vector perm(n); for (int i = 0; i < n; i++) { perm[i] = i; } std::sort(perm.begin(), perm.end(), [&](int i, int j) { return s[i] < s[j]; }); std::vector iperm(n); for (int i = 0; i < n; i++) { iperm[perm[i]] = i; } std::vector lcp(n - 1); for (int i = 0; i < n - 1; i++) { const int l = perm[i]; const int r = perm[i + 1]; const int nl = s[l].size(); const int nr = s[r].size(); int h = 0; while (h < std::min(nl, nr) && s[l][h] == s[r][h]) { h++; } if (h == nl) { lcp[i] = len[l]; } else if (h == nr) { lcp[i] = len[r]; } else { long long v = s[l][h] ^ s[r][h]; int j; for (j = 0; j < 12; j++) { if (v >> (5 * (11 - j)) & 0x1f) { break; } } lcp[i] = h * 12 + j; } } std::vector lg(lcp.size() + 1); for (int i = 2; i < lg.size(); i++) { lg[i] = lg[i / 2] + 1; } std::vector> rmq(lg[lcp.size()] + 1, std::vector(lcp.size())); for (int i = 0; i < lcp.size(); i++) { rmq[0][i] = lcp[i]; } for (int i = 0; i + 1 < rmq.size(); i++) { for (int j = 0; j + (1 << i) < rmq[i].size(); j++) { rmq[i + 1][j] = std::min(rmq[i][j], rmq[i][j + (1 << i)]); } } long long ans = 0; int m; long long x, d; scanf("%d %lld %lld", &m, &x, &d); long long ii = x % (n - 1); long long jj = x / (n - 1); long long di = d % (n - 1); long long dj = d / (n - 1); while (m--) { int i = jj; int j = ii; if (i > j) { std::swap(i, j); } else { j++; } ii += di; if (ii >= n - 1) { ii -= n - 1; jj++; } jj += dj; if (jj >= n) { jj -= n; } i = iperm[i]; j = iperm[j]; if (i > j) { std::swap(i, j); } const int k = lg[j - i]; ans += std::min(rmq[k][i], rmq[k][j - (1 << k)]); } printf("%lld\n", ans); }