/*{{{ Includer.IncludeFiles(ctx, {[[C:\Projekty\r e p o\pali\src\main\pali.hpp]]}) ===*/ /* { -- filePaths [[C:\Projekty\r e p o\pali\src\main\pali.hpp]], }, */ #ifndef PALI_HPP #define PALI_HPP /// /// /// /*{{{ Includer.IncludeFiles(ctx, {[[C:\Projekty\r e p o\CommonLibrary_cpp\src\main\no_sillywarnings_please.h]]}) ===*/ /* { -- filePaths [[C:\Projekty\r e p o\CommonLibrary_cpp\src\main\no_sillywarnings_please.h]], }, */ // Based on no_sillywarnings_please, Copyright (c) Alf P. Steinbach, 2010. // See https://alfps.wordpress.com/the-no_sillywarnings_please-h-file/ . // Extra warnings added for /Wall. #ifndef PROGROCK_CPPX_COMPILERSPECIFIC_MSVC_NOSILLYWARNINGSPLEASE_H #define PROGROCK_CPPX_COMPILERSPECIFIC_MSVC_NOSILLYWARNINGSPLEASE_H #ifdef _MSC_VER // The following are real warnings but are generated by almost all MS headers, including // standard library headers, so it's impractical to leave them on. #pragma warning( disable: 4619 ) // there is no warning number 'XXXX' #pragma warning( disable: 4668 ) // XXX is not defined as a preprocessor macro // The following are pure sillywarnings: #pragma warning( disable: 4061 ) // enum value is not *explicitly* handled in switch #pragma warning( disable: 4099 ) // first seen using 'struct' now seen using 'class' #pragma warning( disable: 4127 ) // conditional expression is constant #pragma warning( disable: 4217 ) // member template isn't copy constructor #pragma warning( disable: 4250 ) // inherits (implements) some member via dominance #pragma warning( disable: 4251 ) // needs to have dll-interface to be used by clients #pragma warning( disable: 4275 ) // exported class derived from non-exported class #pragma warning( disable: 4347 ) // "behavior change", function called instead of template #pragma warning( disable: 4355 ) // "'this': used in member initializer list #pragma warning( disable: 4428 ) // MSVC 9: universal-character-name encountered in source #pragma warning( disable: 4505 ) // unreferenced function has been removed #pragma warning( disable: 4510 ) // default constructor could not be generated #pragma warning( disable: 4511 ) // copy constructor could not be generated #pragma warning( disable: 4512 ) // assignment operator could not be generated #pragma warning( disable: 4513 ) // destructor could not be generated #pragma warning( disable: 4610 ) // can never be instantiated user defined constructor required #pragma warning( disable: 4623 ) // default constructor could not be generated #pragma warning( disable: 4624 ) // destructor could not be generated #pragma warning( disable: 4625 ) // copy constructor could not be generated #pragma warning( disable: 4626 ) // assignment operator could not be generated #pragma warning( disable: 4640 ) // a local static object is not thread-safe #pragma warning( disable: 4661 ) // a member of the template class is not defined. #pragma warning( disable: 4670 ) // a base class of an exception class is inaccessible for catch #pragma warning( disable: 4672 ) // a base class of an exception class is ambiguous for catch #pragma warning( disable: 4673 ) // a base class of an exception class is inaccessible for catch #pragma warning( disable: 4675 ) // resolved overload was found by argument-dependent lookup #pragma warning( disable: 4702 ) // unreachable code, e.g. in <list> header. #pragma warning( disable: 4710 ) // call was not inlined #pragma warning( disable: 4711 ) // call was inlined #pragma warning( disable: 4820 ) // some padding was added #pragma warning( disable: 4917 ) // a GUID can only be associated with a class, interface or namespace #pragma warning( disable: 4996 ) // MSVC 9: a C std library function has been "deprecated" (says MS) // Extra for /Wall: #pragma warning( disable: 4350 ) // behavior change: 'member1' called instead of 'member2' #pragma warning( disable: 4351 ) // new behavior: elements of array 'array' will be default initialized #pragma warning( disable: 4514 ) // 'function' : unreferenced inline function has been removed #pragma warning( disable: 4718 ) // 'function call' : recursive call has no side effects, deleting #pragma warning( disable: 5045 ) // Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified #endif // _MSC_VER #endif // PROGROCK_CPPX_COMPILERSPECIFIC_MSVC_NOSILLYWARNINGSPLEASE_H /*}}}*/ /// /// /// /*{{{ Includer.IncludeFiles(ctx, {[[C:\Projekty\r e p o\CommonLibrary_cpp\src\main\disable_warnings.h]]}) ===*/ /* { -- filePaths [[C:\Projekty\r e p o\CommonLibrary_cpp\src\main\disable_warnings.h]], }, */ #ifdef _MSC_VER # pragma warning( push ) # pragma warning( disable: 4189 ) // 'identifier' : local variable is initialized but not referenced # pragma warning( disable: 4191 ) # pragma warning( disable: 4242 ) # pragma warning( disable: 4244 ) # pragma warning( disable: 4267 ) # pragma warning( disable: 4350 ) # pragma warning( disable: 4365 ) # pragma warning( disable: 4371 ) # pragma warning( disable: 4548 ) # pragma warning( disable: 4738 ) # pragma warning( disable: 4774 ) // '<function>' : format string expected in argument <position> is not a string literal # pragma warning( disable: 4820 ) # pragma warning( disable: 4987 ) # pragma warning( disable: 5026 ) # pragma warning( disable: 5027 ) #endif #ifdef __GNUC__ # if __GNUC__ >= 4 # if __GNUC_MINOR__ >= 6 # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Weffc++" # pragma GCC diagnostic ignored "-Wconversion" # pragma GCC diagnostic ignored "-Wsign-conversion" # pragma GCC diagnostic ignored "-Wshadow" # pragma GCC diagnostic ignored "-Wstrict-aliasing" # endif # endif #endif /*}}}*/ #ifdef UNDEF_NDEBUG #undef NDEBUG #endif #include <cstddef> #include <cassert> #include <algorithm> #include <vector> #include <set> #include <memory> #include <limits> #include <iostream> /*{{{ Includer.IncludeFiles(ctx, {[[C:\Projekty\r e p o\CommonLibrary_cpp\src\main\reenable_warnings.h]]}) ===*/ /* { -- filePaths [[C:\Projekty\r e p o\CommonLibrary_cpp\src\main\reenable_warnings.h]], }, */ #ifdef _MSC_VER # pragma warning( disable: 5031 ) // #pragma warning(pop): likely mismatch, popping warning state pushed in different file # pragma warning( pop ) #endif #ifdef __GNUC__ # if __GNUC__ >= 4 # if __GNUC_MINOR__ >= 6 # pragma GCC diagnostic pop # endif # endif #endif /*}}}*/ /// /// /// void noop(...) { } /// /// /// template<typename T> void debug_echo(T x, const char* name, const char* delim, int line) { std::cout << "DEBUG_ECHO " << name << ": " << delim << x << delim << " (line " << line << ")" << '\n'; } /// /// /// #define DEBUG_ECHO(x) do { debug_echo(x, #x, "", __LINE__); } while (0) /// /// /// #define DEBUG_ECHO_DELIM(x, delim) do { debug_echo(x, #x, delim, __LINE__); } while (0) namespace pali { /// /// /// typedef size_t umsize_t; typedef ptrdiff_t msize_t; typedef size_t umindex_t; typedef ptrdiff_t mindex_t; typedef ptrdiff_t munidiff_t; const mindex_t Null_Index = -1; const int Uninitialized_Positively = 7777777; const int Uninitialized_Negatively = -Uninitialized_Positively; } /// /// /// #ifdef OVERRIDE_NEW_DELETE namespace pali { namespace new_alloc { msize_t _allocated; msize_t _times; msize_t get_allocated() { return _allocated; } msize_t get_times() { return _times; } void clear_counters() { _allocated = 0; _times = 0; } }} void* operator new(size_t size) { pali::new_alloc::_allocated += static_cast<pali::msize_t>(size); ++pali::new_alloc::_times; return malloc(size); } void* operator new[](size_t size) { pali::new_alloc::_allocated += static_cast<pali::msize_t>(size); ++pali::new_alloc::_times; return malloc(size); } void operator delete(void* ptr) noexcept { free(ptr); } void operator delete[](void* ptr) noexcept { free(ptr); } #endif // OVERRIDE_NEW_DELETE namespace pali { namespace alloc { /// /// /// class Pool { public: Pool(msize_t size) : _size(size) , _pool(new char[static_cast<umsize_t>(_size)]) , _allocated(0) , _times(0) { } ~Pool() { delete[] _pool; } template<typename T> void* allocate(msize_t cnt) { if (cnt != 1) { noop(); } auto size = static_cast<msize_t>(cnt * sizeof(T)); if (_allocated + size > _size) { throw std::bad_alloc(); } auto result = _pool + _allocated; _allocated += size; ++_times; return result; } void deallocate(void* /*ptr*/) { } msize_t get_allocated() const { return _allocated; } msize_t get_times() const { return _times; } private: Pool(const Pool&); Pool& operator=(const Pool&); msize_t const _size; char* const _pool; msize_t _allocated; msize_t _times; }; /// /// /// template<typename T> class CustomAllocator { public: typedef T value_type; typedef value_type* pointer; typedef const value_type* const_pointer; typedef value_type& reference; typedef const value_type& const_reference; template<typename U> struct rebind { typedef CustomAllocator<U> other; }; CustomAllocator(Pool& pool) : _pool(pool) { } template<typename U> CustomAllocator(const CustomAllocator<U>& other) : _pool(other._pool) { } value_type* allocate(size_t cnt) { //return static_cast<value_type*>(::operator new(cnt * sizeof(value_type))); return static_cast<value_type*>(_pool.allocate<value_type>(static_cast<msize_t>(cnt))); } void deallocate(value_type* ptr, size_t) { //::operator delete(ptr); _pool.deallocate(ptr); } void construct(value_type* ptr, const value_type& t) const { new(ptr) value_type(t); } void destroy(value_type* ptr) const { /* MSVC C4100 workaround */noop(ptr); ptr->~value_type(); } Pool& _pool; }; template<typename T, typename U> bool operator==(CustomAllocator<T> const&, CustomAllocator<U> const&) { return true; } template<typename T, typename U> bool operator!=(CustomAllocator<T> const& x, CustomAllocator<U> const& y) { return !(x == y); } } // namespace alloc namespace math { /// /// /// int inverse(int x, int mod) { int t = 0; int newt = 1; int r = mod; int newr = x; while (newr != 0) { int quotient = r / newr; int tmp; tmp = t; t = newt; newt = tmp - quotient * newt; tmp = r; r = newr; newr = tmp - quotient * newr; } assert(r == 1); if (t < 0) { t += mod; } return t; } } // namespace math namespace hash { /// /// /// struct RabinKarpHasher { RabinKarpHasher(msize_t size_max) : Base_To_Size(nullptr) , Inverse_Base_To_Size(nullptr) { Base_To_Size = new int[static_cast<umsize_t>(size_max + 1)]; Inverse_Base_To_Size = new int[static_cast<umsize_t>(size_max + 1)]; long long value = 1; Base_To_Size[0] = static_cast<int>(value); Inverse_Base_To_Size[0] = pali::math::inverse(static_cast<int>(value), Mod); for (mindex_t i=1; i <= size_max; ++i) { value *= Base; value %= Mod; Base_To_Size[i] = static_cast<int>(value); Inverse_Base_To_Size[i] = pali::math::inverse(static_cast<int>(value), Mod); } } ~RabinKarpHasher() { delete[] Base_To_Size; delete[] Inverse_Base_To_Size; } int hash(const std::vector<int>& tab, mindex_t from, msize_t size) { long long result = 0; for (mindex_t i=from; i <= from + size - 1; ++i) { result *= Base; result %= Mod; result += hv(tab[static_cast<umindex_t>(i)]); result %= Mod; } return static_cast<int>(result); } int hash(const int* tab, mindex_t from, msize_t size) { long long result = 0; for (mindex_t i=from; i <= from + size - 1; ++i) { result *= Base; result %= Mod; result += hv(tab[i]); result %= Mod; } return static_cast<int>(result); } int hash_next(const std::vector<int>& tab, int prev, mindex_t to) { long long result = prev; result *= Base; result %= Mod; result += hv(tab[static_cast<umindex_t>(to)]); result %= Mod; return static_cast<int>(result); } int hash_next(const int* tab, int prev, mindex_t to) { long long result = prev; result *= Base; result %= Mod; result += hv(tab[to]); result %= Mod; return static_cast<int>(result); } int hash_roll(const std::vector<int>& tab, int prev, mindex_t to, msize_t size) { long long result = prev; result *= Base; result %= Mod; result += hv(tab[static_cast<umindex_t>(to)]); result %= Mod; long long val = Base_To_Size[size] * hv(tab[static_cast<umindex_t>(to - size)]); val %= Mod; result += Mod - val; result %= Mod; return static_cast<int>(result); } int hash_roll(const int* tab, int prev, mindex_t to, msize_t size) { long long result = prev; result *= Base; result %= Mod; result += hv(tab[to]); result %= Mod; long long val = Base_To_Size[size] * hv(tab[to - size]); val %= Mod; result += Mod - val; result %= Mod; return static_cast<int>(result); } int sub_hash(int a, int b, int pos_diff) { long long result = a; result += Mod; result -= b; result *= Inverse_Base_To_Size[pos_diff]; result %= Mod; return static_cast<int>(result); } private: RabinKarpHasher(const RabinKarpHasher&); RabinKarpHasher& operator=(const RabinKarpHasher&); static const int Base = 131071; static const int Mod = 524287; int* Base_To_Size; int* Inverse_Base_To_Size; int hv(int x) { return x; } }; } // namespace hash } // namespace pali #endif // PALI_HPP /*}}}*/ using pali::Uninitialized_Negatively; using pali::msize_t; using pali::umsize_t; using pali::mindex_t; using pali::umindex_t; const int Number_of_RabinKarp_hashes = 2; struct RabinKarpHash { int h[Number_of_RabinKarp_hashes] = { 0 }; bool operator==(const RabinKarpHash &other) { for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { if (this->h[o] != other.h[o]) { return false; } } return true; } }; struct RabinKarpHasher { RabinKarpHasher(msize_t size_max_or_chunk, bool lookup_tables) { for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { if (lookup_tables) { Base_To_Size[o] = new int[static_cast<umsize_t>(size_max_or_chunk + 1)]; Inverse_Base_To_Size[o] = new int[static_cast<umsize_t>(size_max_or_chunk + 1)]; long long value = 1; Base_To_Size[o][0] = static_cast<int>(value); Inverse_Base_To_Size[o][0] = pali::math::inverse(static_cast<int>(value), Mod[o]); for (mindex_t i = 1; i <= size_max_or_chunk; ++i) { value *= Base[o]; value %= Mod[o]; Base_To_Size[o][i] = static_cast<int>(value); Inverse_Base_To_Size[o][i] = pali::math::inverse(static_cast<int>(value), Mod[o]); } Base_To_Chunk_Size[o] = Base_To_Size[o][size_max_or_chunk]; } else { long long value = 1; Base_To_Chunk_Size[o] = static_cast<int>(value); for (mindex_t i = 1; i <= size_max_or_chunk; ++i) { value *= Base[o]; value %= Mod[o]; Base_To_Chunk_Size[o] = static_cast<int>(value); } } } } ~RabinKarpHasher() { for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { if (Base_To_Size[o] != nullptr) { delete[] Base_To_Size[o]; } if (Inverse_Base_To_Size[o] != nullptr) { delete[] Inverse_Base_To_Size[o]; } } } RabinKarpHash hash(const std::vector<int>& tab, mindex_t from, msize_t size) { RabinKarpHash h; for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { long long result = 0; for (mindex_t i = from; i <= from + size - 1; ++i) { result *= Base[o]; result %= Mod[o]; result += hv(tab[static_cast<umindex_t>(i)]); result %= Mod[o]; } h.h[o] = static_cast<int>(result); } return h; } RabinKarpHash hash(const int* tab, mindex_t from, msize_t size) { RabinKarpHash h; for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { long long result = 0; for (mindex_t i = from; i <= from + size - 1; ++i) { result *= Base[o]; result %= Mod[o]; result += hv(tab[i]); result %= Mod[o]; } h.h[o] = static_cast<int>(result); } return h; } RabinKarpHash hash_next(const std::vector<int>& tab, int prev, mindex_t to) { RabinKarpHash h; for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { long long result = prev; result *= Base[o]; result %= Mod[o]; result += hv(tab[static_cast<umindex_t>(to)]); result %= Mod[o]; h.h[o] = static_cast<int>(result); } return h; } RabinKarpHash hash_next(const int* tab, int prev, mindex_t to) { RabinKarpHash h; for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { long long result = prev; result *= Base[o]; result %= Mod[o]; result += hv(tab[to]); result %= Mod[o]; h.h[o] = static_cast<int>(result); } return h; } RabinKarpHash hash_next(const RabinKarpHash& hash, int item) { RabinKarpHash h; for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { long long result = hash.h[o]; result *= Base[o]; result %= Mod[o]; result += hv(item); result %= Mod[o]; h.h[o] = static_cast<int>(result); } return h; } RabinKarpHash hash_chunk(const RabinKarpHash& hash, const RabinKarpHash& chunk_hash, msize_t chunk_number) { RabinKarpHash h; for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { long long result = hash.h[o]; for (mindex_t i = 0; i < chunk_number; ++i) { result *= Base_To_Chunk_Size[o]; result %= Mod[o]; } result += chunk_hash.h[o]; result %= Mod[o]; h.h[o] = static_cast<int>(result); } return h; } RabinKarpHash hash_roll(const std::vector<int>& tab, int prev, mindex_t to, msize_t size) { RabinKarpHash h; for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { long long result = prev; result *= Base[o]; result %= Mod[o]; result += hv(tab[static_cast<umindex_t>(to)]); result %= Mod[o]; long long val = Base_To_Size[o][size] * hv(tab[static_cast<umindex_t>(to - size)]); val %= Mod[o]; result += Mod[o] - val; result %= Mod[o]; h.h[o] = static_cast<int>(result); } return h; } RabinKarpHash hash_roll(const int* tab, int prev, mindex_t to, msize_t size) { RabinKarpHash h; for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { long long result = prev; result *= Base[o]; result %= Mod[o]; result += hv(tab[to]); result %= Mod[o]; long long val = Base_To_Size[o][size] * hv(tab[to - size]); val %= Mod[o]; result += Mod[o] - val; result %= Mod[o]; h.h[o] = static_cast<int>(result); } return h; } RabinKarpHash sub_hash(int a, int b, int pos_diff) { RabinKarpHash h; for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { long long result = a; result += Mod[o]; result -= b; result *= Inverse_Base_To_Size[o][pos_diff]; result %= Mod[o]; h.h[o] = static_cast<int>(result); } return h; } private: RabinKarpHasher(const RabinKarpHasher&); RabinKarpHasher& operator=(const RabinKarpHasher&); const int Base[Number_of_RabinKarp_hashes] = { 1643079, 1434596, };//1225823 }; const int Mod[Number_of_RabinKarp_hashes] = { 1534432337, 1456928321, };//1504918607 }; int* Base_To_Size[Number_of_RabinKarp_hashes]; int* Inverse_Base_To_Size[Number_of_RabinKarp_hashes]; int Base_To_Chunk_Size[Number_of_RabinKarp_hashes]; int hv(int x) { return x; } }; const int buf_len = 200000 + 7; char buf[buf_len + 1]; RabinKarpHasher hasher(buf_len, false); #if 1 #define NOT_DONE(fmt, total_len) (scanf((fmt), buf) > 0) #define GET(chunk, i, print_char) (buf[(i)]) #else #include <cstdlib> #ifdef _MSC_VER #pragma warning( disable: 4365 ) #pragma warning( disable: 4774 ) #endif const int Len = 20000000 + 1; bool NOT_DONE(char fmt[], int total_len) { noop(fmt); // scanf(fmt, buf); return total_len < Len; } char GET(int chunk, int i, bool print_char) { int idx = chunk * buf_len + i; if (idx == Len) { // putchar('\n'); return '\0'; } char c; if (2 * idx != Len - 1) { if (idx >= Len / 2) { idx = Len - 1 - idx; } // double v = exp(idx / 20000.0) + log(idx + 1); // c = 'a' + (char) fmod(v, 26); // c = 'a' + (char) idx % 26; c = 'a' + rand() % 26; } else { c = 'x'; } if (print_char) { // putchar(c); } return c; } #endif int main() { std::ios_base::sync_with_stdio(false); int N; scanf("%d", &N); RabinKarpHash hash_fwd; RabinKarpHash hash_bck; int chunk = 0; int total_len = 0; char fmt[11]; sprintf(fmt, "%%%ds", buf_len); # ifdef _MSC_VER # pragma warning( disable: 4774 ) # endif while (NOT_DONE(fmt, total_len)) { int i = 0; for (; GET(chunk, i, false) != '\0'; ++i) { hash_fwd = hasher.hash_next(hash_fwd, GET(chunk, i, true)); } total_len += i; RabinKarpHash curr_hash_bck; for (--i; i >= 0; --i) { curr_hash_bck = hasher.hash_next(curr_hash_bck, GET(chunk, i, false)); } hash_bck = hasher.hash_chunk(curr_hash_bck, hash_bck, chunk); ++chunk; } bool tak = (hash_fwd == hash_bck); if (tak && total_len <= buf_len) { for (int i = 0; i < total_len / 2; ++i) { if (buf[i] != buf[total_len - i - 1]) { tak = false; break; } } } printf("%s\n", (tak) ? "TAK" : "NIE"); }
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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 | /*{{{ Includer.IncludeFiles(ctx, {[[C:\Projekty\r e p o\pali\src\main\pali.hpp]]}) ===*/ /* { -- filePaths [[C:\Projekty\r e p o\pali\src\main\pali.hpp]], }, */ #ifndef PALI_HPP #define PALI_HPP /// /// /// /*{{{ Includer.IncludeFiles(ctx, {[[C:\Projekty\r e p o\CommonLibrary_cpp\src\main\no_sillywarnings_please.h]]}) ===*/ /* { -- filePaths [[C:\Projekty\r e p o\CommonLibrary_cpp\src\main\no_sillywarnings_please.h]], }, */ // Based on no_sillywarnings_please, Copyright (c) Alf P. Steinbach, 2010. // See https://alfps.wordpress.com/the-no_sillywarnings_please-h-file/ . // Extra warnings added for /Wall. #ifndef PROGROCK_CPPX_COMPILERSPECIFIC_MSVC_NOSILLYWARNINGSPLEASE_H #define PROGROCK_CPPX_COMPILERSPECIFIC_MSVC_NOSILLYWARNINGSPLEASE_H #ifdef _MSC_VER // The following are real warnings but are generated by almost all MS headers, including // standard library headers, so it's impractical to leave them on. #pragma warning( disable: 4619 ) // there is no warning number 'XXXX' #pragma warning( disable: 4668 ) // XXX is not defined as a preprocessor macro // The following are pure sillywarnings: #pragma warning( disable: 4061 ) // enum value is not *explicitly* handled in switch #pragma warning( disable: 4099 ) // first seen using 'struct' now seen using 'class' #pragma warning( disable: 4127 ) // conditional expression is constant #pragma warning( disable: 4217 ) // member template isn't copy constructor #pragma warning( disable: 4250 ) // inherits (implements) some member via dominance #pragma warning( disable: 4251 ) // needs to have dll-interface to be used by clients #pragma warning( disable: 4275 ) // exported class derived from non-exported class #pragma warning( disable: 4347 ) // "behavior change", function called instead of template #pragma warning( disable: 4355 ) // "'this': used in member initializer list #pragma warning( disable: 4428 ) // MSVC 9: universal-character-name encountered in source #pragma warning( disable: 4505 ) // unreferenced function has been removed #pragma warning( disable: 4510 ) // default constructor could not be generated #pragma warning( disable: 4511 ) // copy constructor could not be generated #pragma warning( disable: 4512 ) // assignment operator could not be generated #pragma warning( disable: 4513 ) // destructor could not be generated #pragma warning( disable: 4610 ) // can never be instantiated user defined constructor required #pragma warning( disable: 4623 ) // default constructor could not be generated #pragma warning( disable: 4624 ) // destructor could not be generated #pragma warning( disable: 4625 ) // copy constructor could not be generated #pragma warning( disable: 4626 ) // assignment operator could not be generated #pragma warning( disable: 4640 ) // a local static object is not thread-safe #pragma warning( disable: 4661 ) // a member of the template class is not defined. #pragma warning( disable: 4670 ) // a base class of an exception class is inaccessible for catch #pragma warning( disable: 4672 ) // a base class of an exception class is ambiguous for catch #pragma warning( disable: 4673 ) // a base class of an exception class is inaccessible for catch #pragma warning( disable: 4675 ) // resolved overload was found by argument-dependent lookup #pragma warning( disable: 4702 ) // unreachable code, e.g. in <list> header. #pragma warning( disable: 4710 ) // call was not inlined #pragma warning( disable: 4711 ) // call was inlined #pragma warning( disable: 4820 ) // some padding was added #pragma warning( disable: 4917 ) // a GUID can only be associated with a class, interface or namespace #pragma warning( disable: 4996 ) // MSVC 9: a C std library function has been "deprecated" (says MS) // Extra for /Wall: #pragma warning( disable: 4350 ) // behavior change: 'member1' called instead of 'member2' #pragma warning( disable: 4351 ) // new behavior: elements of array 'array' will be default initialized #pragma warning( disable: 4514 ) // 'function' : unreferenced inline function has been removed #pragma warning( disable: 4718 ) // 'function call' : recursive call has no side effects, deleting #pragma warning( disable: 5045 ) // Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified #endif // _MSC_VER #endif // PROGROCK_CPPX_COMPILERSPECIFIC_MSVC_NOSILLYWARNINGSPLEASE_H /*}}}*/ /// /// /// /*{{{ Includer.IncludeFiles(ctx, {[[C:\Projekty\r e p o\CommonLibrary_cpp\src\main\disable_warnings.h]]}) ===*/ /* { -- filePaths [[C:\Projekty\r e p o\CommonLibrary_cpp\src\main\disable_warnings.h]], }, */ #ifdef _MSC_VER # pragma warning( push ) # pragma warning( disable: 4189 ) // 'identifier' : local variable is initialized but not referenced # pragma warning( disable: 4191 ) # pragma warning( disable: 4242 ) # pragma warning( disable: 4244 ) # pragma warning( disable: 4267 ) # pragma warning( disable: 4350 ) # pragma warning( disable: 4365 ) # pragma warning( disable: 4371 ) # pragma warning( disable: 4548 ) # pragma warning( disable: 4738 ) # pragma warning( disable: 4774 ) // '<function>' : format string expected in argument <position> is not a string literal # pragma warning( disable: 4820 ) # pragma warning( disable: 4987 ) # pragma warning( disable: 5026 ) # pragma warning( disable: 5027 ) #endif #ifdef __GNUC__ # if __GNUC__ >= 4 # if __GNUC_MINOR__ >= 6 # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Weffc++" # pragma GCC diagnostic ignored "-Wconversion" # pragma GCC diagnostic ignored "-Wsign-conversion" # pragma GCC diagnostic ignored "-Wshadow" # pragma GCC diagnostic ignored "-Wstrict-aliasing" # endif # endif #endif /*}}}*/ #ifdef UNDEF_NDEBUG #undef NDEBUG #endif #include <cstddef> #include <cassert> #include <algorithm> #include <vector> #include <set> #include <memory> #include <limits> #include <iostream> /*{{{ Includer.IncludeFiles(ctx, {[[C:\Projekty\r e p o\CommonLibrary_cpp\src\main\reenable_warnings.h]]}) ===*/ /* { -- filePaths [[C:\Projekty\r e p o\CommonLibrary_cpp\src\main\reenable_warnings.h]], }, */ #ifdef _MSC_VER # pragma warning( disable: 5031 ) // #pragma warning(pop): likely mismatch, popping warning state pushed in different file # pragma warning( pop ) #endif #ifdef __GNUC__ # if __GNUC__ >= 4 # if __GNUC_MINOR__ >= 6 # pragma GCC diagnostic pop # endif # endif #endif /*}}}*/ /// /// /// void noop(...) { } /// /// /// template<typename T> void debug_echo(T x, const char* name, const char* delim, int line) { std::cout << "DEBUG_ECHO " << name << ": " << delim << x << delim << " (line " << line << ")" << '\n'; } /// /// /// #define DEBUG_ECHO(x) do { debug_echo(x, #x, "", __LINE__); } while (0) /// /// /// #define DEBUG_ECHO_DELIM(x, delim) do { debug_echo(x, #x, delim, __LINE__); } while (0) namespace pali { /// /// /// typedef size_t umsize_t; typedef ptrdiff_t msize_t; typedef size_t umindex_t; typedef ptrdiff_t mindex_t; typedef ptrdiff_t munidiff_t; const mindex_t Null_Index = -1; const int Uninitialized_Positively = 7777777; const int Uninitialized_Negatively = -Uninitialized_Positively; } /// /// /// #ifdef OVERRIDE_NEW_DELETE namespace pali { namespace new_alloc { msize_t _allocated; msize_t _times; msize_t get_allocated() { return _allocated; } msize_t get_times() { return _times; } void clear_counters() { _allocated = 0; _times = 0; } }} void* operator new(size_t size) { pali::new_alloc::_allocated += static_cast<pali::msize_t>(size); ++pali::new_alloc::_times; return malloc(size); } void* operator new[](size_t size) { pali::new_alloc::_allocated += static_cast<pali::msize_t>(size); ++pali::new_alloc::_times; return malloc(size); } void operator delete(void* ptr) noexcept { free(ptr); } void operator delete[](void* ptr) noexcept { free(ptr); } #endif // OVERRIDE_NEW_DELETE namespace pali { namespace alloc { /// /// /// class Pool { public: Pool(msize_t size) : _size(size) , _pool(new char[static_cast<umsize_t>(_size)]) , _allocated(0) , _times(0) { } ~Pool() { delete[] _pool; } template<typename T> void* allocate(msize_t cnt) { if (cnt != 1) { noop(); } auto size = static_cast<msize_t>(cnt * sizeof(T)); if (_allocated + size > _size) { throw std::bad_alloc(); } auto result = _pool + _allocated; _allocated += size; ++_times; return result; } void deallocate(void* /*ptr*/) { } msize_t get_allocated() const { return _allocated; } msize_t get_times() const { return _times; } private: Pool(const Pool&); Pool& operator=(const Pool&); msize_t const _size; char* const _pool; msize_t _allocated; msize_t _times; }; /// /// /// template<typename T> class CustomAllocator { public: typedef T value_type; typedef value_type* pointer; typedef const value_type* const_pointer; typedef value_type& reference; typedef const value_type& const_reference; template<typename U> struct rebind { typedef CustomAllocator<U> other; }; CustomAllocator(Pool& pool) : _pool(pool) { } template<typename U> CustomAllocator(const CustomAllocator<U>& other) : _pool(other._pool) { } value_type* allocate(size_t cnt) { //return static_cast<value_type*>(::operator new(cnt * sizeof(value_type))); return static_cast<value_type*>(_pool.allocate<value_type>(static_cast<msize_t>(cnt))); } void deallocate(value_type* ptr, size_t) { //::operator delete(ptr); _pool.deallocate(ptr); } void construct(value_type* ptr, const value_type& t) const { new(ptr) value_type(t); } void destroy(value_type* ptr) const { /* MSVC C4100 workaround */noop(ptr); ptr->~value_type(); } Pool& _pool; }; template<typename T, typename U> bool operator==(CustomAllocator<T> const&, CustomAllocator<U> const&) { return true; } template<typename T, typename U> bool operator!=(CustomAllocator<T> const& x, CustomAllocator<U> const& y) { return !(x == y); } } // namespace alloc namespace math { /// /// /// int inverse(int x, int mod) { int t = 0; int newt = 1; int r = mod; int newr = x; while (newr != 0) { int quotient = r / newr; int tmp; tmp = t; t = newt; newt = tmp - quotient * newt; tmp = r; r = newr; newr = tmp - quotient * newr; } assert(r == 1); if (t < 0) { t += mod; } return t; } } // namespace math namespace hash { /// /// /// struct RabinKarpHasher { RabinKarpHasher(msize_t size_max) : Base_To_Size(nullptr) , Inverse_Base_To_Size(nullptr) { Base_To_Size = new int[static_cast<umsize_t>(size_max + 1)]; Inverse_Base_To_Size = new int[static_cast<umsize_t>(size_max + 1)]; long long value = 1; Base_To_Size[0] = static_cast<int>(value); Inverse_Base_To_Size[0] = pali::math::inverse(static_cast<int>(value), Mod); for (mindex_t i=1; i <= size_max; ++i) { value *= Base; value %= Mod; Base_To_Size[i] = static_cast<int>(value); Inverse_Base_To_Size[i] = pali::math::inverse(static_cast<int>(value), Mod); } } ~RabinKarpHasher() { delete[] Base_To_Size; delete[] Inverse_Base_To_Size; } int hash(const std::vector<int>& tab, mindex_t from, msize_t size) { long long result = 0; for (mindex_t i=from; i <= from + size - 1; ++i) { result *= Base; result %= Mod; result += hv(tab[static_cast<umindex_t>(i)]); result %= Mod; } return static_cast<int>(result); } int hash(const int* tab, mindex_t from, msize_t size) { long long result = 0; for (mindex_t i=from; i <= from + size - 1; ++i) { result *= Base; result %= Mod; result += hv(tab[i]); result %= Mod; } return static_cast<int>(result); } int hash_next(const std::vector<int>& tab, int prev, mindex_t to) { long long result = prev; result *= Base; result %= Mod; result += hv(tab[static_cast<umindex_t>(to)]); result %= Mod; return static_cast<int>(result); } int hash_next(const int* tab, int prev, mindex_t to) { long long result = prev; result *= Base; result %= Mod; result += hv(tab[to]); result %= Mod; return static_cast<int>(result); } int hash_roll(const std::vector<int>& tab, int prev, mindex_t to, msize_t size) { long long result = prev; result *= Base; result %= Mod; result += hv(tab[static_cast<umindex_t>(to)]); result %= Mod; long long val = Base_To_Size[size] * hv(tab[static_cast<umindex_t>(to - size)]); val %= Mod; result += Mod - val; result %= Mod; return static_cast<int>(result); } int hash_roll(const int* tab, int prev, mindex_t to, msize_t size) { long long result = prev; result *= Base; result %= Mod; result += hv(tab[to]); result %= Mod; long long val = Base_To_Size[size] * hv(tab[to - size]); val %= Mod; result += Mod - val; result %= Mod; return static_cast<int>(result); } int sub_hash(int a, int b, int pos_diff) { long long result = a; result += Mod; result -= b; result *= Inverse_Base_To_Size[pos_diff]; result %= Mod; return static_cast<int>(result); } private: RabinKarpHasher(const RabinKarpHasher&); RabinKarpHasher& operator=(const RabinKarpHasher&); static const int Base = 131071; static const int Mod = 524287; int* Base_To_Size; int* Inverse_Base_To_Size; int hv(int x) { return x; } }; } // namespace hash } // namespace pali #endif // PALI_HPP /*}}}*/ using pali::Uninitialized_Negatively; using pali::msize_t; using pali::umsize_t; using pali::mindex_t; using pali::umindex_t; const int Number_of_RabinKarp_hashes = 2; struct RabinKarpHash { int h[Number_of_RabinKarp_hashes] = { 0 }; bool operator==(const RabinKarpHash &other) { for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { if (this->h[o] != other.h[o]) { return false; } } return true; } }; struct RabinKarpHasher { RabinKarpHasher(msize_t size_max_or_chunk, bool lookup_tables) { for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { if (lookup_tables) { Base_To_Size[o] = new int[static_cast<umsize_t>(size_max_or_chunk + 1)]; Inverse_Base_To_Size[o] = new int[static_cast<umsize_t>(size_max_or_chunk + 1)]; long long value = 1; Base_To_Size[o][0] = static_cast<int>(value); Inverse_Base_To_Size[o][0] = pali::math::inverse(static_cast<int>(value), Mod[o]); for (mindex_t i = 1; i <= size_max_or_chunk; ++i) { value *= Base[o]; value %= Mod[o]; Base_To_Size[o][i] = static_cast<int>(value); Inverse_Base_To_Size[o][i] = pali::math::inverse(static_cast<int>(value), Mod[o]); } Base_To_Chunk_Size[o] = Base_To_Size[o][size_max_or_chunk]; } else { long long value = 1; Base_To_Chunk_Size[o] = static_cast<int>(value); for (mindex_t i = 1; i <= size_max_or_chunk; ++i) { value *= Base[o]; value %= Mod[o]; Base_To_Chunk_Size[o] = static_cast<int>(value); } } } } ~RabinKarpHasher() { for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { if (Base_To_Size[o] != nullptr) { delete[] Base_To_Size[o]; } if (Inverse_Base_To_Size[o] != nullptr) { delete[] Inverse_Base_To_Size[o]; } } } RabinKarpHash hash(const std::vector<int>& tab, mindex_t from, msize_t size) { RabinKarpHash h; for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { long long result = 0; for (mindex_t i = from; i <= from + size - 1; ++i) { result *= Base[o]; result %= Mod[o]; result += hv(tab[static_cast<umindex_t>(i)]); result %= Mod[o]; } h.h[o] = static_cast<int>(result); } return h; } RabinKarpHash hash(const int* tab, mindex_t from, msize_t size) { RabinKarpHash h; for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { long long result = 0; for (mindex_t i = from; i <= from + size - 1; ++i) { result *= Base[o]; result %= Mod[o]; result += hv(tab[i]); result %= Mod[o]; } h.h[o] = static_cast<int>(result); } return h; } RabinKarpHash hash_next(const std::vector<int>& tab, int prev, mindex_t to) { RabinKarpHash h; for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { long long result = prev; result *= Base[o]; result %= Mod[o]; result += hv(tab[static_cast<umindex_t>(to)]); result %= Mod[o]; h.h[o] = static_cast<int>(result); } return h; } RabinKarpHash hash_next(const int* tab, int prev, mindex_t to) { RabinKarpHash h; for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { long long result = prev; result *= Base[o]; result %= Mod[o]; result += hv(tab[to]); result %= Mod[o]; h.h[o] = static_cast<int>(result); } return h; } RabinKarpHash hash_next(const RabinKarpHash& hash, int item) { RabinKarpHash h; for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { long long result = hash.h[o]; result *= Base[o]; result %= Mod[o]; result += hv(item); result %= Mod[o]; h.h[o] = static_cast<int>(result); } return h; } RabinKarpHash hash_chunk(const RabinKarpHash& hash, const RabinKarpHash& chunk_hash, msize_t chunk_number) { RabinKarpHash h; for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { long long result = hash.h[o]; for (mindex_t i = 0; i < chunk_number; ++i) { result *= Base_To_Chunk_Size[o]; result %= Mod[o]; } result += chunk_hash.h[o]; result %= Mod[o]; h.h[o] = static_cast<int>(result); } return h; } RabinKarpHash hash_roll(const std::vector<int>& tab, int prev, mindex_t to, msize_t size) { RabinKarpHash h; for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { long long result = prev; result *= Base[o]; result %= Mod[o]; result += hv(tab[static_cast<umindex_t>(to)]); result %= Mod[o]; long long val = Base_To_Size[o][size] * hv(tab[static_cast<umindex_t>(to - size)]); val %= Mod[o]; result += Mod[o] - val; result %= Mod[o]; h.h[o] = static_cast<int>(result); } return h; } RabinKarpHash hash_roll(const int* tab, int prev, mindex_t to, msize_t size) { RabinKarpHash h; for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { long long result = prev; result *= Base[o]; result %= Mod[o]; result += hv(tab[to]); result %= Mod[o]; long long val = Base_To_Size[o][size] * hv(tab[to - size]); val %= Mod[o]; result += Mod[o] - val; result %= Mod[o]; h.h[o] = static_cast<int>(result); } return h; } RabinKarpHash sub_hash(int a, int b, int pos_diff) { RabinKarpHash h; for (int o = 0; o < Number_of_RabinKarp_hashes; ++o) { long long result = a; result += Mod[o]; result -= b; result *= Inverse_Base_To_Size[o][pos_diff]; result %= Mod[o]; h.h[o] = static_cast<int>(result); } return h; } private: RabinKarpHasher(const RabinKarpHasher&); RabinKarpHasher& operator=(const RabinKarpHasher&); const int Base[Number_of_RabinKarp_hashes] = { 1643079, 1434596, };//1225823 }; const int Mod[Number_of_RabinKarp_hashes] = { 1534432337, 1456928321, };//1504918607 }; int* Base_To_Size[Number_of_RabinKarp_hashes]; int* Inverse_Base_To_Size[Number_of_RabinKarp_hashes]; int Base_To_Chunk_Size[Number_of_RabinKarp_hashes]; int hv(int x) { return x; } }; const int buf_len = 200000 + 7; char buf[buf_len + 1]; RabinKarpHasher hasher(buf_len, false); #if 1 #define NOT_DONE(fmt, total_len) (scanf((fmt), buf) > 0) #define GET(chunk, i, print_char) (buf[(i)]) #else #include <cstdlib> #ifdef _MSC_VER #pragma warning( disable: 4365 ) #pragma warning( disable: 4774 ) #endif const int Len = 20000000 + 1; bool NOT_DONE(char fmt[], int total_len) { noop(fmt); // scanf(fmt, buf); return total_len < Len; } char GET(int chunk, int i, bool print_char) { int idx = chunk * buf_len + i; if (idx == Len) { // putchar('\n'); return '\0'; } char c; if (2 * idx != Len - 1) { if (idx >= Len / 2) { idx = Len - 1 - idx; } // double v = exp(idx / 20000.0) + log(idx + 1); // c = 'a' + (char) fmod(v, 26); // c = 'a' + (char) idx % 26; c = 'a' + rand() % 26; } else { c = 'x'; } if (print_char) { // putchar(c); } return c; } #endif int main() { std::ios_base::sync_with_stdio(false); int N; scanf("%d", &N); RabinKarpHash hash_fwd; RabinKarpHash hash_bck; int chunk = 0; int total_len = 0; char fmt[11]; sprintf(fmt, "%%%ds", buf_len); # ifdef _MSC_VER # pragma warning( disable: 4774 ) # endif while (NOT_DONE(fmt, total_len)) { int i = 0; for (; GET(chunk, i, false) != '\0'; ++i) { hash_fwd = hasher.hash_next(hash_fwd, GET(chunk, i, true)); } total_len += i; RabinKarpHash curr_hash_bck; for (--i; i >= 0; --i) { curr_hash_bck = hasher.hash_next(curr_hash_bck, GET(chunk, i, false)); } hash_bck = hasher.hash_chunk(curr_hash_bck, hash_bck, chunk); ++chunk; } bool tak = (hash_fwd == hash_bck); if (tak && total_len <= buf_len) { for (int i = 0; i < total_len / 2; ++i) { if (buf[i] != buf[total_len - i - 1]) { tak = false; break; } } } printf("%s\n", (tak) ? "TAK" : "NIE"); } |