#include <cstdio> #include <cstring> #include <cmath> #include <cassert> #include <iostream> #include <algorithm> #include <iterator> #include <string> #include <vector> #include <queue> #include <bitset> #include <utility> #include <stack> using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef vector<int> VI; #define MP make_pair #define FOR(v,p,k) for(int v=(p);v<=(k);++v) #define FORD(v,p,k) for(int v=(p);v>=(k);--v) #define REP(i,n) for(int i=0;i<(n);++i) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define SIZE(x) (int)x.size() #define ALL(c) c.begin(),c.end() #define ODD(x) ((x)%2) #define EVEN(x) (!(ODD(x))) VI big_primes = {1000000087, 1000000093}; VI bases = {47}; class RollingHash2Way { int modulo; int base; int curr_base_powered; int hash_value; int reversed_hash_value; public: RollingHash2Way(int modulo, int base) : modulo(modulo), base(base), curr_base_powered(1), hash_value(0), reversed_hash_value(0) {} void update(int a) { hash_value = ((LL) curr_base_powered * a + hash_value) % modulo; reversed_hash_value = ((LL) reversed_hash_value * base + a) % modulo; curr_base_powered = ((LL) base * curr_base_powered) % modulo; } bool is_palindrome() { return hash_value == reversed_hash_value; } void debug() { cout << "hash_value:" << hash_value << " reversed_hash_value: " << reversed_hash_value << "\n";} }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); vector<RollingHash2Way> hashes; hashes.reserve(big_primes.size() * bases.size()); for(int modulo: big_primes) for(int base: bases) hashes.PB(RollingHash2Way(modulo, base)); int n; cin >> n; char a; while(cin >> a) { for(auto& rh: hashes) { rh.update(a -'a' +1); //rh.debug(); } } bool res = all_of(hashes.begin(), hashes.end(), [](RollingHash2Way &h){return h.is_palindrome(); }); //for(auto& rh: hashes) rh.debug(); string res_str = res ? "TAK" : "NIE"; cout << res_str << "\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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | #include <cstdio> #include <cstring> #include <cmath> #include <cassert> #include <iostream> #include <algorithm> #include <iterator> #include <string> #include <vector> #include <queue> #include <bitset> #include <utility> #include <stack> using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef vector<int> VI; #define MP make_pair #define FOR(v,p,k) for(int v=(p);v<=(k);++v) #define FORD(v,p,k) for(int v=(p);v>=(k);--v) #define REP(i,n) for(int i=0;i<(n);++i) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define SIZE(x) (int)x.size() #define ALL(c) c.begin(),c.end() #define ODD(x) ((x)%2) #define EVEN(x) (!(ODD(x))) VI big_primes = {1000000087, 1000000093}; VI bases = {47}; class RollingHash2Way { int modulo; int base; int curr_base_powered; int hash_value; int reversed_hash_value; public: RollingHash2Way(int modulo, int base) : modulo(modulo), base(base), curr_base_powered(1), hash_value(0), reversed_hash_value(0) {} void update(int a) { hash_value = ((LL) curr_base_powered * a + hash_value) % modulo; reversed_hash_value = ((LL) reversed_hash_value * base + a) % modulo; curr_base_powered = ((LL) base * curr_base_powered) % modulo; } bool is_palindrome() { return hash_value == reversed_hash_value; } void debug() { cout << "hash_value:" << hash_value << " reversed_hash_value: " << reversed_hash_value << "\n";} }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); vector<RollingHash2Way> hashes; hashes.reserve(big_primes.size() * bases.size()); for(int modulo: big_primes) for(int base: bases) hashes.PB(RollingHash2Way(modulo, base)); int n; cin >> n; char a; while(cin >> a) { for(auto& rh: hashes) { rh.update(a -'a' +1); //rh.debug(); } } bool res = all_of(hashes.begin(), hashes.end(), [](RollingHash2Way &h){return h.is_palindrome(); }); //for(auto& rh: hashes) rh.debug(); string res_str = res ? "TAK" : "NIE"; cout << res_str << "\n"; return 0; } |