結果

問題 No.1018 suffixsuffixsuffix
ユーザー beetbeet
提出日時 2020-04-02 11:40:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,199 bytes
コンパイル時間 2,313 ms
コンパイル使用メモリ 210,588 KB
実行使用メモリ 10,484 KB
最終ジャッジ日時 2023-09-15 23:20:58
合計ジャッジ時間 7,307 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,756 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 54 ms
5,216 KB
testcase_10 AC 50 ms
5,156 KB
testcase_11 AC 49 ms
5,264 KB
testcase_12 AC 38 ms
5,220 KB
testcase_13 AC 38 ms
5,196 KB
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0