結果

問題 No.1018 suffixsuffixsuffix
ユーザー LayCurseLayCurse
提出日時 2020-04-05 18:33:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 8,468 bytes
コンパイル時間 2,883 ms
コンパイル使用メモリ 221,908 KB
実行使用メモリ 12,512 KB
最終ジャッジ日時 2023-09-16 06:47:24
合計ジャッジ時間 6,811 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
10,480 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 3 ms
10,200 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 4 ms
10,276 KB
testcase_07 AC 4 ms
10,292 KB
testcase_08 AC 3 ms
10,248 KB
testcase_09 WA -
testcase_10 AC 23 ms
12,000 KB
testcase_11 AC 23 ms
11,992 KB
testcase_12 AC 22 ms
11,848 KB
testcase_13 AC 22 ms
12,112 KB
testcase_14 WA -
testcase_15 AC 24 ms
12,136 KB
testcase_16 WA -
testcase_17 AC 23 ms
12,024 KB
testcase_18 AC 23 ms
12,052 KB
testcase_19 WA -
testcase_20 AC 9 ms
10,992 KB
testcase_21 WA -
testcase_22 AC 9 ms
11,012 KB
testcase_23 AC 9 ms
10,996 KB
testcase_24 WA -
testcase_25 AC 29 ms
12,360 KB
testcase_26 AC 25 ms
12,404 KB
testcase_27 WA -
testcase_28 AC 26 ms
12,360 KB
testcase_29 AC 13 ms
12,424 KB
testcase_30 AC 13 ms
12,248 KB
testcase_31 AC 13 ms
12,308 KB
testcase_32 AC 13 ms
12,312 KB
testcase_33 AC 13 ms
12,512 KB
testcase_34 AC 4 ms
10,408 KB
testcase_35 AC 9 ms
11,220 KB
testcase_36 AC 13 ms
12,416 KB
testcase_37 AC 28 ms
12,288 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, class U> inline T GCD_L(T a, U b){
  T r;
  while(b){
    r=a;
    a=b;
    b=r%a;
  }
  return a;
}
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 ytthggxT = N;
  for(i=(1);i<(ytthggxT);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 NLJcSLph = N;
  for(i=(m);i<(NLJcSLph);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 =GCD_L(N, N-fail[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 KrdatlYV;
    if(Q==0){
      wt_L('\n');
    }
    else{
      for(KrdatlYV=(0);KrdatlYV<(Q-1);KrdatlYV++){
        wt_L(res[KrdatlYV]);
        wt_L(' ');
      }
      wt_L(res[KrdatlYV]);
      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 = gcd(N,N-fail[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