結果

問題 No.1018 suffixsuffixsuffix
ユーザー LayCurseLayCurse
提出日時 2020-04-05 18:27:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,958 bytes
コンパイル時間 2,828 ms
コンパイル使用メモリ 222,564 KB
実行使用メモリ 11,724 KB
最終ジャッジ日時 2024-07-03 08:20:19
合計ジャッジ時間 5,515 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,388 KB
testcase_01 AC 2 ms
7,884 KB
testcase_02 AC 3 ms
7,632 KB
testcase_03 AC 3 ms
7,880 KB
testcase_04 AC 3 ms
8,384 KB
testcase_05 AC 3 ms
8,260 KB
testcase_06 AC 3 ms
7,632 KB
testcase_07 AC 3 ms
8,140 KB
testcase_08 AC 4 ms
8,268 KB
testcase_09 AC 19 ms
11,200 KB
testcase_10 AC 18 ms
8,772 KB
testcase_11 AC 20 ms
9,420 KB
testcase_12 AC 18 ms
10,304 KB
testcase_13 AC 18 ms
11,344 KB
testcase_14 AC 18 ms
8,672 KB
testcase_15 AC 20 ms
9,160 KB
testcase_16 AC 19 ms
9,160 KB
testcase_17 AC 19 ms
11,296 KB
testcase_18 AC 19 ms
10,320 KB
testcase_19 AC 7 ms
8,516 KB
testcase_20 AC 9 ms
8,516 KB
testcase_21 AC 8 ms
8,772 KB
testcase_22 AC 8 ms
8,648 KB
testcase_23 AC 9 ms
8,776 KB
testcase_24 AC 24 ms
9,288 KB
testcase_25 AC 26 ms
9,292 KB
testcase_26 AC 23 ms
9,416 KB
testcase_27 AC 24 ms
11,724 KB
testcase_28 AC 23 ms
11,600 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 3 ms
8,392 KB
testcase_35 AC 8 ms
8,556 KB
testcase_36 WA -
testcase_37 AC 23 ms
10,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void rd(long long &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void rd(char &c){
  int i;
  for(;;){
    i = my_getchar_unlocked();
    if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
      break;
    }
  }
  c = i;
}
inline int rd(char c[]){
  int i;
  int sz = 0;
  for(;;){
    i = my_getchar_unlocked();
    if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
      break;
    }
  }
  c[sz++] = i;
  for(;;){
    i = my_getchar_unlocked();
    if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF){
      break;
    }
    c[sz++] = i;
  }
  c[sz]='\0';
  return sz;
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
template<class T> void SuffixArray(T *s, int N, int K, int *SA, int *LCP = NULL, void *mem = wmem){
  int i;
  int j;
  int d;
  int m;
  int *s1;
  int name;
  int prev;
  int pos;
  char *t;
  char *lms;
  int *cnt;
  int *cnt1;
  int *cnt2;
  walloc1d(&t, N+1, &mem);
  walloc1d(&lms, N+1, &mem);
  walloc1d(&cnt, K+1, &mem);
  walloc1d(&cnt1, K+1, &mem);
  walloc1d(&cnt2, K+1, &mem);
  N++;
  s[N-1] = 0;
  t[N-1] = 1;
  t[N-2] = 0;
  for(i=N-3;i>=0;i--){
    if(s[i] < s[i+1] || (s[i]==s[i+1] && t[i+1])){
      t[i] = 1;
    }
    else{
      t[i] = 0;
    }
  }
  lms[0] = 0;
  int aTqQ6rt8 = N;
  for(i=(1);i<(aTqQ6rt8);i++){
    if(t[i] && !t[i-1]){
      lms[i] = 1;
    }
    else{
      lms[i] = 0;
    }
  }
  for(i=(0);i<(K+1);i++){
    cnt1[i] = 0;
  }
  for(i=(0);i<(N);i++){
    cnt1[s[i]]++;
  }
  j = 0;
  for(i=(0);i<(K+1);i++){
    j += cnt1[i];
    cnt2[i] = j - cnt1[i];
    cnt1[i] = j;
  }
  for(i=(0);i<(K+1);i++){
    cnt[i] = cnt1[i];
  }
  for(i=0; i<N; i++){
    SA[i] = -1;
  }
  for(i=1; i<N; i++){
    if(lms[i]){
      SA[--cnt[s[i]]]=i;
    }
  }
  for(i=(0);i<(K+1);i++){
    cnt[i] = cnt2[i];
  }
  for(i=(0);i<(N);i++){
    j = SA[i]-1;
    if(j>=0 && !t[j]){
      SA[cnt[s[j]]++] = j;
    }
  }
  for(i=(0);i<(K+1);i++){
    cnt[i] = cnt1[i];
  }
  for(i=N-1;i>=0;i--){
    j = SA[i] - 1;
    if(j>=0 && t[j]){
      SA[--cnt[s[j]]] = j;
    }
  }
  m = 0;
  for(i=(0);i<(N);i++){
    if(lms[SA[i]]){
      SA[m++] = SA[i];
    }
  }
  int jO2HaRTX = N;
  for(i=(m);i<(jO2HaRTX);i++){
    SA[i] = -1;
  }
  name=0;
  prev=-1;
  for(i=(0);i<(m);i++){
    pos = SA[i];
    for(d=(0);d<(N);d++){
      if(prev==-1 || s[pos+d]!=s[prev+d] || t[pos+d]!=t[prev+d]){
        name++;
        prev=pos;
        break;
      }
      else if(d>0 && (lms[pos+d] || lms[prev+d])){
        break;
      }
    }
    pos /= 2;
    SA[m+pos]=name-1;
  }
  for(i=N-1, j=N-1; i>=m; i--){
    if(SA[i]>=0){
      SA[j--]=SA[i];
    }
  }
  s1 = SA+N-m;
  if(name<m){
    SuffixArray(s1, m-1, name-1, SA, NULL, mem);
  }
  else{
    for(i=0; i<m; i++){
      SA[s1[i]] = i;
    }
  }
  for(i=(0);i<(K+1);i++){
    cnt[i] = cnt1[i];
  }
  for(i=1, j=0; i<N; i++){
    if(lms[i]){
      s1[j++]=i;
    }
  }
  for(i=0; i<m; i++){
    SA[i]=s1[SA[i]];
  }
  for(i=m; i<N; i++){
    SA[i]=-1;
  }
  for(i=m-1; i>=0; i--){
    j=SA[i];
    SA[i]=-1;
    SA[--cnt[s[j]]]=j;
  }
  for(i=(0);i<(N);i++){
    j = SA[i]-1;
    if(j>=0 && !t[j]){
      SA[cnt2[s[j]]++] = j;
    }
  }
  for(i=N-1;i>=0;i--){
    j = SA[i] - 1;
    if(j>=0 && t[j]){
      SA[--cnt1[s[j]]] = j;
    }
  }
  if(LCP != NULL){
    cnt = (int*)t;
    d = 0;
    for(i=(0);i<(N);i++){
      cnt[SA[i]] = i;
    }
    for(i=(0);i<(N);i++){
      if(cnt[i]){
        for(j=SA[cnt[i]-1]; j+d<N-1&&i+d<N-1&&s[j+d]==s[i+d];d++){
          ;
        }
        LCP[cnt[i]]=d;
      }
      else{
        LCP[cnt[i]] = -1;
      }
      if(d>0){
        d--;
      }
    }
  }
}
int N;
int M;
int Q;
long long K[100000];
char S[200000+2];
int sa[200000+2];
long long res[100000];
int main(){
  int i;
  wmem = memarr;
  int k = 0;
  long long now = 0;
  long long len;
  long long st;
  rd(N);
  rd(M);
  rd(Q);
  rd(S);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(Q);Lj4PdHRW++){
      rd(K[Lj4PdHRW]);
    }
  }
  for(i=(0);i<(N);i++){
    S[i+N] = S[i];
  }
  SuffixArray(S, 2*N, 128, sa);
  for(i=(0);i<(2*N+1);i++){
    len = M - 1;
    if(sa[i] >= N){
      len = 1;
    }
    st = sa[i] + (long long)(M-2) * N + 1;
    while(now + len > K[k]){
      res[k] = st - (K[k] - now) * N;
      k++;
    }
    now += len;
  }
  {
    int t_ynMSdg;
    if(Q==0){
      wt_L('\n');
    }
    else{
      for(t_ynMSdg=(0);t_ynMSdg<(Q-1);t_ynMSdg++){
        wt_L(res[t_ynMSdg]);
        wt_L(' ');
      }
      wt_L(res[t_ynMSdg]);
      wt_L('\n');
    }
  }
  return 0;
}
// cLay varsion 20200404-1

// --- original code ---
// int N, M, Q; ll K[1d5];
// char S[2d5+2];
// int sa[2d5+2];
// ll res[1d5];
// {
//   int k = 0;
//   ll now = 0, len, st;
//   rd(N,M,Q,S,K(Q));
// 
//   rep(i,N) S[i+N] = S[i];
//   SuffixArray(S, 2N, 128, sa);
//   rep(i,2N+1){
//     len = M - 1;
//     if(sa[i] >= N) len = 1;
//     st = sa[i] + (ll)(M-2) * N + 1;
//     while(now + len > K[k]){
//       res[k] = st - (K[k] - now) * N;
//       k++;
//     }
//     now += len;
//   }
//   wt(res(Q));
// }
0