#include<stdio.h> unsigned long long hashes1[10000]; unsigned long long hashes2[10000]; const unsigned PRIME_MOD1 = 1000000007; const unsigned K1 = 33; const unsigned PRIME_MOD2 = 2000000011; const unsigned K2 = 65599; const unsigned WINDOW_SIZE = 2048; unsigned char window[WINDOW_SIZE+1]; unsigned char offset_buffer[WINDOW_SIZE+1]; const unsigned SWITCH_POINT = 1048576; unsigned char buffer[SWITCH_POINT+1]; long hist[26]; unsigned long djb2(unsigned char *str) { unsigned long long hash = 0; int c; while (c = *str++) hash = (hash * K1 + c)%PRIME_MOD1; /* hash * 33 + c */ return hash; } unsigned long sdbm(unsigned char *str) { unsigned long long hash = 0; int c; while (c = *str++) hash = (hash * K2 + c)%PRIME_MOD2; return hash; } void reverse(unsigned char *str, long l) { char c; //printf("l=%d [%s]->", l, str); for(long i=0; i<l/2; i++) { c = str[i]; str[i] = str[l-i-1]; str[l-i-1] = c; } //printf("[%s]\n", str); } int main() { long n; bool palindrome = true; scanf("%ld", &n); char c; long i = 0, length = 0, multiplier = 1, direction=1; while((c = getchar())!=EOF && (c == ' '||c == '\n' ||c == '\r'||c == '\t')); if(n!=0 && n < SWITCH_POINT) { do { buffer[i] = c; i++; length++; }while(scanf("%c",&c)==1 && c!='\n' && c!='\r'); char diff = 0; long j=0; do{ diff = buffer[j]-buffer[length-j-1]; j++; }while(diff == 0 && j<length/2); if(diff == 0) printf("TAK\n"); else printf("NIE\n"); }else if(n == 0) { long j, ws, h=0, ret = 1; unsigned long long seed_pow_mod_prime1 = 1, seed_pow_mod_prime2 = 1, hash_djb2 = 0, hash_sdbm = 0; for(j=0; j<WINDOW_SIZE; j++) { seed_pow_mod_prime1*=K1; seed_pow_mod_prime1%=PRIME_MOD1; seed_pow_mod_prime2*=K2; seed_pow_mod_prime2%=PRIME_MOD2; } //printf("Read\n"); do{ for(j=0, ws=0; j<WINDOW_SIZE && ret == 1 && c!='\n' && c!='\r'; j++, ws++, i++) { hist[c-'a']++; window[j] = c; ret = scanf("%c",&c); } //printf("Hash1\n"); window[j] = 0; if(ret == 1 && c!='\n' && c!='\r') { hash_djb2 *= seed_pow_mod_prime1; hash_djb2 %= PRIME_MOD1; //printf("a. [%x]\n",hashes1[0]); hash_sdbm *= seed_pow_mod_prime2; hash_sdbm %= PRIME_MOD2; }else { for(j; j>0; j--) { hash_djb2 *= K1; hash_djb2 %= PRIME_MOD1; hash_sdbm *= K2; hash_sdbm %= PRIME_MOD2; } //printf("b. [%x]\n",hashes1[0]); } //printf("c. '%s' - [%x]\n",window, djb2(window)); hash_djb2 += djb2(window); hash_djb2 %= PRIME_MOD1; //printf("d. [%x]\n",hashes1[0]); hash_sdbm += sdbm(window); hash_sdbm %= PRIME_MOD2; //printf("Reverse\n"); reverse(window, ws); //printf("[%s]", window); hashes1[h] = djb2(window); //printf("e. '%s' - [%x]\n",window, rev_hashes1[h]); hashes2[h++] = sdbm(window); } while(ret== 1 && c!='\n' && c!='\r'); unsigned long long rh1 = 0, rh2 = 0; for(j=h-1; j>=0; j--) { rh1*= seed_pow_mod_prime1; rh1%= PRIME_MOD1; rh1 += hashes1[j]; rh1%= PRIME_MOD1; //printf("f. [%x]\n",rh1); rh2*= seed_pow_mod_prime2; rh2%= PRIME_MOD2; rh2 += hashes2[j]; rh2%= PRIME_MOD2; } //printf("rh1=%x hashes1=%x\n", rh1, hashes1[0]); if(rh1 == hash_djb2 && rh2 == hash_sdbm) palindrome=true; else palindrome=false; bool odd = false; for(int j=0; j<26; j++) if(hist[j]%2) if(i%2==0 || odd) { palindrome = false; break; } else odd = true; palindrome?printf("TAK\n"):printf("NIE\n"); } else { long chunks = n/WINDOW_SIZE, h=0, offset = 0, j; if(chunks%2) chunks--; offset = (n-chunks*WINDOW_SIZE)/2; //printf("offset = %d\n", offset); i = 1; for(int j=0; j<offset; j++, i++) { hist[c-'a']++; offset_buffer[j] = c; scanf("%c",&c); } while(i+offset<n/2) { for(j=0; j<WINDOW_SIZE; j++, i++) { hist[c-'a']++; window[j] = c; scanf("%c",&c); } window[j] = 0; hashes1[h] = djb2(window); hashes2[h++] = sdbm(window); //printf("a. %d %s -> [djb2=%u] [sdbm=%u]\n", h-1, window, hashes1[h-1], hashes2[h-1]); } //the mid point reached. if(n%2) { //skip character that is in the middle of the input string i++; scanf("%c",&c); } h--; unsigned long h_djb2, h_sdbm; while(n-i>offset) { window[WINDOW_SIZE] = 0; for(j=WINDOW_SIZE-1; j>=0; j--, i++) { hist[c-'a']--; window[j] = c; scanf("%c",&c); } h_djb2 = djb2(window); h_sdbm = sdbm(window); //printf("b. %s -> [djb2=%u] [sdbm=%u]\n", window, h_djb2, h_sdbm); if(hashes1[h] != h_djb2 || hashes2[h--] != h_sdbm) { palindrome = false; break; } } //Verify the last part - must be same as the offset when read in a reversed order. if(palindrome) { for(int j=offset-1; j>=0; j--, i++) { hist[c-'a']--; if(offset_buffer[j] != c) { palindrome = false; break; } scanf("%c",&c); } } for(int j=0; j<26; j++) if(hist[j]) palindrome=false; palindrome?printf("TAK\n"):printf("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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | #include<stdio.h> unsigned long long hashes1[10000]; unsigned long long hashes2[10000]; const unsigned PRIME_MOD1 = 1000000007; const unsigned K1 = 33; const unsigned PRIME_MOD2 = 2000000011; const unsigned K2 = 65599; const unsigned WINDOW_SIZE = 2048; unsigned char window[WINDOW_SIZE+1]; unsigned char offset_buffer[WINDOW_SIZE+1]; const unsigned SWITCH_POINT = 1048576; unsigned char buffer[SWITCH_POINT+1]; long hist[26]; unsigned long djb2(unsigned char *str) { unsigned long long hash = 0; int c; while (c = *str++) hash = (hash * K1 + c)%PRIME_MOD1; /* hash * 33 + c */ return hash; } unsigned long sdbm(unsigned char *str) { unsigned long long hash = 0; int c; while (c = *str++) hash = (hash * K2 + c)%PRIME_MOD2; return hash; } void reverse(unsigned char *str, long l) { char c; //printf("l=%d [%s]->", l, str); for(long i=0; i<l/2; i++) { c = str[i]; str[i] = str[l-i-1]; str[l-i-1] = c; } //printf("[%s]\n", str); } int main() { long n; bool palindrome = true; scanf("%ld", &n); char c; long i = 0, length = 0, multiplier = 1, direction=1; while((c = getchar())!=EOF && (c == ' '||c == '\n' ||c == '\r'||c == '\t')); if(n!=0 && n < SWITCH_POINT) { do { buffer[i] = c; i++; length++; }while(scanf("%c",&c)==1 && c!='\n' && c!='\r'); char diff = 0; long j=0; do{ diff = buffer[j]-buffer[length-j-1]; j++; }while(diff == 0 && j<length/2); if(diff == 0) printf("TAK\n"); else printf("NIE\n"); }else if(n == 0) { long j, ws, h=0, ret = 1; unsigned long long seed_pow_mod_prime1 = 1, seed_pow_mod_prime2 = 1, hash_djb2 = 0, hash_sdbm = 0; for(j=0; j<WINDOW_SIZE; j++) { seed_pow_mod_prime1*=K1; seed_pow_mod_prime1%=PRIME_MOD1; seed_pow_mod_prime2*=K2; seed_pow_mod_prime2%=PRIME_MOD2; } //printf("Read\n"); do{ for(j=0, ws=0; j<WINDOW_SIZE && ret == 1 && c!='\n' && c!='\r'; j++, ws++, i++) { hist[c-'a']++; window[j] = c; ret = scanf("%c",&c); } //printf("Hash1\n"); window[j] = 0; if(ret == 1 && c!='\n' && c!='\r') { hash_djb2 *= seed_pow_mod_prime1; hash_djb2 %= PRIME_MOD1; //printf("a. [%x]\n",hashes1[0]); hash_sdbm *= seed_pow_mod_prime2; hash_sdbm %= PRIME_MOD2; }else { for(j; j>0; j--) { hash_djb2 *= K1; hash_djb2 %= PRIME_MOD1; hash_sdbm *= K2; hash_sdbm %= PRIME_MOD2; } //printf("b. [%x]\n",hashes1[0]); } //printf("c. '%s' - [%x]\n",window, djb2(window)); hash_djb2 += djb2(window); hash_djb2 %= PRIME_MOD1; //printf("d. [%x]\n",hashes1[0]); hash_sdbm += sdbm(window); hash_sdbm %= PRIME_MOD2; //printf("Reverse\n"); reverse(window, ws); //printf("[%s]", window); hashes1[h] = djb2(window); //printf("e. '%s' - [%x]\n",window, rev_hashes1[h]); hashes2[h++] = sdbm(window); } while(ret== 1 && c!='\n' && c!='\r'); unsigned long long rh1 = 0, rh2 = 0; for(j=h-1; j>=0; j--) { rh1*= seed_pow_mod_prime1; rh1%= PRIME_MOD1; rh1 += hashes1[j]; rh1%= PRIME_MOD1; //printf("f. [%x]\n",rh1); rh2*= seed_pow_mod_prime2; rh2%= PRIME_MOD2; rh2 += hashes2[j]; rh2%= PRIME_MOD2; } //printf("rh1=%x hashes1=%x\n", rh1, hashes1[0]); if(rh1 == hash_djb2 && rh2 == hash_sdbm) palindrome=true; else palindrome=false; bool odd = false; for(int j=0; j<26; j++) if(hist[j]%2) if(i%2==0 || odd) { palindrome = false; break; } else odd = true; palindrome?printf("TAK\n"):printf("NIE\n"); } else { long chunks = n/WINDOW_SIZE, h=0, offset = 0, j; if(chunks%2) chunks--; offset = (n-chunks*WINDOW_SIZE)/2; //printf("offset = %d\n", offset); i = 1; for(int j=0; j<offset; j++, i++) { hist[c-'a']++; offset_buffer[j] = c; scanf("%c",&c); } while(i+offset<n/2) { for(j=0; j<WINDOW_SIZE; j++, i++) { hist[c-'a']++; window[j] = c; scanf("%c",&c); } window[j] = 0; hashes1[h] = djb2(window); hashes2[h++] = sdbm(window); //printf("a. %d %s -> [djb2=%u] [sdbm=%u]\n", h-1, window, hashes1[h-1], hashes2[h-1]); } //the mid point reached. if(n%2) { //skip character that is in the middle of the input string i++; scanf("%c",&c); } h--; unsigned long h_djb2, h_sdbm; while(n-i>offset) { window[WINDOW_SIZE] = 0; for(j=WINDOW_SIZE-1; j>=0; j--, i++) { hist[c-'a']--; window[j] = c; scanf("%c",&c); } h_djb2 = djb2(window); h_sdbm = sdbm(window); //printf("b. %s -> [djb2=%u] [sdbm=%u]\n", window, h_djb2, h_sdbm); if(hashes1[h] != h_djb2 || hashes2[h--] != h_sdbm) { palindrome = false; break; } } //Verify the last part - must be same as the offset when read in a reversed order. if(palindrome) { for(int j=offset-1; j>=0; j--, i++) { hist[c-'a']--; if(offset_buffer[j] != c) { palindrome = false; break; } scanf("%c",&c); } } for(int j=0; j<26; j++) if(hist[j]) palindrome=false; palindrome?printf("TAK\n"):printf("NIE\n"); } return 0; } |