#include <bits/stdc++.h> using namespace std; const int MAXA = 1e9 + 7; const int MAXB = 1e9 + 9; const int base = 27; typedef long long LL; long long hashfa, hashea, hashfb, hasheb, basea, baseb; int main() { int n; char a; cin >> n; basea = baseb = 1; while(cin >> a){ hashfa = ((LL)(hashfa*base)%MAXA + a - 'a' + 1)%MAXA; hashfb = ((LL)(hashfb*base)%MAXB + a - 'a' + 1)%MAXB; hashea = (hashea + (LL)(a - 'a' + 1)*basea)%MAXA; hasheb = (hasheb + (LL)(a - 'a' + 1)*baseb)%MAXB; basea = (LL)(basea * base)%MAXA; baseb = (LL)(baseb * base)%MAXB; } if(hashfa == hashea && hashfb == hasheb) cout << "TAK\n"; else cout << "NIE\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 | #include <bits/stdc++.h> using namespace std; const int MAXA = 1e9 + 7; const int MAXB = 1e9 + 9; const int base = 27; typedef long long LL; long long hashfa, hashea, hashfb, hasheb, basea, baseb; int main() { int n; char a; cin >> n; basea = baseb = 1; while(cin >> a){ hashfa = ((LL)(hashfa*base)%MAXA + a - 'a' + 1)%MAXA; hashfb = ((LL)(hashfb*base)%MAXB + a - 'a' + 1)%MAXB; hashea = (hashea + (LL)(a - 'a' + 1)*basea)%MAXA; hasheb = (hasheb + (LL)(a - 'a' + 1)*baseb)%MAXB; basea = (LL)(basea * base)%MAXA; baseb = (LL)(baseb * base)%MAXB; } if(hashfa == hashea && hashfb == hasheb) cout << "TAK\n"; else cout << "NIE\n"; return 0; } |