#include <stdio.h> #define MOD_VAL 1000000007 #define NO_PRIMES 7 char c[2]; long long int primes[NO_PRIMES] = {31,47,53,101,127,131,157}; long long int hash[NO_PRIMES] = {0,0,0,0,0,0,0}; long long int revHash[NO_PRIMES] = {0,0,0,0,0,0,0}; long long int multi[NO_PRIMES] = {1,1,1,1,1,1,1}; int main(){ int n; scanf("%d", &n); while(scanf("%1s",c) != EOF){ // printf("(%s)\n",c); for(int i = 0; i < NO_PRIMES ; ++i){ hash[i] = (primes[i] * hash[i] + c[0]) % MOD_VAL; revHash[i] = (revHash[i] + multi[i] * c[0]) % MOD_VAL; multi[i] = (multi[i] * primes[i]) % MOD_VAL; } } for(int i = 0; i < NO_PRIMES ; ++i){ if (hash[i] != revHash[i]){ printf("NIE\n"); return 0; } // printf("%lld %lld\n",hash[i],revHash[i]); } printf("TAK\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 | #include <stdio.h> #define MOD_VAL 1000000007 #define NO_PRIMES 7 char c[2]; long long int primes[NO_PRIMES] = {31,47,53,101,127,131,157}; long long int hash[NO_PRIMES] = {0,0,0,0,0,0,0}; long long int revHash[NO_PRIMES] = {0,0,0,0,0,0,0}; long long int multi[NO_PRIMES] = {1,1,1,1,1,1,1}; int main(){ int n; scanf("%d", &n); while(scanf("%1s",c) != EOF){ // printf("(%s)\n",c); for(int i = 0; i < NO_PRIMES ; ++i){ hash[i] = (primes[i] * hash[i] + c[0]) % MOD_VAL; revHash[i] = (revHash[i] + multi[i] * c[0]) % MOD_VAL; multi[i] = (multi[i] * primes[i]) % MOD_VAL; } } for(int i = 0; i < NO_PRIMES ; ++i){ if (hash[i] != revHash[i]){ printf("NIE\n"); return 0; } // printf("%lld %lld\n",hash[i],revHash[i]); } printf("TAK\n"); return 0; } |