#include using namespace std; using lint = long long int; struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; template bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } // Manacher's Algorithm: radius of palindromes // Input: std::string of length N // Output: std::vector of size N // Complexity: O(N) // Sample: // - `sakanakanandaka` -> [1, 1, 2, 1, 4, 1, 4, 1, 2, 2, 1, 1, 1, 2, 1] // Reference: std::vector manacher(std::string S) { std::vector res(S.length()); int i = 0, j = 0; while (i < (int)S.size()) { while (i - j >= 0 and i + j < (int)S.size() and S[i - j] == S[i + j]) j++; res[i] = j; int k = 1; while (i - k >= 0 and i + k < (int)S.size() and k + res[i - k] < j) res[i + k] = res[i - k], k++; i += k, j -= k; } return res; } int main() { lint N, K, Q; cin >> N >> K >> Q; string S; cin >> S; if (K <= 5) { string T; while (K--) T += S; vector ret = manacher(T); while (Q--) { lint a; cin >> a; printf("%d\n", ret[a - 1] * 2 - 1); } return 0; } vector man3 = manacher(S + S + S); lint LEN = K * N; while (Q--) { lint a; cin >> a; a--; lint ret; if (a < N) { ret = man3[a]; } else if (a >= LEN - N) { ret = man3[a % N + N * 2]; } else { ret = man3[a % N + N]; if (ret >= N) { ret = a + 1; mmin(ret, LEN - a); } } printf("%lld\n", ret * 2 - 1); } }