#include <cstdio> int n; char tab[3000000]; long long NICE_PRIME1 = 87452237L; long long MOD_PRIME1 = 97459169L; void bruteforce(){ int c; n=0; while ((c = getchar()) != EOF) { tab[n++]=c; //printf("'%d:%c'",n-1,c); } for(int i = 0 ; i < n/2; i++){ if(tab[i]!=tab[n-i-1]){ printf("NIE\n"); return; } } printf("TAK\n"); } long long lefthash = 0, righthash=0, rightprime=1; void hashtest(){ // printf("===\n"); int i; int halfn = n/2; char c; for(i = 0 ; i < halfn; i++){ c = getchar(); lefthash=(lefthash * NICE_PRIME1 + c) % MOD_PRIME1; } if(n%2){ getchar(); halfn++; } for(i=halfn;i<n;i++){ c = getchar(); righthash = (righthash+(c*rightprime))%MOD_PRIME1; rightprime = (rightprime*NICE_PRIME1)%MOD_PRIME1; } if(righthash == lefthash){ printf("TAK\n"); } else{ printf("NIE\n"); } } int main() // (void) not necessary in C++ { scanf("%d\n",&n); if(n<1){ bruteforce(); } else { hashtest(); } 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 | #include <cstdio> int n; char tab[3000000]; long long NICE_PRIME1 = 87452237L; long long MOD_PRIME1 = 97459169L; void bruteforce(){ int c; n=0; while ((c = getchar()) != EOF) { tab[n++]=c; //printf("'%d:%c'",n-1,c); } for(int i = 0 ; i < n/2; i++){ if(tab[i]!=tab[n-i-1]){ printf("NIE\n"); return; } } printf("TAK\n"); } long long lefthash = 0, righthash=0, rightprime=1; void hashtest(){ // printf("===\n"); int i; int halfn = n/2; char c; for(i = 0 ; i < halfn; i++){ c = getchar(); lefthash=(lefthash * NICE_PRIME1 + c) % MOD_PRIME1; } if(n%2){ getchar(); halfn++; } for(i=halfn;i<n;i++){ c = getchar(); righthash = (righthash+(c*rightprime))%MOD_PRIME1; rightprime = (rightprime*NICE_PRIME1)%MOD_PRIME1; } if(righthash == lefthash){ printf("TAK\n"); } else{ printf("NIE\n"); } } int main() // (void) not necessary in C++ { scanf("%d\n",&n); if(n<1){ bruteforce(); } else { hashtest(); } return 0; } |