#include <cstdio> #define rep(i,a,b) for (int i=a; i<b; i++) #define pb push_back #define mp make_pair #define fi first #define se second #define debug // using namespace std; typedef long long ll; struct hasz { ll mod; ll pod; ll pot; void init (ll mod1, ll pod1) { mod=mod1; pod=pod1; pot=1; } ll jed; ll dru; void lewo (char c) { jed=jed*pod+(ll)(c-'a'+1); if (jed>=mod) jed%=mod; } void prawo (char c) { dru=pot*(ll)(c-'a'+1)+dru; pot*=pod; if (pot>=mod) pot%=mod; if (dru>=mod) dru%=mod; } bool pal () { return (jed==dru); } }; hasz tab[5]; int main () { //ios_base::sync_with_stdio(false); tab[1].init(1234567891LL, 29LL); tab[0].init(1000000009LL, 29LL); tab[2].init(218216651LL, 31LL); tab[3].init(256089259LL, 31LL); tab[4].init(73358480136091LL, 29LL); int d; char c; scanf ("%d ", &d); scanf ("%c", &c); while (c<='z' && c>='a') { rep(i,3,5) { tab[i].lewo(c); tab[i].prawo(c); } scanf ("%c", &c); } bool f=true; rep(i,3,5) if (!tab[i].pal()) f=false; if (f) printf ("TAK\n"); else printf ("NIE\n"); }
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 | #include <cstdio> #define rep(i,a,b) for (int i=a; i<b; i++) #define pb push_back #define mp make_pair #define fi first #define se second #define debug // using namespace std; typedef long long ll; struct hasz { ll mod; ll pod; ll pot; void init (ll mod1, ll pod1) { mod=mod1; pod=pod1; pot=1; } ll jed; ll dru; void lewo (char c) { jed=jed*pod+(ll)(c-'a'+1); if (jed>=mod) jed%=mod; } void prawo (char c) { dru=pot*(ll)(c-'a'+1)+dru; pot*=pod; if (pot>=mod) pot%=mod; if (dru>=mod) dru%=mod; } bool pal () { return (jed==dru); } }; hasz tab[5]; int main () { //ios_base::sync_with_stdio(false); tab[1].init(1234567891LL, 29LL); tab[0].init(1000000009LL, 29LL); tab[2].init(218216651LL, 31LL); tab[3].init(256089259LL, 31LL); tab[4].init(73358480136091LL, 29LL); int d; char c; scanf ("%d ", &d); scanf ("%c", &c); while (c<='z' && c>='a') { rep(i,3,5) { tab[i].lewo(c); tab[i].prawo(c); } scanf ("%c", &c); } bool f=true; rep(i,3,5) if (!tab[i].pal()) f=false; if (f) printf ("TAK\n"); else printf ("NIE\n"); } |