#include<bits/stdc++.h> using namespace std; using i64 = long long; using i32 = int; template<typename T> T load() { T x; cin >> x; return x; } template<typename T> vector<T> loadN(int s) { vector<T> vt(s); for(auto& el : vt) el = load<T>(); return vt; } template<typename T> ostream& operator<<(ostream& os, const vector<T>& vt) { for(auto& el : vt) os << el << ' '; return os; } struct Hash { Hash(i32 ival = 0):val(ival % MOD) {} Hash(i64 lval):val(lval % LMOD) {} static void tune(i32 val) { LMOD = MOD = val; } Hash operator+(const Hash& el) const { return { val + el.val}; } Hash operator*(const Hash& el) const { return { static_cast<i64>(val) * el.val}; } Hash& operator*=(const Hash& el) { *this = *this * el; return *this; } Hash& operator+=(const Hash& el) { *this = *this + el; return *this; } friend ostream& operator<<(ostream& os, const Hash& el ) { return os << el.val; } i32 val; static i64 LMOD; static i32 MOD; }; i32 Hash::MOD = 1; i64 Hash::LMOD = 1; const vector<i32> BASES = {61, 67, 71, 73}; const vector<i32> MODULOS = { 1000000007, 1000000009, 1000000021, 1000000033, }; i32 main() { ios::sync_with_stdio(false); cin.tie(nullptr); Hash::tune(MODULOS[0]); auto size = load<i32>(); char c; const auto diff = (i32)BASES.size(); auto rev = vector<Hash>(diff); auto simple = vector<Hash>(diff); auto pows = vector<Hash>(diff, Hash{1}); while(cin >> c) { c -= 'a'; for(i32 curr = 0 ; curr < diff ; ++curr) { Hash::tune(MODULOS[curr]); auto base = BASES[curr]; rev[curr] = rev[curr] * base + c; simple[curr] = simple[curr] + pows[curr] * c; pows[curr] *= base; } } for(i32 i = 0 ; i < diff ; ++i) { if(simple[i].val != rev[i].val) { cout << "NIE\n"; return 0; } } cout << "TAK\n"; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | #include<bits/stdc++.h> using namespace std; using i64 = long long; using i32 = int; template<typename T> T load() { T x; cin >> x; return x; } template<typename T> vector<T> loadN(int s) { vector<T> vt(s); for(auto& el : vt) el = load<T>(); return vt; } template<typename T> ostream& operator<<(ostream& os, const vector<T>& vt) { for(auto& el : vt) os << el << ' '; return os; } struct Hash { Hash(i32 ival = 0):val(ival % MOD) {} Hash(i64 lval):val(lval % LMOD) {} static void tune(i32 val) { LMOD = MOD = val; } Hash operator+(const Hash& el) const { return { val + el.val}; } Hash operator*(const Hash& el) const { return { static_cast<i64>(val) * el.val}; } Hash& operator*=(const Hash& el) { *this = *this * el; return *this; } Hash& operator+=(const Hash& el) { *this = *this + el; return *this; } friend ostream& operator<<(ostream& os, const Hash& el ) { return os << el.val; } i32 val; static i64 LMOD; static i32 MOD; }; i32 Hash::MOD = 1; i64 Hash::LMOD = 1; const vector<i32> BASES = {61, 67, 71, 73}; const vector<i32> MODULOS = { 1000000007, 1000000009, 1000000021, 1000000033, }; i32 main() { ios::sync_with_stdio(false); cin.tie(nullptr); Hash::tune(MODULOS[0]); auto size = load<i32>(); char c; const auto diff = (i32)BASES.size(); auto rev = vector<Hash>(diff); auto simple = vector<Hash>(diff); auto pows = vector<Hash>(diff, Hash{1}); while(cin >> c) { c -= 'a'; for(i32 curr = 0 ; curr < diff ; ++curr) { Hash::tune(MODULOS[curr]); auto base = BASES[curr]; rev[curr] = rev[curr] * base + c; simple[curr] = simple[curr] + pows[curr] * c; pows[curr] *= base; } } for(i32 i = 0 ; i < diff ; ++i) { if(simple[i].val != rev[i].val) { cout << "NIE\n"; return 0; } } cout << "TAK\n"; } |