SHARE
TWEET

pal

bartekltg Dec 14th, 2018 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdint>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. template <unsigned int M, unsigned  int base>
  9. class hashh{
  10. public:
  11.     hashh(): val(0),rp(1) {};
  12.     uint32_t val;
  13.     uint32_t rp;
  14.     void add(uint32_t a){
  15.         val = (val + rp*a)%M;
  16.         rp=(rp*base)%M;
  17.     }
  18. };
  19.  
  20. template <unsigned int M, unsigned  int base>
  21. class rev_hash{
  22. public:
  23.     rev_hash(): val(0) {};
  24.     uint32_t val;
  25.     void add(uint32_t b0){
  26.         val = (val*base + b0)%M;
  27.     }
  28. };
  29.  
  30.  
  31. int main() {
  32.     ios_base::sync_with_stdio(0);
  33.     cin.tie(0);
  34.  
  35.     int n;
  36.     cin >> n;
  37.     hashh<32711, 31> h1;
  38.     rev_hash<32711, 31> rh1;
  39.     hashh<32999, 29> h2;
  40.     rev_hash<32999, 29> rh2;
  41.     hashh<42709, 29> h3;
  42.     rev_hash<42709, 29> rh3;
  43.  
  44.     char c;
  45.  
  46.     while (cin >> c && isalpha(c)) {
  47.         uint32_t t = c - 'a';
  48.         h1.add(t);
  49.         rh1.add(t);
  50.         h2.add(t);
  51.         rh2.add(t);
  52.         h3.add(t);
  53.         rh3.add(t);
  54.     }
  55.     cout<< ((rh1.val==h1.val && rh2.val==h2.val && rh3.val==h3.val)?"TAK":"NIE")<<endl;
  56.     return 0;
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top