#include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long ll; ll PRIME = 100000004987; ll x = 7; int main(){ ll hash = 0; ll hashRev = 0; ll currentPow = 1; char c; int n; cin >> n; while(cin >> c){ hash = ((c * currentPow) % PRIME + hash) % PRIME; hashRev = (((hashRev * x) % PRIME) + c ) % PRIME; currentPow = (currentPow * x) % PRIME; } if(hash == hashRev){ cout << "TAK" << endl; }else{ cout << "NIE" << endl; } 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long ll; ll PRIME = 100000004987; ll x = 7; int main(){ ll hash = 0; ll hashRev = 0; ll currentPow = 1; char c; int n; cin >> n; while(cin >> c){ hash = ((c * currentPow) % PRIME + hash) % PRIME; hashRev = (((hashRev * x) % PRIME) + c ) % PRIME; currentPow = (currentPow * x) % PRIME; } if(hash == hashRev){ cout << "TAK" << endl; }else{ cout << "NIE" << endl; } return 0; } |