結果
| 問題 |
No.1018 suffixsuffixsuffix
|
| コンテスト | |
| ユーザー |
beet
|
| 提出日時 | 2020-04-02 11:40:51 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,199 bytes |
| コンパイル時間 | 2,541 ms |
| コンパイル使用メモリ | 206,984 KB |
| 最終ジャッジ日時 | 2025-01-09 12:30:16 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 23 TLE * 11 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
using Int = long long;
const char newl = '\n';
// longest common prefix of s and s[i:n]
template<typename T>
vector<int> zalgorithm(vector<T> vs){
int n=vs.size();
vector<int> as(n+1,0);
as[0]=n;
int i=1,j=0;
while(i<n){
while(i+j<n&&vs[j]==vs[i+j]) j++;
as[i]=j;
if(j==0){
i++;
continue;
}
int k=1;
while(i+k<n&&k+as[k]<j) as[i+k]=as[k],k++;
i+=k;
j-=k;
}
return as;
}
vector<int> zalgorithm(string s){
return zalgorithm(vector<char>(s.begin(),s.end()));
}
struct SuffixArray{
string s;
vector<int> sa,rev;
SuffixArray(){}
SuffixArray(const string &S):s(S){
int n=s.size();
s.push_back('$');
sa.resize(n+1);
iota(sa.begin(),sa.end(),0);
sort(sa.begin(),sa.end(),
[&](int a,int b){
if(a==b) return false;
while(s[a]==s[b]) a++,b++;
return s[a]<s[b];
});
}
};
//INSERT ABOVE HERE
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
using ll = long long;
ll n,m,q;
cin>>n>>m>>q;
string s;
cin>>s;
vector<ll> ks(q);
for(int i=0;i<q;i++) cin>>ks[i];
auto zs=zalgorithm(s+s);
for(int i=1;i<=n;i++){
if(n%i) continue;
if(zs[i]>=n){
m*=n/i;
s=s.substr(0,i);
break;
}
}
n=s.size();
if(n==1){
for(int i=0;i<q;i++){
if(i) cout<<" ";
cout<<m-ks[i]+1;
}
cout<<endl;
return 0;
}
if(m==1){
SuffixArray sa(s);
for(int i=0;i<q;i++){
if(i) cout<<" ";
cout<<1+sa.sa[ks[i]];
}
cout<<endl;
return 0;
}
SuffixArray sa(s+s);
vector<ll> ans(q,-1);
ll tmp=0;
for(int i=1,j=0;i<=n*2;i++){
if(sa.sa[i]>=n){
tmp++;
if(j<q and ks[j]==tmp){
ans[j]=n*(m-2)+1+sa.sa[i];
j++;
}
}else{
tmp+=m-1;
while(j<q and ks[j]<=tmp){
ans[j]=1+sa.sa[i]-(ks[j]-tmp)*n;
j++;
}
}
}
for(int i=0;i<q;i++){
if(i) cout<<" ";
cout<<ans[i];
}
cout<<endl;
return 0;
}
beet