#include <iostream> #include <vector> using namespace std; // #define DEBUG #ifdef DEBUG #define PREFIX cerr << __FUNCTION__ << ": " #define PRINT(x) #x " = " << (x) #define PRINT1(x) PREFIX << PRINT(x) << endl #define PRINT2(x, y) PREFIX << PRINT(x) << ", " << PRINT(y) << endl #define PRINT3(x, y, z) PREFIX << PRINT(x) << ", " << PRINT(y) << ", " << PRINT(z) << endl #define PRINT4(x, y, z, t) PREFIX << PRINT(x) << ", " << PRINT(y) << ", " << PRINT(z) << ", " << PRINT(t) << endl #else #define PRINT1(x) #define PRINT2(x, y) #define PRINT3(x, y, z) #define PRINT4(x, y, z, t) #endif const int treshold = 350000; const int maxSize = 5 * 2 * treshold; // size of array const int maxElems = 5 * 2 * treshold; // number of elements stored in array char data[maxSize]; void put(char c, int pos) { data[pos] = c; // TODO } char read(int pos) { return data[pos]; // TODO } vector<int> counts[2], pairs[2], triples[2], firstPos, lastPos; void addCount(char c, bool secondHalf) { // PRINT2(c, secondHalf); ++counts[secondHalf][c - 'a']; } void addPair(char c1, char c2, bool secondHalf) { // PRINT3(c1, c2, secondHalf); ++pairs[secondHalf][26 * (c1 - 'a') + (c2 - 'a')]; } void addTriple(char c1, char c2, char c3, bool secondHalf) { // PRINT4(c1, c2, c3, secondHalf); ++triples[secondHalf][26 * 26 * (c1 - 'a') + 26 * (c2 - 'a') + (c3 - 'a')]; } bool checkPalindromSimple(int len) { for (int i = 0; i < len / 2; ++i) { if (read(i) != read(len - i - 1)) { return false; } } return true; } bool checkPalindrom(int len, bool halfs) { if (halfs) { for (int letter = 0; letter < 26; ++letter) { if (counts[0][letter] != counts[1][letter]) { PRINT4("counts fail", char(letter + 'a'), counts[0][letter], counts[1][letter]); return false; } } for (int l1 = 0; l1 < 26; ++l1) { for (int l2 = 0; l2 < 26; ++l2) { if (pairs[0][26 * l1 + l2] != pairs[1][26 * l2 + l1]) { PRINT3("pairs fail", char(l1 + 'a'), char(l2 + 'a')); return false; } } } for (int l1 = 0; l1 < 26; ++l1) { for (int l2 = 0; l2 < 26; ++l2) { for (int l3 = 0; l3 < 26; ++l3) { if (triples[0][26 * 26 * l1 + 26 * l2 + l3] != triples[1][26 * 26 * l3 + 26 * l2 + l1]) { PRINT4("triples fail", char(l1 + 'a'), char(l2 + 'a'), char(l3 + 'a')); return false; } } } } } else { int numOfOdd = 0; for (int letter = 0; letter < 26; ++letter) { bool odd = counts[0][letter] & 1; if (odd) { PRINT3("counts odd", char(letter + 'a'), counts[0][letter]); ++numOfOdd; if (numOfOdd > 1) return false; } } numOfOdd = 0; for (int l1 = 0; l1 < 26; ++l1) { for (int l2 = l1; l2 < 26; ++l2) { if (pairs[0][26 * l1 + l2] != pairs[0][26 * l2 + l1]) { PRINT3("pairs odd", char(l1 + 'a'), char(l2 + 'a')); if (l1 != l2) return false; ++numOfOdd; if (numOfOdd > 1) return false; } } } numOfOdd = 0; for (int l1 = 0; l1 < 26; ++l1) { for (int l2 = 0; l2 < 26; ++l2) { for (int l3 = 0; l3 < 26; ++l3) { if (triples[0][26 * 26 * l1 + 26 * l2 + l3] != triples[0][26 * 26 * l3 + 26 * l2 + l1]) { PRINT4("pairs odd", char(l1 + 'a'), char(l2 + 'a'), char(l3 + 'a')); if (l1 != l3) return false; ++numOfOdd; if (numOfOdd > 1) return false; } } } } } --len; for (int i = 0; i < 26; ++i) { if (firstPos[i] != -1 and firstPos[i] != len - lastPos[i]) { PRINT4(char(i + 'a'), len, firstPos[i], lastPos[i]); return false; } } len %= maxElems; ++len; for (int i = 0; i < maxElems / 2; ++i) { --len; if (len < maxElems / 2) len += maxElems / 2; if (read(i) != read(len)) { PRINT3("fail on pos", i, len); return false; } } return true; } void printCounts() { #ifdef DEBUG for (int i = 0; i < 26; ++i) { PRINT3(char(i + 'a'), counts[0][i], counts[1][i]); } #endif } void printPairs() { #ifdef DEBUG for (int i = 0; i < 26; ++i) { for (int j = 0; j < 26; ++j) { cerr << '(' << pairs[0][26 * i + j] << ',' << pairs[1][26 * j + i] << "),"; } cerr << endl; } #endif } bool palindromWithoutLength() { int i = 0; char c; for (; i < maxElems; ++i) { cin >> c; if (!cin) break; put(c, i); if (firstPos[c - 'a'] == -1) firstPos[c - 'a'] = i; lastPos[c - 'a'] = i; } if (!cin) { return checkPalindromSimple(i); } for (int j = 0; j < maxElems; ++j) { addCount(read(j), false); } for (int j = 1; j < maxElems; ++j) { #ifdef DEBUG if (read(j - 1) == 'f' and read(j) == 'a') cerr << "fa 1 na pozycji " << j << endl; #endif addPair(read(j - 1), read(j), false); } for (int j = 2; j < maxElems; ++j) { addTriple(read(j - 2), read(j - 1), read(j), false); } int realPos = i; int prevI = i - 1, prevII = i - 2; while (cin >> c) { if (i == maxElems) { i /= 2; } put(c, i); if (firstPos[c - 'a'] == -1) firstPos[c - 'a'] = realPos; lastPos[c - 'a'] = realPos; addCount(read(i), false); #ifdef DEBUG if (read(prevI) == 'f' and read(i) == 'a') cerr << "fa 2 na pozycji " << realPos << endl; #endif addPair(read(prevI), read(i), false); addTriple(read(prevII), read(prevI), read(i), false); prevII = prevI; prevI = i; ++i; ++realPos; } printCounts(); printPairs(); return checkPalindrom(realPos, false); } void readInput(int end) { PRINT1(end); char lastTwo[2]; cin >> lastTwo[0] >> lastTwo[1]; put(lastTwo[0], 0); firstPos[lastTwo[0] - 'a'] = 0; lastPos[lastTwo[0] - 'a'] = 0; addCount(lastTwo[0], false); put(lastTwo[1], 1); if (firstPos[lastTwo[1] - 'a'] == -1) firstPos[lastTwo[1] - 'a'] = 1; lastPos[lastTwo[1] - 'a'] = 1; addCount(lastTwo[1], false); addPair(lastTwo[0], lastTwo[1], false); for (int i = 2, j = i; j < end; ++i, ++j) { if (i == maxElems) { i /= 2; } char c; cin >> c; put(c, i); if (firstPos[c - 'a'] == -1) firstPos[c - 'a'] = i; lastPos[c - 'a'] = i; if (2 * j + 1 != end) { addCount(c, 2 * j + 1 > end); } if (2 * j != end) { addPair(lastTwo[1], c, 2 * j > end); } if (2 * j - 1 != end) { addTriple(lastTwo[0], lastTwo[1], c, 2 * j - 1 > end); } lastTwo[0] = lastTwo[1]; lastTwo[1] = c; } printCounts(); printPairs(); } void readInputSimple(int start, int end) { for (int i = start; i < end; ++i) { char c; cin >> c; put(c, i); } } bool palindromWithLength(int len) { if (len < maxElems) { readInputSimple(0, len); return checkPalindromSimple(len); } readInput(len); return checkPalindrom(len, true); } int main() { counts[0].resize(26); counts[1].resize(26); pairs[0].resize(26 * 26); pairs[1].resize(26 * 26); triples[0].resize(26 * 26 * 26); triples[1].resize(26 * 26 * 26); firstPos.resize(26, -1); lastPos.resize(26); int n; cin >> n; bool palindrom; if (n == 0) { palindrom = palindromWithoutLength(); } else { palindrom = palindromWithLength(n); } cout << (palindrom ? "TAK" : "NIE") << endl; }
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 278 279 280 281 282 283 284 285 286 287 288 | #include <iostream> #include <vector> using namespace std; // #define DEBUG #ifdef DEBUG #define PREFIX cerr << __FUNCTION__ << ": " #define PRINT(x) #x " = " << (x) #define PRINT1(x) PREFIX << PRINT(x) << endl #define PRINT2(x, y) PREFIX << PRINT(x) << ", " << PRINT(y) << endl #define PRINT3(x, y, z) PREFIX << PRINT(x) << ", " << PRINT(y) << ", " << PRINT(z) << endl #define PRINT4(x, y, z, t) PREFIX << PRINT(x) << ", " << PRINT(y) << ", " << PRINT(z) << ", " << PRINT(t) << endl #else #define PRINT1(x) #define PRINT2(x, y) #define PRINT3(x, y, z) #define PRINT4(x, y, z, t) #endif const int treshold = 350000; const int maxSize = 5 * 2 * treshold; // size of array const int maxElems = 5 * 2 * treshold; // number of elements stored in array char data[maxSize]; void put(char c, int pos) { data[pos] = c; // TODO } char read(int pos) { return data[pos]; // TODO } vector<int> counts[2], pairs[2], triples[2], firstPos, lastPos; void addCount(char c, bool secondHalf) { // PRINT2(c, secondHalf); ++counts[secondHalf][c - 'a']; } void addPair(char c1, char c2, bool secondHalf) { // PRINT3(c1, c2, secondHalf); ++pairs[secondHalf][26 * (c1 - 'a') + (c2 - 'a')]; } void addTriple(char c1, char c2, char c3, bool secondHalf) { // PRINT4(c1, c2, c3, secondHalf); ++triples[secondHalf][26 * 26 * (c1 - 'a') + 26 * (c2 - 'a') + (c3 - 'a')]; } bool checkPalindromSimple(int len) { for (int i = 0; i < len / 2; ++i) { if (read(i) != read(len - i - 1)) { return false; } } return true; } bool checkPalindrom(int len, bool halfs) { if (halfs) { for (int letter = 0; letter < 26; ++letter) { if (counts[0][letter] != counts[1][letter]) { PRINT4("counts fail", char(letter + 'a'), counts[0][letter], counts[1][letter]); return false; } } for (int l1 = 0; l1 < 26; ++l1) { for (int l2 = 0; l2 < 26; ++l2) { if (pairs[0][26 * l1 + l2] != pairs[1][26 * l2 + l1]) { PRINT3("pairs fail", char(l1 + 'a'), char(l2 + 'a')); return false; } } } for (int l1 = 0; l1 < 26; ++l1) { for (int l2 = 0; l2 < 26; ++l2) { for (int l3 = 0; l3 < 26; ++l3) { if (triples[0][26 * 26 * l1 + 26 * l2 + l3] != triples[1][26 * 26 * l3 + 26 * l2 + l1]) { PRINT4("triples fail", char(l1 + 'a'), char(l2 + 'a'), char(l3 + 'a')); return false; } } } } } else { int numOfOdd = 0; for (int letter = 0; letter < 26; ++letter) { bool odd = counts[0][letter] & 1; if (odd) { PRINT3("counts odd", char(letter + 'a'), counts[0][letter]); ++numOfOdd; if (numOfOdd > 1) return false; } } numOfOdd = 0; for (int l1 = 0; l1 < 26; ++l1) { for (int l2 = l1; l2 < 26; ++l2) { if (pairs[0][26 * l1 + l2] != pairs[0][26 * l2 + l1]) { PRINT3("pairs odd", char(l1 + 'a'), char(l2 + 'a')); if (l1 != l2) return false; ++numOfOdd; if (numOfOdd > 1) return false; } } } numOfOdd = 0; for (int l1 = 0; l1 < 26; ++l1) { for (int l2 = 0; l2 < 26; ++l2) { for (int l3 = 0; l3 < 26; ++l3) { if (triples[0][26 * 26 * l1 + 26 * l2 + l3] != triples[0][26 * 26 * l3 + 26 * l2 + l1]) { PRINT4("pairs odd", char(l1 + 'a'), char(l2 + 'a'), char(l3 + 'a')); if (l1 != l3) return false; ++numOfOdd; if (numOfOdd > 1) return false; } } } } } --len; for (int i = 0; i < 26; ++i) { if (firstPos[i] != -1 and firstPos[i] != len - lastPos[i]) { PRINT4(char(i + 'a'), len, firstPos[i], lastPos[i]); return false; } } len %= maxElems; ++len; for (int i = 0; i < maxElems / 2; ++i) { --len; if (len < maxElems / 2) len += maxElems / 2; if (read(i) != read(len)) { PRINT3("fail on pos", i, len); return false; } } return true; } void printCounts() { #ifdef DEBUG for (int i = 0; i < 26; ++i) { PRINT3(char(i + 'a'), counts[0][i], counts[1][i]); } #endif } void printPairs() { #ifdef DEBUG for (int i = 0; i < 26; ++i) { for (int j = 0; j < 26; ++j) { cerr << '(' << pairs[0][26 * i + j] << ',' << pairs[1][26 * j + i] << "),"; } cerr << endl; } #endif } bool palindromWithoutLength() { int i = 0; char c; for (; i < maxElems; ++i) { cin >> c; if (!cin) break; put(c, i); if (firstPos[c - 'a'] == -1) firstPos[c - 'a'] = i; lastPos[c - 'a'] = i; } if (!cin) { return checkPalindromSimple(i); } for (int j = 0; j < maxElems; ++j) { addCount(read(j), false); } for (int j = 1; j < maxElems; ++j) { #ifdef DEBUG if (read(j - 1) == 'f' and read(j) == 'a') cerr << "fa 1 na pozycji " << j << endl; #endif addPair(read(j - 1), read(j), false); } for (int j = 2; j < maxElems; ++j) { addTriple(read(j - 2), read(j - 1), read(j), false); } int realPos = i; int prevI = i - 1, prevII = i - 2; while (cin >> c) { if (i == maxElems) { i /= 2; } put(c, i); if (firstPos[c - 'a'] == -1) firstPos[c - 'a'] = realPos; lastPos[c - 'a'] = realPos; addCount(read(i), false); #ifdef DEBUG if (read(prevI) == 'f' and read(i) == 'a') cerr << "fa 2 na pozycji " << realPos << endl; #endif addPair(read(prevI), read(i), false); addTriple(read(prevII), read(prevI), read(i), false); prevII = prevI; prevI = i; ++i; ++realPos; } printCounts(); printPairs(); return checkPalindrom(realPos, false); } void readInput(int end) { PRINT1(end); char lastTwo[2]; cin >> lastTwo[0] >> lastTwo[1]; put(lastTwo[0], 0); firstPos[lastTwo[0] - 'a'] = 0; lastPos[lastTwo[0] - 'a'] = 0; addCount(lastTwo[0], false); put(lastTwo[1], 1); if (firstPos[lastTwo[1] - 'a'] == -1) firstPos[lastTwo[1] - 'a'] = 1; lastPos[lastTwo[1] - 'a'] = 1; addCount(lastTwo[1], false); addPair(lastTwo[0], lastTwo[1], false); for (int i = 2, j = i; j < end; ++i, ++j) { if (i == maxElems) { i /= 2; } char c; cin >> c; put(c, i); if (firstPos[c - 'a'] == -1) firstPos[c - 'a'] = i; lastPos[c - 'a'] = i; if (2 * j + 1 != end) { addCount(c, 2 * j + 1 > end); } if (2 * j != end) { addPair(lastTwo[1], c, 2 * j > end); } if (2 * j - 1 != end) { addTriple(lastTwo[0], lastTwo[1], c, 2 * j - 1 > end); } lastTwo[0] = lastTwo[1]; lastTwo[1] = c; } printCounts(); printPairs(); } void readInputSimple(int start, int end) { for (int i = start; i < end; ++i) { char c; cin >> c; put(c, i); } } bool palindromWithLength(int len) { if (len < maxElems) { readInputSimple(0, len); return checkPalindromSimple(len); } readInput(len); return checkPalindrom(len, true); } int main() { counts[0].resize(26); counts[1].resize(26); pairs[0].resize(26 * 26); pairs[1].resize(26 * 26); triples[0].resize(26 * 26 * 26); triples[1].resize(26 * 26 * 26); firstPos.resize(26, -1); lastPos.resize(26); int n; cin >> n; bool palindrom; if (n == 0) { palindrom = palindromWithoutLength(); } else { palindrom = palindromWithLength(n); } cout << (palindrom ? "TAK" : "NIE") << endl; } |