結果

問題 No.1018 suffixsuffixsuffix
ユーザー LayCurseLayCurse
提出日時 2020-04-05 18:37:53
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 8,379 bytes
コンパイル時間 2,835 ms
コンパイル使用メモリ 220,956 KB
実行使用メモリ 12,552 KB
最終ジャッジ日時 2023-09-16 06:47:53
合計ジャッジ時間 6,429 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
10,312 KB
testcase_01 AC 3 ms
10,404 KB
testcase_02 AC 4 ms
10,272 KB
testcase_03 AC 3 ms
10,308 KB
testcase_04 AC 3 ms
10,360 KB
testcase_05 AC 4 ms
10,464 KB
testcase_06 AC 4 ms
10,220 KB
testcase_07 AC 4 ms
10,352 KB
testcase_08 AC 3 ms
10,432 KB
testcase_09 AC 21 ms
11,824 KB
testcase_10 AC 21 ms
12,072 KB
testcase_11 AC 23 ms
12,200 KB
testcase_12 AC 21 ms
11,908 KB
testcase_13 AC 21 ms
12,052 KB
testcase_14 AC 22 ms
11,864 KB
testcase_15 AC 23 ms
11,892 KB
testcase_16 AC 24 ms
11,944 KB
testcase_17 AC 22 ms
12,040 KB
testcase_18 AC 22 ms
12,160 KB
testcase_19 AC 8 ms
11,064 KB
testcase_20 AC 8 ms
10,908 KB
testcase_21 AC 9 ms
11,248 KB
testcase_22 AC 8 ms
11,212 KB
testcase_23 AC 9 ms
11,228 KB
testcase_24 AC 26 ms
12,372 KB
testcase_25 AC 28 ms
12,552 KB
testcase_26 AC 26 ms
12,428 KB
testcase_27 AC 26 ms
12,436 KB
testcase_28 AC 26 ms
12,288 KB
testcase_29 AC 13 ms
12,388 KB
testcase_30 AC 13 ms
12,340 KB
testcase_31 AC 13 ms
12,272 KB
testcase_32 AC 12 ms
12,376 KB
testcase_33 AC 12 ms
12,368 KB
testcase_34 AC 4 ms
10,304 KB
testcase_35 AC 8 ms
10,952 KB
testcase_36 AC 13 ms
12,424 KB
testcase_37 AC 27 ms
12,480 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--;
      }
    }
  }
}
template<class T> int KMP(T A[], int As, T B[], int Bs, int res[] = NULL, int *fail = (int*)wmem){
  int i;
  int k;
  int cnt = 0;
  k = fail[0] = -1;
  for(i=(0);i<(Bs);i++){
    while(k>=0 && B[k]!=B[i]){
      k = fail[k];
    }
    fail[i+1] = ++k;
  }
  if(res != NULL){
    for(i=(0);i<(As);i++){
      res[i] = 0;
    }
  }
  k = 0;
  for(i=(0);i<(As);i++){
    while(k >= 0 && B[k] != A[i]){
      k = fail[k];
    }
    k++;
    if(k == Bs){
      cnt++;
      if(res != NULL){
        res[i-Bs+1] = 1;
      }
      k = fail[k];
    }
  }
  return cnt;
}
template<class T, class S> int KMP(T A[], int As, T B[], int Bs, S res[], int *fail = (int*)wmem){
  int i;
  int k;
  int cnt = 0;
  k = fail[0] = -1;
  for(i=(0);i<(Bs);i++){
    while(k>=0 && B[k]!=B[i]){
      k = fail[k];
    }
    fail[i+1] = ++k;
  }
  if(res != NULL){
    for(i=(0);i<(As);i++){
      res[i] = 0;
    }
  }
  k = 0;
  for(i=(0);i<(As);i++){
    while(k >= 0 && B[k] != A[i]){
      k = fail[k];
    }
    k++;
    if(k == Bs){
      cnt++;
      if(res != NULL){
        res[i-Bs+1] = 1;
      }
      k = fail[k];
    }
  }
  return cnt;
}
int N;
int Q;
long long M;
long long K[100000];
char S[200000+2];
int sa[200000+2];
long long res[100000];
int tmp[100000+2];
int fail[100000+2];
int main(){
  int i;
  wmem = memarr;
  int k = 0;
  int p;
  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]);
    }
  }
  KMP(S,N,S,N,tmp,fail);
  p = N-fail[N];
  if(N%p){
    p = N;
  }
  M *= N / p;
  N = p;
  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, Q; ll M, K[1d5];
// char S[2d5+2];
// int sa[2d5+2];
// ll res[1d5];
// 
// int tmp[1d5+2], fail[1d5+2];
// 
// {
//   int k = 0, p;
//   ll now = 0, len, st;
//   rd(N,M,Q,S,K(Q));
// 
//   KMP(S,N,S,N,tmp,fail);
//   p = N-fail[N];
//   if(N%p) p = N;
//   M *= N / p;
//   N = p;
// 
//   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