結果
| 問題 |
No.2102 [Cherry Alpha *] Conditional Reflection
|
| コンテスト | |
| ユーザー |
tnakao0123
|
| 提出日時 | 2022-10-15 14:01:01 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 587 ms / 3,000 ms |
| コード長 | 2,983 bytes |
| コンパイル時間 | 746 ms |
| コンパイル使用メモリ | 67,444 KB |
| 実行使用メモリ | 29,440 KB |
| 最終ジャッジ日時 | 2024-12-23 01:50:58 |
| 合計ジャッジ時間 | 25,293 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 70 |
ソースコード
/* -*- 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;
}
tnakao0123