#include<cstdio> #include<algorithm> using namespace std; int n; const int prime7 = 1000000007; const int prime9 = 1000000009; long long hash_przod_7 = 0; long long hash_tyl_7 = 0; long long hash_tyl_9 = 0; long long hash_przod_9 = 0; int main(){ scanf("%d\n", &n); long long pot7 = 1; long long pot9 = 1; int x = getchar(); while(x >= 'a' and x <= 'z'){ // printf("%lld %lld %lld %lld\n", hash_przod_7, hash_tyl_7, hash_przod_9, hash_tyl_9); hash_przod_7 *= 26LL; hash_przod_7 %= prime7; hash_przod_7 += (x - 'a'); hash_przod_7 %= prime7; hash_przod_9 *= 26LL; hash_przod_9 %= prime9; hash_przod_9 += (x - 'a'); hash_przod_9 %= prime9; long long x7 = pot7 * (long long)(x - 'a'); long long x9 = pot9 * (long long)(x - 'a'); x7 %= prime7; x9 %= prime9; hash_tyl_7 += x7; hash_tyl_7 %= prime7; hash_tyl_9 += x9; hash_tyl_9 %= prime9; pot7 *= 26LL; pot9 *= 26LL; pot7 %= prime7; pot9 %= prime9; x = getchar(); } // printf("%lld %lld %lld %lld\n", hash_przod_7, hash_tyl_7, hash_przod_9, hash_tyl_9); if(hash_przod_7 == hash_tyl_7 and hash_przod_9 == hash_tyl_9) printf("TAK"); else printf("NIE"); 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<cstdio> #include<algorithm> using namespace std; int n; const int prime7 = 1000000007; const int prime9 = 1000000009; long long hash_przod_7 = 0; long long hash_tyl_7 = 0; long long hash_tyl_9 = 0; long long hash_przod_9 = 0; int main(){ scanf("%d\n", &n); long long pot7 = 1; long long pot9 = 1; int x = getchar(); while(x >= 'a' and x <= 'z'){ // printf("%lld %lld %lld %lld\n", hash_przod_7, hash_tyl_7, hash_przod_9, hash_tyl_9); hash_przod_7 *= 26LL; hash_przod_7 %= prime7; hash_przod_7 += (x - 'a'); hash_przod_7 %= prime7; hash_przod_9 *= 26LL; hash_przod_9 %= prime9; hash_przod_9 += (x - 'a'); hash_przod_9 %= prime9; long long x7 = pot7 * (long long)(x - 'a'); long long x9 = pot9 * (long long)(x - 'a'); x7 %= prime7; x9 %= prime9; hash_tyl_7 += x7; hash_tyl_7 %= prime7; hash_tyl_9 += x9; hash_tyl_9 %= prime9; pot7 *= 26LL; pot9 *= 26LL; pot7 %= prime7; pot9 %= prime9; x = getchar(); } // printf("%lld %lld %lld %lld\n", hash_przod_7, hash_tyl_7, hash_przod_9, hash_tyl_9); if(hash_przod_7 == hash_tyl_7 and hash_przod_9 == hash_tyl_9) printf("TAK"); else printf("NIE"); return 0; } |