//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
//~ #pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define pb push_back
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(),x.end()
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD,LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;
template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
while(*sdbg!=',')cerr<<*sdbg++;
cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...);
}
#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#define cerr if(0)cout
#endif
const int maxn = (1e6)+7;
const int maxk = 20;
const int inf = (1e9)+7;
const LL LLinf = ((LL)1e18)+7LL;
const LD eps = 1e-9;
const LL mod = 1e9+7;
// ***************************** CODE ***************************** //
struct hasz
{
LL jed = 0LL;
LL dwa = 0LL;
LL mod;
LL pot = 1;
LL skok;
hasz(LL m, LL P)
{
mod = m;
skok = P;
}
void add(int a)
{
jed = (jed * skok + a) % mod;
dwa = (dwa + pot * a) % mod;
pot = (pot * skok) % mod;
}
void rob()
{
jed = (((jed % mod) + mod) % mod);
dwa = (((dwa % mod) + mod) % mod);
}
};
int main()
{
_upgrade
int n;
scanf("%d ", &n);
char a;
hasz cnt(1e9 + 7, 2137LL);
hasz elo(1000000271LL, 29LL);
while(scanf("%c", &a) > 0)
{
if(a < 'a' || a > 'z')
break;
a -= 'a';
a++;
cnt.add(a);
elo.add(a);
}
cnt.rob();
elo.rob();
// dbg(cnt.jed, cnt.dwa);
if(cnt.jed == cnt.dwa && elo.jed == elo.dwa)
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 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | //~ while (clock()<=69*CLOCKS_PER_SEC) //~ #pragma comment(linker, "/stack:200000000") //~ #pragma GCC optimize("O3") //~ #pragma GCC optimize("Ofast") //~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //~ #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #define pb push_back #define SZ(x) ((int)(x).size()) #define ALL(x) x.begin(),x.end() #define all(x) x.begin(),x.end() #define fi first #define se second #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x)))); using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; typedef vector<PII> VPII; typedef vector<PLL> VPLL; typedef vector<LL> VLL; typedef vector<int> VI; typedef vector<string> VS; typedef vector<char> VC; typedef long double LD; typedef pair<LD,LD> PLD; typedef vector<LD> VLD; typedef vector<PLD> VPLD; template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...); } #ifdef LOCAL #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define dbg(...) #define cerr if(0)cout #endif const int maxn = (1e6)+7; const int maxk = 20; const int inf = (1e9)+7; const LL LLinf = ((LL)1e18)+7LL; const LD eps = 1e-9; const LL mod = 1e9+7; // ***************************** CODE ***************************** // struct hasz { LL jed = 0LL; LL dwa = 0LL; LL mod; LL pot = 1; LL skok; hasz(LL m, LL P) { mod = m; skok = P; } void add(int a) { jed = (jed * skok + a) % mod; dwa = (dwa + pot * a) % mod; pot = (pot * skok) % mod; } void rob() { jed = (((jed % mod) + mod) % mod); dwa = (((dwa % mod) + mod) % mod); } }; int main() { _upgrade int n; scanf("%d ", &n); char a; hasz cnt(1e9 + 7, 2137LL); hasz elo(1000000271LL, 29LL); while(scanf("%c", &a) > 0) { if(a < 'a' || a > 'z') break; a -= 'a'; a++; cnt.add(a); elo.add(a); } cnt.rob(); elo.rob(); // dbg(cnt.jed, cnt.dwa); if(cnt.jed == cnt.dwa && elo.jed == elo.dwa) cout<<"TAK\n"; else cout<<"NIE\n"; return 0; } |
English