#pragma region Tmpl #include // clang-format off #define pb push_back #define eb emplace_back #define fi first #define se second #define each(x,v) for(auto& x : v) #define all(v) (v).begin(),(v).end() #define sz(v) ((int)(v).size()) #define ini(...) int __VA_ARGS__; in(__VA_ARGS__) #define inl(...) long long __VA_ARGS__; in(__VA_ARGS__) #define ins(...) string __VA_ARGS__; in(__VA_ARGS__) #define inc(...) char __VA_ARGS__; in(__VA_ARGS__) #define in2(s,t) rep(i,sz(s)){in(s[i] , t[i]);} #define in3(s,t,u) rep(i,sz(s)){in(s[i] , t[i] , u[i]);} #define in4(s,t,u,v) rep(i,sz(s)){in(s[i] , t[i] , u[i] , v[i]);} #ifdef ONLINE_JUDGE #define rep(i,N) for(int i = 0; i < (int)(N); i++) #define repr(i,N) for(int i = (int)(N) - 1; i >= 0; i--) #define rep1(i,N) for(int i = 1; i <= (int)(N) ; i++) #define repr1(i,N) for(int i = (N) ; (int)(i) > 0 ; i--) #else #define rep(i,N) for(long long i = 0; i < (long long)(N); i++) #define repr(i,N) for(long long i = (long long)(N) - 1; i >= 0; i--) #define rep1(i,N) for(long long i = 1; i <= (long long)(N) ; i++) #define repr1(i,N) for(long long i = (N) ; (long long)(i) > 0 ; i--) #endif using namespace std; void solve(); using ll = long long; template using V = vector; using vi = V; using vl = V<>; using vvi = V< V >; using vd = V; using vs = V; using vvl = V< V<> >; constexpr int inf = 1001001001; constexpr ll infLL = (1LL << 61) - 1; template inline bool amin(T &x, U y) { return (y < x) ? (x = y, true) : false; } template inline bool amax(T &x, U y) { return (x < y) ? (x = y, true) : false; } templatell ceil(T a,U b){return (a + b - 1) / b;} template ostream& operator <<(ostream& os, const pair &p) { os << p.first << " " << p.second; return os; } template istream& operator >>(istream& is, pair &p) { is >> p.first >> p.second; return is; } template ostream& operator <<(ostream& os, const vector &v) { int s = (int)v.size(); for(int i=0;i istream& operator >>(istream& is, vector &v) { for(auto &x : v) is >> x; return is; } void in(){} template void in(T &t,U &...u){ cin >> t; in(u...);} void out(){cout << "\n";} template void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);} templatevoid die(T x){out(x); exit(0);} #ifdef NyaanDebug #include "NyaanDebug.h" #define trc(...) do { cerr << #__VA_ARGS__ << " = "; dbg_out(__VA_ARGS__);} while(0) #define trca(v,N) do { cerr << #v << " = "; array_out(v , N);} while(0) #define trcc(v) do { cerr << "name : " << #v << "\n"; int cnt = 0; each(x , v){cerr << (cnt++) << " : "; trc(x); } } while(0) #else #define trc(...) #define trca(...) #define trcc(...) int main(){solve();} #endif constexpr ll TEN(int n){ll ret=1,x=10;while(n){if(n&1)ret*=x;x*=x;n>>=1;}return ret;} #define mem(a, val) memset(a, val, sizeof(a)) #pragma endregion struct IoSetupNya {IoSetupNya() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(7);} } iosetupnya; using P = pair; using vp = V

; constexpr uint32_t MOD = /** 1000000007; //*/ 998244353; // clang-format on //////////////////////////////////////////////////// // Suffix Array //verify https://judge.yosupo.jp/submission/240 struct SuffixArray{ int _size; vector sa; string &s; SuffixArray(string &str):_size(str.size()) , s(str) { // うしさんのO( N logN )の実装 s.push_back(0); sa.resize(s.size()); iota(begin(sa), end(sa), 0); sort(begin(sa), end(sa), [&](int a, int b) { return s[a] == s[b] ? a > b : s[a] < s[b]; }); vector< int > classes(s.size()), c(s.begin(), s.end()), cnt(s.size()); for(int len = 1; len < (int)s.size(); len <<= 1) { for(int i = 0; i < (int)s.size(); i++) { if(i > 0 && c[sa[i - 1]] == c[sa[i]] && sa[i - 1] + len < (int)s.size() && c[sa[i - 1] + len / 2] == c[sa[i] + len / 2]) { classes[sa[i]] = classes[sa[i - 1]]; } else { classes[sa[i]] = i; } } iota(begin(cnt), end(cnt), 0); copy(begin(sa), end(sa), begin(c)); for(int i = 0; i < (int)s.size(); i++) { int s1 = c[i] - len; if(s1 >= 0) sa[cnt[classes[s1]]++] = s1; } classes.swap(c); } s.pop_back(); } // デバッグ用に実装 void output() { cout << "SA\tidx\tstr" << endl; for(int i = 0; i < size(); i++) { cout << i << ": \t" << sa[i] << " \t" ; if(sa[i] != _size) cout << s.substr(sa[i],_size - sa[i]) << endl; else cout << "$" << endl; } cout << endl; } // sa.size()と表せると便利なので実装 int size() const{return _size + 1;} // sa[]と表せると便利なのでオーバーロードしておく int operator[](int k) const{return sa[k]; } }; struct LCPArray { const SuffixArray &SA; vector LCP, rank; LCPArray(const SuffixArray &sa) : SA(sa) { LCP.resize(SA.size()); rank.resize(SA.size()); // 初期化 rankはsaの逆関数 for(int i = 0; i < SA.size(); i++) { rank[SA[i]] = i; } LCP[0] = 0; // 構築 for(int i = 0, h = 0; i < SA.size() - 1 ; i++) { int j = SA[rank[i] - 1] ; h ? h-- : h; // ここで尺取り法に近い手法を使うことでO(N)でLCPの構築をしている while( (i > j ? i : j) + h < SA.size() - 1 && SA.s[i + h] == SA.s[j + h] && ++h ); LCP[rank[i] - 1] = h; } } // デバッグ用に実装 void output() { cout << "SA\tidx\tLCP\tstr" << endl; for(int i = 0 ; i < SA.size() ; i++){ cout << i << "\t" << SA[i] <<" \t" << LCP[i] << "\t"; if(SA[i] == SA.size() - 1) cout << "$"; else cout << SA.s.substr(SA[i] , SA.size() - 1 - SA[i]); cout << endl; } } }; // Sparse Table template struct SparseTable{ vector< vector< T > > table; vector< int > log_table; SparseTable(const vector< T > &v) { int b = 0; while((1 << b) <= (int)v.size()) ++b; table.assign(b, vector< T >(1 << b)); for(int i = 0; i < (int)v.size(); i++) { table[0][i] = v[i]; } for(int i = 1; i < b; i++) { for(int j = 0; j + (1 << i) <= (1 << b); j++) { table[i][j] = min(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]); } } log_table.resize(v.size() + 1); for(int i = 2; i < (int)log_table.size(); i++) { log_table[i] = log_table[i >> 1] + 1; } } // 区間 [l , r) の最小値を返す inline T query(int l, int r) { int b = log_table[r - l]; return min(table[b][l], table[b][r - (1 << b)]); } }; // 文字列検索 検索 O(M + logN) メモリO(N logN) // verify // https://onlinejudge.u-aizu.ac.jp/status/users/NyaanNyaan/submissions/1/ALDS1_14_D/judge/3874273/C++14 // https://atcoder.jp/contests/abc135/submissions/7574225 // https://judge.yosupo.jp/submission/241 // https://atcoder.jp/contests/abc141/submissions/7577295 struct StringSearch{ string &s; const SuffixArray &sa; const LCPArray &lcp; SparseTable sparse; StringSearch(LCPArray &lcp) : s(lcp.SA.s) , sa(lcp.SA) , lcp(lcp) , sparse(lcp.LCP){ } // 文字列sの[i , N)と[j , N)の共通接頭辞の長さを求める int ArbitaryLCP(int i , int j){ if(i == j) return (int)(s.size()) - i; return sparse.query( min(lcp.rank[i] , lcp.rank[j]) , max(lcp.rank[i] , lcp.rank[j]) ); } pair comp(const string &t , int len , int si , int ti = 0){ int sn = (int)s.size() , tn = (int) t.size(); si += len , ti += len; while(si < sn && ti < tn){ if(s[si] != t[ti]) return make_pair( s[si]=sn && ti find_range(int left , int med , int right , int len){ { int ng = left - 1, ok = med; while(ng + 1 < ok){ int cur = (ng + ok) / 2; if(sparse.query(cur , med) >= len) ok = cur; else ng = cur; } left = ok; } { int ok = med , ng = right + 1; while(ok + 1 < ng){ int cur = (ng + ok) / 2; if(sparse.query(med, cur) >= len) ok = cur; else ng = cur; } right = ok; } return make_pair(left , right); } // 全ての出現範囲をSA上の[left , right]の範囲で返す // 存在しない場合は-1を返す pair find(string &t){ // 条件を満たす[left , right]を見つける // sa[0]は空文字列なので left = 1 とする // lenは既に一致している文字列の長さ int left = 1 , right = sa.size() - 1 , med = left; int leftlen = 0 , rightlen = 0 , tlen = t.size(); pair ret; while(left + 1 < right){ med = (left + right) / 2; int corres_len = max( min(leftlen , sparse.query(left , med)) , min(rightlen, sparse.query(med , right)) ); if(corres_len < max(leftlen , rightlen)){ if(leftlen < rightlen) left = med , leftlen = corres_len; else right= med ,rightlen = corres_len; continue; } ret = comp(t , corres_len , sa[med]); //trc(left,med,right,ret); if(ret.second == tlen) return find_range(left,med,right,tlen); if(ret.first == 0) right = med , rightlen = ret.second; else left = med , leftlen = ret.second; } if(sa.size() <= 3){ if(comp(t,0,sa[left]).second==tlen) return find_range(left,left,right,tlen); if(comp(t,0,sa[right]).second==tlen) return find_range(left,right,right,tlen); return make_pair(-1,-1); } med = left + right - med; ret = comp(t , min(leftlen,rightlen) , sa[med]); //trc(left,med,right,ret); if(ret.second == tlen) return find_range(left,med,right,tlen); return make_pair(-1,-1); } }; // Suffix Arrayの使い方(メモリを食うので必要なものだけ使う) // 参照があるのでstringを削除などしないこと /* SuffixArray sa(S); LCPArray lcp(sa); StringSearch search(lcp); */ void solve(){ inl(N,M,Q); ins(S); vl K(Q); in(K); // 別に処理 if(M == 1){ SuffixArray sa(S); vi ans(Q); rep(i,Q){ ans[i] = sa.sa[K[i]] + 1; } out(ans); exit(0); } // S += S; SuffixArray sa(S); // 前から見ていく vl ans(Q); ll ki = 0; ll cur = 1; each(x , sa.sa){ if(ki == Q) break; if(x == sz(S)) continue; // 次のものがcur番目 // nxtは? ll nxt; // 短い方 if(x >= N) nxt = cur + 1; // 長い方 else nxt = cur + M - 1; while(ki != Q && K[ki] < nxt){ if(x >= N) ans[ki] = N * M - 2 * N + x + 1; // cur が N*M - 2 * N + x + 1 // 1増えるごとにN後退していく else ans[ki] = N*M - 2*N + x + 1 - (K[ki]-cur)*N; ki++; } cur = nxt; } out(ans); }