/* Hashowanie napisow na podstawie rozwiazania zadania Poszukiwania (autor Tomasz Czajka) z Potyczek Algorytmicznych 2015 */ #include <cassert> #include <iostream> #include <limits> #include <vector> #include <cstring> #include <cstdio> #include <ctime> using std::numeric_limits; using std::vector; typedef long long LL; typedef unsigned long long ULL; // Mersenne prime. constexpr int primeExponent = 61; constexpr ULL prime = (1ULL << primeExponent) - 1ULL; // Modulo prime. class Mod { public: // x must be < p explicit constexpr Mod(ULL x) : val{x} {} Mod operator+(Mod b) const { ULL res = val + b.val; if (res >= prime) res -= prime; return Mod{res}; } Mod operator-(Mod b) const { ULL res = val; if (res < b.val) res += prime; res -= b.val; return Mod{res}; } Mod operator*(unsigned b) const { unsigned a0 = unsigned(val); unsigned a1 = unsigned(val >> 32); ULL res0 = ULL(a0) * ULL(b); ULL res1 = ULL(a1) * ULL(b); ULL res = (res0 & prime) + (res0 >> primeExponent) + ((res1 << 32) & prime) + (res1 >> (primeExponent - 32)); return Mod{res % prime}; } Mod operator*(Mod b) const { unsigned a0 = unsigned(val); unsigned a1 = unsigned(val >> 32); unsigned b0 = unsigned(b.val); unsigned b1 = unsigned(b.val >> 32); ULL res0 = ULL(a0) * ULL(b0); ULL res1 = ULL(a0) * ULL(b1) + ULL(a1) * ULL(b0); ULL res2 = ULL(a1) * ULL(b1); ULL res = (res0 & prime) + (res0 >> primeExponent) + ((res1 << 32) & prime) + (res1 >> (primeExponent - 32)) + ((res2 << (64 - primeExponent)) & prime) + (res2 >> (2 * primeExponent - 64)); return Mod{res % prime}; } bool operator==(Mod b) const { return val == b.val; } void operator+=(Mod b) { *this = *this + b; } void operator-=(Mod b) { *this = *this - b; } void operator*=(unsigned b) { *this = *this * b; } void operator*=(Mod b) { *this = *this * b; } ULL Extract() const { return val; } private: ULL val; }; Mod Power(Mod a, ULL n) { Mod res{1}; ULL two_b = 1u; Mod a_two_b = a; while(two_b <= n) { if (n & two_b) res *= a_two_b; two_b <<= 1; a_two_b *= a_two_b; } return res; } template<ULL baseULL> class Hash { static_assert(baseULL < prime, "base < prime"); static constexpr Mod base = Mod{baseULL}; public: Hash() : val{0}, powBaseLen{0} {} Hash(Mod v) : val{v}, powBaseLen{0} {} void Append(unsigned c) { val = val * base + Mod{c}; } bool operator==(const Hash &b) const { return val == b.val; } Mod Extract() const { return val; } private: Mod val; Mod powBaseLen; }; template<ULL baseULL> constexpr Mod Hash<baseULL>::base; class StrongHash { public: StrongHash() : hash1{}, hash2{} {} StrongHash(Mod h1, Mod h2) : hash1{h1}, hash2{h2} {} void Append(unsigned c) { hash1.Append(c); hash2.Append(c); } bool operator==(const StrongHash &b) const { return hash1 == b.hash1 && hash2 == b.hash2; } Mod Extract1() const { return hash1.Extract(); } Mod Extract2() const { return hash2.Extract(); } private: Hash<1919222792405563639ULL> hash1; Hash<1940348388277362841ULL> hash2; }; //------------------------------------------------- template<ULL baseULL> class HashRev { static_assert(baseULL < prime, "base < prime"); static constexpr Mod base = Mod{baseULL}; public: Mod revPow{0}; HashRev() : val{0}, powBaseLen{1} { } HashRev(Mod v) : val{v}, powBaseLen{1} { } void Append(unsigned c) { Mod x = powBaseLen * Mod{c}; val = val + x; powBaseLen = powBaseLen * base; } Mod Extract() const { return val; } private: Mod val; Mod powBaseLen; }; template<ULL baseULL> constexpr Mod HashRev<baseULL>::base; class StrongHashRev { public: StrongHashRev() : hash1{}, hash2{} {} StrongHashRev(Mod h1, Mod h2) : hash1{h1}, hash2{h2} {} void Append(unsigned c) { hash1.Append(c); hash2.Append(c); } Mod Extract1() const { return hash1.Extract(); } Mod Extract2() const { return hash2.Extract(); } private: HashRev<1919222792405563639ULL> hash1; HashRev<1940348388277362841ULL> hash2; }; int main() { StrongHash sh; StrongHashRev shrev; int n; scanf("%d\n", &n); char c; while (c = getchar()) { int x = c - 'a' + 1; if (x > 26 || x < 1) break; sh.Append(x); shrev.Append(x); } if (sh.Extract1() == shrev.Extract1() && sh.Extract2() == shrev.Extract2()) { printf("TAK\n"); } else { printf("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 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | /* Hashowanie napisow na podstawie rozwiazania zadania Poszukiwania (autor Tomasz Czajka) z Potyczek Algorytmicznych 2015 */ #include <cassert> #include <iostream> #include <limits> #include <vector> #include <cstring> #include <cstdio> #include <ctime> using std::numeric_limits; using std::vector; typedef long long LL; typedef unsigned long long ULL; // Mersenne prime. constexpr int primeExponent = 61; constexpr ULL prime = (1ULL << primeExponent) - 1ULL; // Modulo prime. class Mod { public: // x must be < p explicit constexpr Mod(ULL x) : val{x} {} Mod operator+(Mod b) const { ULL res = val + b.val; if (res >= prime) res -= prime; return Mod{res}; } Mod operator-(Mod b) const { ULL res = val; if (res < b.val) res += prime; res -= b.val; return Mod{res}; } Mod operator*(unsigned b) const { unsigned a0 = unsigned(val); unsigned a1 = unsigned(val >> 32); ULL res0 = ULL(a0) * ULL(b); ULL res1 = ULL(a1) * ULL(b); ULL res = (res0 & prime) + (res0 >> primeExponent) + ((res1 << 32) & prime) + (res1 >> (primeExponent - 32)); return Mod{res % prime}; } Mod operator*(Mod b) const { unsigned a0 = unsigned(val); unsigned a1 = unsigned(val >> 32); unsigned b0 = unsigned(b.val); unsigned b1 = unsigned(b.val >> 32); ULL res0 = ULL(a0) * ULL(b0); ULL res1 = ULL(a0) * ULL(b1) + ULL(a1) * ULL(b0); ULL res2 = ULL(a1) * ULL(b1); ULL res = (res0 & prime) + (res0 >> primeExponent) + ((res1 << 32) & prime) + (res1 >> (primeExponent - 32)) + ((res2 << (64 - primeExponent)) & prime) + (res2 >> (2 * primeExponent - 64)); return Mod{res % prime}; } bool operator==(Mod b) const { return val == b.val; } void operator+=(Mod b) { *this = *this + b; } void operator-=(Mod b) { *this = *this - b; } void operator*=(unsigned b) { *this = *this * b; } void operator*=(Mod b) { *this = *this * b; } ULL Extract() const { return val; } private: ULL val; }; Mod Power(Mod a, ULL n) { Mod res{1}; ULL two_b = 1u; Mod a_two_b = a; while(two_b <= n) { if (n & two_b) res *= a_two_b; two_b <<= 1; a_two_b *= a_two_b; } return res; } template<ULL baseULL> class Hash { static_assert(baseULL < prime, "base < prime"); static constexpr Mod base = Mod{baseULL}; public: Hash() : val{0}, powBaseLen{0} {} Hash(Mod v) : val{v}, powBaseLen{0} {} void Append(unsigned c) { val = val * base + Mod{c}; } bool operator==(const Hash &b) const { return val == b.val; } Mod Extract() const { return val; } private: Mod val; Mod powBaseLen; }; template<ULL baseULL> constexpr Mod Hash<baseULL>::base; class StrongHash { public: StrongHash() : hash1{}, hash2{} {} StrongHash(Mod h1, Mod h2) : hash1{h1}, hash2{h2} {} void Append(unsigned c) { hash1.Append(c); hash2.Append(c); } bool operator==(const StrongHash &b) const { return hash1 == b.hash1 && hash2 == b.hash2; } Mod Extract1() const { return hash1.Extract(); } Mod Extract2() const { return hash2.Extract(); } private: Hash<1919222792405563639ULL> hash1; Hash<1940348388277362841ULL> hash2; }; //------------------------------------------------- template<ULL baseULL> class HashRev { static_assert(baseULL < prime, "base < prime"); static constexpr Mod base = Mod{baseULL}; public: Mod revPow{0}; HashRev() : val{0}, powBaseLen{1} { } HashRev(Mod v) : val{v}, powBaseLen{1} { } void Append(unsigned c) { Mod x = powBaseLen * Mod{c}; val = val + x; powBaseLen = powBaseLen * base; } Mod Extract() const { return val; } private: Mod val; Mod powBaseLen; }; template<ULL baseULL> constexpr Mod HashRev<baseULL>::base; class StrongHashRev { public: StrongHashRev() : hash1{}, hash2{} {} StrongHashRev(Mod h1, Mod h2) : hash1{h1}, hash2{h2} {} void Append(unsigned c) { hash1.Append(c); hash2.Append(c); } Mod Extract1() const { return hash1.Extract(); } Mod Extract2() const { return hash2.Extract(); } private: HashRev<1919222792405563639ULL> hash1; HashRev<1940348388277362841ULL> hash2; }; int main() { StrongHash sh; StrongHashRev shrev; int n; scanf("%d\n", &n); char c; while (c = getchar()) { int x = c - 'a' + 1; if (x > 26 || x < 1) break; sh.Append(x); shrev.Append(x); } if (sh.Extract1() == shrev.Extract1() && sh.Extract2() == shrev.Extract2()) { printf("TAK\n"); } else { printf("NIE\n"); } return 0; } |