結果

問題 No.2102 [Cherry Alpha *] Conditional Reflection
ユーザー tnakao0123
提出日時 2022-10-15 13:41:07
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,767 bytes
コンパイル時間 499 ms
コンパイル使用メモリ 56,832 KB
実行使用メモリ 20,352 KB
最終ジャッジ日時 2024-06-26 19:45:29
合計ジャッジ時間 17,542 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32 WA * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2102.cc:  No.2102 [Cherry Alpha *] Conditional Reflection - yukicoder
 */

#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_N = 200000;
const int MAX_L = 1000000;
const int P = 4079;
const int MOD = 1000000033;

/* typedef */

/* typedef */

template<const int MOD>
struct MI {
  int v;
  MI(): v() {}
  MI(int _v): v(_v % MOD) {}
  MI(long long _v): v(_v % MOD) {}

  MI operator+(const MI m) const { return MI(v + m.v); }
  MI operator-(const MI m) const { return MI(v + MOD - m.v); }
  MI operator*(const MI m) const { return MI((long long)v * m.v); }

  MI &operator+=(const MI m) { return (*this = *this + m); }
  MI &operator-=(const MI m) { return (*this = *this - m); }
  MI &operator*=(const MI m) { return (*this = *this * m); }

  bool operator==(const MI m) const { return v == m.v; }
  bool operator!=(const MI m) const { return v != m.v; }
  bool operator<(const MI m) const { return v < m.v; }

  MI pow(int n) const {  // a^n % MOD
    MI pm = 1, a = *this;
    while (n > 0) {
      if (n & 1) pm *= a;
      a *= a;
      n >>= 1;
    }
    return pm;
  }

  MI inv() const { return pow(MOD - 2); }
  MI operator/(const MI m) const { return *this * m.inv(); }
  MI &operator/=(const MI m) { return (*this = *this / m); }
};

typedef MI<MOD> mi;

typedef pair<int,mi> pimi;
typedef set<pimi> spimi;

/* global variables */

mi pes[MAX_L + 1], invpes[MAX_L + 1];
char s[MAX_L + 4];

/* subroutines */

inline void prep_rhash(int n) {
  pes[0] = invpes[0] = 1;
  pes[1] = P;
  invpes[1] = pes[1].inv();
  for (int i = 2; i <= n; i++) {
    pes[i] = pes[i - 1] * P;
    invpes[i] = invpes[i - 1] * invpes[1];
  }
}

inline int s2rh(mi rh[], const char s[]) {
  int k = 0;
  for (; s[k]; k++)
    rh[k + 1] = rh[k] + pes[k] * s[k];
  return k;
}

inline mi s2h(const char s[]) {
  mi h = 0;
  for (int k = 0; s[k]; k++) h += pes[k] * s[k];
  return h;
}

inline mi rhash(mi rh[], int i, int j) {
  return (rh[j] - rh[i]) * invpes[i];
}

inline mi replh(mi h, char s[], int i, int j) {
  h -= pes[i] * s[i] + pes[j] * s[j];
  h += pes[i] * s[j] + pes[j] * s[i];
  return h;
}

/* main */

int main() {
  prep_rhash(MAX_L);

  int n;
  scanf("%d", &n);

  spimi gs;
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    int l = strlen(s);
    mi hi = s2h(s);
    //printf("%d: %s = %d,%d\n", i, s, l, hi.v);

    if (gs.find(pimi(l, hi)) != gs.end()) { puts("Yes"); continue; }

    bool ok = false;
    for (int j = 0; ! ok && j + 1 < l; j++) {
      mi hj = replh(hi, s, j, j + 1);
      ok = (gs.find(pimi(l, hj)) != gs.end());
    }

    gs.insert(pimi(l, hi));

    if (ok) puts("Yes");
    else puts("No");
  }
  return 0;
}
0