#include <bits/stdc++.h> using namespace std; #define e1 first #define e2 second #define pb push_back #define mp make_pair #define boost ios_base::sync_with_stdio(false) #define eb emplace_back #define OUT(x) {cout << x; exit(0); } #define scanf(...) scanf(__VA_ARGS__)?:0 #define FOR(i, a, b) for (int i=(a); i<=(b); ++i) typedef long long ll; typedef unsigned long long ull; typedef pair <int, int> PII; typedef pair <ll, ll> PLL; typedef pair <PLL, PLL> PP; typedef unsigned int ui; const int mod = 1e9+696969; const int inf = 1e9+9; const ll INF = 1e18; const ll P = 31; ull P2 = 31; ll P3 = 29; int LEN = 20000000; int random(int a, int b ){ return rand()%(b-a+1) + a; } int main() { int mod2 = random(inf - 100, inf + 100); srand(time(0)); int xd; scanf("%d\n", &xd); xd = 0; ll hash1 = 0, rev1 = 0; ull hash2 = 0, rev2 = 0; ll hash3 = 0, rev3 = 0; ull pot = 1, pot2 = 1, pot3 = 1; int i = 1; while (1) { char zn; zn = getchar_unlocked(); //if (i > LEN) zn = '\n'; //else zn = 'a'; //++i; if (zn < 'a' || zn > 'z') { if (hash1 == rev1 && hash2 == rev2 && hash3 == rev3) OUT("TAK"); OUT("NIE"); } ull liczba = zn - 'a' + 1; hash1 = (P * hash1 + liczba) % mod; hash2 = (P2 * hash2 + liczba); hash3 = (P3 * hash3 + liczba) % mod2; //to sa hasze od przodu rev1 = (rev1 + pot * liczba) % mod; rev2 = (rev2 + pot2 * liczba); rev3 = (rev3 + pot3 * liczba) % mod2; pot = P * pot % mod; pot2 = P2 * pot2; pot3 = P3 * pot3 % mod2; } }
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 | #include <bits/stdc++.h> using namespace std; #define e1 first #define e2 second #define pb push_back #define mp make_pair #define boost ios_base::sync_with_stdio(false) #define eb emplace_back #define OUT(x) {cout << x; exit(0); } #define scanf(...) scanf(__VA_ARGS__)?:0 #define FOR(i, a, b) for (int i=(a); i<=(b); ++i) typedef long long ll; typedef unsigned long long ull; typedef pair <int, int> PII; typedef pair <ll, ll> PLL; typedef pair <PLL, PLL> PP; typedef unsigned int ui; const int mod = 1e9+696969; const int inf = 1e9+9; const ll INF = 1e18; const ll P = 31; ull P2 = 31; ll P3 = 29; int LEN = 20000000; int random(int a, int b ){ return rand()%(b-a+1) + a; } int main() { int mod2 = random(inf - 100, inf + 100); srand(time(0)); int xd; scanf("%d\n", &xd); xd = 0; ll hash1 = 0, rev1 = 0; ull hash2 = 0, rev2 = 0; ll hash3 = 0, rev3 = 0; ull pot = 1, pot2 = 1, pot3 = 1; int i = 1; while (1) { char zn; zn = getchar_unlocked(); //if (i > LEN) zn = '\n'; //else zn = 'a'; //++i; if (zn < 'a' || zn > 'z') { if (hash1 == rev1 && hash2 == rev2 && hash3 == rev3) OUT("TAK"); OUT("NIE"); } ull liczba = zn - 'a' + 1; hash1 = (P * hash1 + liczba) % mod; hash2 = (P2 * hash2 + liczba); hash3 = (P3 * hash3 + liczba) % mod2; //to sa hasze od przodu rev1 = (rev1 + pot * liczba) % mod; rev2 = (rev2 + pot2 * liczba); rev3 = (rev3 + pot3 * liczba) % mod2; pot = P * pot % mod; pot2 = P2 * pot2; pot3 = P3 * pot3 % mod2; } } |