結果
| 問題 |
No.1018 suffixsuffixsuffix
|
| ユーザー |
chocorusk
|
| 提出日時 | 2020-04-04 18:12:20 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 360 ms / 2,000 ms |
| コード長 | 2,343 bytes |
| コンパイル時間 | 1,294 ms |
| コンパイル使用メモリ | 121,140 KB |
| 実行使用メモリ | 8,064 KB |
| 最終ジャッジ日時 | 2024-07-03 07:36:24 |
| 合計ジャッジ時間 | 11,075 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 34 |
ソースコード
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
struct SuffixArray{
int n, k;
vector<int> rk, tmp, sa;
string s;
SuffixArray(string s):n(s.size()), s(s), rk(s.size()+1), tmp(s.size()+1), sa(s.size()+1){
auto compare_sa=[&](int i, int j){
if(rk[i]!=rk[j]) return rk[i]<rk[j];
else{
int ri, rj;
if(i+k<=n) ri=rk[i+k];
else ri=-1;
if(j+k<=n) rj=rk[j+k];
else rj=-1;
return ri<rj;
}
};
for(int i=0; i<=n; i++){
sa[i]=i;
if(i<n){
rk[i]=s[i]-'A';
}else{
rk[i]=-1;
}
}
for(k=1; k<=n; k<<=1){
sort(sa.begin(), sa.end(), compare_sa);
tmp[sa[0]]=0;
for(int i=1; i<=n; i++){
tmp[sa[i]]=tmp[sa[i-1]];
if(compare_sa(sa[i-1], sa[i])) tmp[sa[i]]++;
}
rk=tmp;
}
}
int operator[](const int &k) const{
return sa[k];
}
};
vector< int > z_algorithm(const string &s) {
vector< int > prefix(s.size());
for(int i = 1, j = 0; i < s.size(); i++) {
if(i + prefix[i - j] < j + prefix[j]) {
prefix[i] = prefix[i - j];
} else {
int k = max(0, j + prefix[j] - i);
while(i + k < s.size() && s[k] == s[i + k]) ++k;
prefix[i] = k;
j = i;
}
}
prefix[0] = (int) s.size();
return prefix;
}
int main()
{
int n, q;
ll m;
cin>>n>>m>>q;
string s;
cin>>s;
if(m==1){
SuffixArray sa(s);
while(q--){
ll k; cin>>k;
cout<<sa[k]+1<<" ";
}
cout<<endl;
return 0;
}
auto z=z_algorithm(s);
int d;
for(d=1; d<n; d++){
if(n%d!=0) continue;
if(z[d]==n-d) break;
}
s=s.substr(0, d);
m*=n/d;
n=d;
ll c=0;
SuffixArray sa(s+s);
ll x[200020];
x[0]=0;
for(int i=1; i<=2*n; i++){
x[i]=x[i-1]+((sa[i]<n)?(m-1):1);
}
while(q--){
ll k; cin>>k;
int j=lower_bound(x, x+2*n+1, k)-x;
ll ans;
if(sa[j]>=n){
ans=sa[j]-n+n*(m-1)+1;
}else{
ans=n*(x[j]-k)+1+sa[j];
}
cout<<ans<<" ";
}
cout<<endl;
return 0;
}
chocorusk