#include<iostream> #include<string> #include<vector> #include <stdio.h> using namespace std; int howmany = 9; vector<unsigned> hashes (howmany, 0); vector<unsigned> rhashes (howmany, 0); vector<unsigned> mantis (howmany, 1); vector<unsigned> primes; unsigned inf = 10000009; int main(){ int n = howmany; int cur = 127; for(int i = 0; i < howmany;){ cur++; bool prime = true; for(int j = 2; j * j <= cur; j++){ if(cur % j == 0){ prime = false; break; } } if(prime){ i++; primes.push_back(cur); } } cin >> n; char a; while(cin >> a){ char cur = a; int numb = cur - 'a'; for(int i = 0; i < howmany; i++){ hashes[i] *= primes[i] ; hashes[i] += numb; rhashes[i] += mantis[i] * numb; mantis[i] *= primes[i]; hashes[i] %= inf; rhashes[i] %= inf; mantis[i] %= inf; } } for(int i = 0; i < howmany; i++){ if(hashes[i] != rhashes[i]){ cout << "NIE" << endl; return 0; } } cout << "TAK" << 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include<iostream> #include<string> #include<vector> #include <stdio.h> using namespace std; int howmany = 9; vector<unsigned> hashes (howmany, 0); vector<unsigned> rhashes (howmany, 0); vector<unsigned> mantis (howmany, 1); vector<unsigned> primes; unsigned inf = 10000009; int main(){ int n = howmany; int cur = 127; for(int i = 0; i < howmany;){ cur++; bool prime = true; for(int j = 2; j * j <= cur; j++){ if(cur % j == 0){ prime = false; break; } } if(prime){ i++; primes.push_back(cur); } } cin >> n; char a; while(cin >> a){ char cur = a; int numb = cur - 'a'; for(int i = 0; i < howmany; i++){ hashes[i] *= primes[i] ; hashes[i] += numb; rhashes[i] += mantis[i] * numb; mantis[i] *= primes[i]; hashes[i] %= inf; rhashes[i] %= inf; mantis[i] %= inf; } } for(int i = 0; i < howmany; i++){ if(hashes[i] != rhashes[i]){ cout << "NIE" << endl; return 0; } } cout << "TAK" << endl; return 0; } |