#include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto &i : a) #define SZ(i) ((int)(i).size()) #define X first #define Y second #define PR std::pair #define PII std::pair<int, int> #define MP std::make_pair #define ll long long #define VI std::vector<int> struct Hash{ unsigned long long base, mod; unsigned long long pot; unsigned long long fw, bk; Hash(unsigned long long b, unsigned long long m) : base(b), mod(m), pot(1), fw(0), bk(0) {} void add(char ch){ fw = (fw+1ll*(ch-'a'+1)*pot)%mod; bk = (bk*base+1ll*(ch-'a'+1))%mod; pot = (pot*base)%mod; } bool check(){ return fw == bk; } }; int n; int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cin >> n; Hash h1(31ll, 98765432101456789ll); // Hash h2(43, 1020026771); // Hash h3(37, 94291997); char ch; while(std::cin >> ch){ h1.add(ch); // h2.add(ch); // h3.add(ch); } if(h1.check()) std::cout << "TAK\n"; else std::cout << "NIE\n"; return 0; }
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 | #include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto &i : a) #define SZ(i) ((int)(i).size()) #define X first #define Y second #define PR std::pair #define PII std::pair<int, int> #define MP std::make_pair #define ll long long #define VI std::vector<int> struct Hash{ unsigned long long base, mod; unsigned long long pot; unsigned long long fw, bk; Hash(unsigned long long b, unsigned long long m) : base(b), mod(m), pot(1), fw(0), bk(0) {} void add(char ch){ fw = (fw+1ll*(ch-'a'+1)*pot)%mod; bk = (bk*base+1ll*(ch-'a'+1))%mod; pot = (pot*base)%mod; } bool check(){ return fw == bk; } }; int n; int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cin >> n; Hash h1(31ll, 98765432101456789ll); // Hash h2(43, 1020026771); // Hash h3(37, 94291997); char ch; while(std::cin >> ch){ h1.add(ch); // h2.add(ch); // h3.add(ch); } if(h1.check()) std::cout << "TAK\n"; else std::cout << "NIE\n"; return 0; } |