結果

問題 No.2102 [Cherry Alpha *] Conditional Reflection
ユーザー tnakao0123tnakao0123
提出日時 2022-10-15 14:01:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 547 ms / 3,000 ms
コード長 2,983 bytes
コンパイル時間 2,145 ms
コンパイル使用メモリ 66,564 KB
実行使用メモリ 30,520 KB
最終ジャッジ日時 2023-08-24 17:31:33
合計ジャッジ時間 26,689 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
18,156 KB
testcase_01 AC 13 ms
18,132 KB
testcase_02 AC 197 ms
23,704 KB
testcase_03 AC 288 ms
25,432 KB
testcase_04 AC 204 ms
23,560 KB
testcase_05 AC 108 ms
21,072 KB
testcase_06 AC 216 ms
23,848 KB
testcase_07 AC 225 ms
23,044 KB
testcase_08 AC 35 ms
18,476 KB
testcase_09 AC 259 ms
24,548 KB
testcase_10 AC 72 ms
20,224 KB
testcase_11 AC 82 ms
21,072 KB
testcase_12 AC 163 ms
23,164 KB
testcase_13 AC 348 ms
26,164 KB
testcase_14 AC 155 ms
22,396 KB
testcase_15 AC 238 ms
24,336 KB
testcase_16 AC 420 ms
27,564 KB
testcase_17 AC 147 ms
21,368 KB
testcase_18 AC 289 ms
23,532 KB
testcase_19 AC 115 ms
21,544 KB
testcase_20 AC 378 ms
26,712 KB
testcase_21 AC 56 ms
18,764 KB
testcase_22 AC 465 ms
28,488 KB
testcase_23 AC 469 ms
28,496 KB
testcase_24 AC 466 ms
28,540 KB
testcase_25 AC 470 ms
28,500 KB
testcase_26 AC 468 ms
28,508 KB
testcase_27 AC 470 ms
28,376 KB
testcase_28 AC 469 ms
28,524 KB
testcase_29 AC 474 ms
28,492 KB
testcase_30 AC 482 ms
28,452 KB
testcase_31 AC 470 ms
28,496 KB
testcase_32 AC 481 ms
28,468 KB
testcase_33 AC 484 ms
28,436 KB
testcase_34 AC 472 ms
28,496 KB
testcase_35 AC 473 ms
28,492 KB
testcase_36 AC 468 ms
28,492 KB
testcase_37 AC 469 ms
28,508 KB
testcase_38 AC 483 ms
28,436 KB
testcase_39 AC 466 ms
28,520 KB
testcase_40 AC 446 ms
28,484 KB
testcase_41 AC 458 ms
28,444 KB
testcase_42 AC 390 ms
25,080 KB
testcase_43 AC 403 ms
24,952 KB
testcase_44 AC 394 ms
25,068 KB
testcase_45 AC 391 ms
25,128 KB
testcase_46 AC 406 ms
25,080 KB
testcase_47 AC 310 ms
22,316 KB
testcase_48 AC 302 ms
22,288 KB
testcase_49 AC 297 ms
22,252 KB
testcase_50 AC 289 ms
22,300 KB
testcase_51 AC 297 ms
22,308 KB
testcase_52 AC 37 ms
19,660 KB
testcase_53 AC 37 ms
19,660 KB
testcase_54 AC 37 ms
19,756 KB
testcase_55 AC 112 ms
19,148 KB
testcase_56 AC 113 ms
19,200 KB
testcase_57 AC 112 ms
19,188 KB
testcase_58 AC 38 ms
19,176 KB
testcase_59 AC 547 ms
30,520 KB
testcase_60 AC 27 ms
18,140 KB
testcase_61 AC 113 ms
19,200 KB
testcase_62 AC 18 ms
18,456 KB
testcase_63 AC 35 ms
20,480 KB
testcase_64 AC 35 ms
20,496 KB
testcase_65 AC 38 ms
20,620 KB
testcase_66 AC 106 ms
19,196 KB
testcase_67 AC 41 ms
18,140 KB
testcase_68 AC 51 ms
19,184 KB
testcase_69 AC 42 ms
18,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#include<cstdio>
#include<cstring>
#include<string>
#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;

struct Stat {
  int l, i;
  mi h;
  Stat() {}
  Stat(int _l, int _i, mi _h): l(_l), i(_i), h(_h) {}
  bool operator<(const Stat &s) const {
    return l < s.l || (l == s.l && h < s.h);
  }
};

typedef set<Stat> sst;

/* global variables */

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

/* 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 mi s2h(const string &s) {
  mi h = 0;
  for (int k = 0; k < s.size(); k++) h += pes[k] * s[k];
  return h;
}

inline mi replh(mi h, const string &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;
}

bool check(sst &gs, string &s, mi h) {
  sst::iterator sit = gs.find(Stat((int)s.size(), -1, h));
  return (sit != gs.end() && ss[sit->i] == s);
}

/* main */

int main() {
  prep_rhash(MAX_L);

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

  sst gs;
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    string si(s);
    ss[i] = si;
    int l = si.size();
    mi hi = s2h(si);
    //printf("%d: %s = %d,%d\n", i, s, l, hi.v);

    if (check(gs, si, hi)) { puts("Yes"); continue; }

    bool ok = false;
    for (int j = 0; ! ok && j + 1 < l; j++) {
      mi hj = replh(hi, si, j, j + 1);
      swap(si[j], si[j + 1]);
      ok = check(gs, si, hj);
      swap(si[j], si[j + 1]);
    }

    gs.insert(Stat(l, i, hi));

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