#include <cstdio> long long mod[2] = {1000000007, 1000000009}; long long p[2] = {1000007, 31}; long long pot[2] = {1, 1}; long long v[2][2]; int main(){ int n; scanf("%d", &n); char a; while (scanf(" %c", &a) != EOF){ for (int i = 0; i < 2; i++){ v[0][i] += pot[i] * (a - 'a' + 1); v[0][i] %= mod[i]; pot[i] *= p[i]; pot[i] %= mod[i]; v[1][i] *= p[i]; v[1][i] += (a - 'a' + 1); v[1][i] %= mod[i]; //printf("%d %d %lld %lld %lld %lld\n", (int)a, (int)'a', pot[i], p[i], v[0][i], v[1][i]); } } if ((v[0][0] == v[1][0]) && (v[0][1] == v[1][1])){ printf("TAK\n"); } else { //printf("%lld %lld\n", v[0][1], v[1][1]); printf("NIE\n"); } }
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 | #include <cstdio> long long mod[2] = {1000000007, 1000000009}; long long p[2] = {1000007, 31}; long long pot[2] = {1, 1}; long long v[2][2]; int main(){ int n; scanf("%d", &n); char a; while (scanf(" %c", &a) != EOF){ for (int i = 0; i < 2; i++){ v[0][i] += pot[i] * (a - 'a' + 1); v[0][i] %= mod[i]; pot[i] *= p[i]; pot[i] %= mod[i]; v[1][i] *= p[i]; v[1][i] += (a - 'a' + 1); v[1][i] %= mod[i]; //printf("%d %d %lld %lld %lld %lld\n", (int)a, (int)'a', pot[i], p[i], v[0][i], v[1][i]); } } if ((v[0][0] == v[1][0]) && (v[0][1] == v[1][1])){ printf("TAK\n"); } else { //printf("%lld %lld\n", v[0][1], v[1][1]); printf("NIE\n"); } } |