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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//
//  main.cpp
//  pa_pal
//
//  Created by Michal Kowalski on 13/12/2018.
//  Copyright © 2018 Kowalski Apps. All rights reserved.
//

#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <functional>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <stdexcept>

using namespace std;

// A Huffman Tree Node
struct HuffmanTree {
    char c; // character in an alphabet
    int cfreq; // frequency of c.
    struct HuffmanTree *left;
    struct HuffmanTree *right;
    HuffmanTree(char c, int cfreq, struct HuffmanTree *left=NULL,
                struct HuffmanTree *right=NULL) :
    c(c), cfreq(cfreq), left(left), right(right) {
    }
    ~HuffmanTree() {
        delete left, delete right;
    }
    // Compare two tree nodes
    class Compare {
    public:
        bool operator()(HuffmanTree *a, HuffmanTree *b) {
            return a->cfreq > b->cfreq;
        }
    };
};

/**
 * Builds a Huffman Tree from an input of alphabet C, where C is a vector
 * of (character, frequency) pairs.
 */
HuffmanTree *build_tree(vector< pair<char, unsigned> > &alph) {
    // First build a min-heap
    // Build leaf nodes first
    priority_queue<HuffmanTree *, vector<HuffmanTree *>, HuffmanTree::Compare > alph_heap;
    for (vector< pair<char, unsigned> >::iterator it = alph.begin();
         it != alph.end(); ++it) {
        HuffmanTree *leaf = new HuffmanTree(it->first, it->second);
        alph_heap.push(leaf);
    }
    
    // HuffmanTree algorithm: Merge two lowest weight leaf nodes until
    // only one node is left (root).
    HuffmanTree *root = NULL;
    while (alph_heap.size() > 1) {
        HuffmanTree *l, *r;
        l = alph_heap.top();
        alph_heap.pop();
        r = alph_heap.top();
        alph_heap.pop();
        root = new HuffmanTree(0, l->cfreq + r->cfreq, l, r);
        alph_heap.push(root);
    }
    
    return root;
}

typedef vector<bool> code_t;
typedef map<char, code_t> codetable;
/**
 * Makes a lookup table (std::map) of (c -> code) from a HuffmanTree, where
 * code is an unsigned long representing the binary code.
 */
map<char, code_t> build_lookup_table(HuffmanTree *htree) {
    codetable lookup;
    deque< pair<HuffmanTree *, code_t> > q;
    
    q.push_back(make_pair(htree, code_t()));
    while (!q.empty()) {
        HuffmanTree *node, *lc, *rc;
        code_t code;
        node = q.front().first;
        code = q.front().second;
        q.pop_front();
        lc = node->left;
        rc = node->right;
        if (lc) {
            // HuffmanTree is always full (either no children or two children)
            // Left child is appended a 0 and right child a 1.
            code_t code_cp(code);
            q.push_back(make_pair(lc, (code.push_back(0), code)));
            q.push_back(make_pair(rc, (code_cp.push_back(1), code_cp)));
        } else {
            // Leaf node: contains the character
            lookup.insert(make_pair(node->c, code));
        }
    }
    
    return lookup;
}

/**
 * Encodes an input string. returns a byte vector.
 */
code_t encode(string input, codetable &lookup) {
    code_t result;
    
    for (string::iterator it = input.begin(); it != input.end(); ++it) {
        code_t b = lookup[*it];
        result.insert(result.end(), b.begin(), b.end());
    }
    
    return result;
}

/**
 * Look up the next valid code in @biter using @htree and returns the
 * resulting string. Note the iterator @biter is advanced by the actual
 * length of the next valid code, which varies.
 */
char code_lookup(code_t::iterator &biter, const code_t::iterator &biter_end,
                 const HuffmanTree *htree) {
    const HuffmanTree *node = htree;
    
    while (true) {
        if (!node->left) {
            // Huffman tree is full: always contains both children or none.
            break;
        }
        if (biter == biter_end) {
            throw std::out_of_range("No more bits");
        }
        if (*biter) {
            node = node->right;
        } else {
            node =node->left;
        }
        ++biter;
    }
    
    return node->c;
}

/**
 * Decodes a compressed string represented by a bit vector (vector<char>)
 * @compressed, using a HuffmanTree @htree.
 * Returns the original string.
 */
string decode(code_t &compressed, const HuffmanTree *htree) {
    string result;
    
    code_t::iterator biter = compressed.begin();
    while (true) {
        try {
            result += code_lookup(biter, compressed.end(), htree);
        } catch (const std::out_of_range &oor) {
            // Iterator exhausted.
            break;
        }
    }
    
    return result;
}

vector< pair<char, unsigned> > make_freq_table(string inp) {
    map<char, unsigned> cfmap;
    vector< pair<char, unsigned> >cfvec;
    
    for (unsigned i = 0; i < inp.size(); i++) {
        if (cfmap.find(inp[i]) == cfmap.end()) {
            cfmap.insert(make_pair(inp[i], 1));
        }
        cfmap[inp[i]] += 1;
    }
    
    for (map<char, unsigned>::iterator it = cfmap.begin();
         it != cfmap.end(); ++it) {
        cfvec.push_back(make_pair(it->first, it->second));
    }
    
    return cfvec;
}

string bitvec_to_string(code_t &bitvec) {
    string result;
    size_t nbits;
    
    nbits = bitvec.size() & 7;
    
    // Write the number of "hanging bits" at the first byte
    result += static_cast<char>(nbits); // at most 7
    
    char byte = 0;
    for (unsigned i = 0; i < bitvec.size(); i++) {
        unsigned boff = i & 7;
        byte |= bitvec[i] << boff;
        if (boff == 7) {
            // Write a byte
            result += byte;
            byte = 0;
        }
    }
    if (nbits) {
        result += byte;
    }
    
    return result;
}

code_t string_to_bitvec(string packed) {
    code_t result;
    
    if (packed.size() == 1) {
        return result;
    }
    unsigned nbits = packed[0];
    for (string::iterator it = packed.begin() + 1; it != packed.end(); ++it) {
        for (unsigned i = 0; i < 8; i++) {
            result.push_back((*it >> i) & 1);
        }
    }
    // fix the last byte
    if (nbits) {
        for (unsigned i = 0; i < (8 - nbits); i++) {
            result.pop_back();
        }
    }
    
    return result;
}

/////
/////
#define MAX_WORD_SIZE 2000000
#define HASH_BUCKET_SIZE 1000

bool isPalindrome(int N, char * P) {
    for (int i = 0; i<(N/2);++i) {
        if (P[i] != P[N-1-i]) return false;
    }
    return true;
}

bool solve1(int N) {
    char P[MAX_WORD_SIZE+1];
    scanf("%s",P);
    return isPalindrome(N, P);
}

bool solveWithHashing(int N) {
    size_t BH[200005];
    char BUCKET[HASH_BUCKET_SIZE+1];
    int restSize = N % HASH_BUCKET_SIZE;
    int nBuckets = N / HASH_BUCKET_SIZE;
    if (nBuckets % 2 == 1) {
        nBuckets--;
        restSize += HASH_BUCKET_SIZE;
    }
    nBuckets = nBuckets/2;
    for (int l = 0 ;l < nBuckets; ++l) {
        for (int cb=0;cb < HASH_BUCKET_SIZE; ++cb) {
            scanf(" %c",&BUCKET[cb]);
        }
        BUCKET[HASH_BUCKET_SIZE]=0;
        string strBucket = string(BUCKET);
        BH[l] = hash<string>{}(strBucket);
    }
    ///mid
    for (int cb=0;cb < restSize; ++cb) {
        scanf("%c",&BUCKET[cb]);
    }
    BUCKET[restSize] = 0;
    if (!isPalindrome(restSize, BUCKET)) {
        return false;
    }
    //after mid
    for (int l = nBuckets-1 ;l >= 0; --l) {
        for (int cb=HASH_BUCKET_SIZE-1;cb >= 0; --cb) {
            scanf("%c",&BUCKET[cb]);
        }
        BUCKET[HASH_BUCKET_SIZE]=0;
        string strBucket = string(BUCKET);
        size_t thash = hash<string>{}(strBucket);
        if (BH[l] != thash)
            return false;
    }
    return true;
}

#define COMP_CHUNK 50000

bool readAll() {
    vector<string> PACKED;
    vector<HuffmanTree *> TREES;
    
    char W[COMP_CHUNK+1];
    int N = 0;
    int wi = 0;
    int c = getchar();
    while ((c = getchar()) && c!=-1) {
        W[wi] = (char)c;
        ++wi;
        ++N;
        if (wi == COMP_CHUNK) {
            string input = string(W)+"-";
            vector< pair<char, unsigned> > cfvec = make_freq_table(input);
            HuffmanTree *htree = build_tree(cfvec);
            codetable ctbl = build_lookup_table(htree);
            code_t t = encode(input, ctbl);
            string packed = bitvec_to_string(t);
            PACKED.push_back(packed);
            TREES.push_back(htree);
            wi=0;
        }
    }
    W[wi] = (char)0;
    string str = string(W)+"-";
    if (wi > 0 && str.length() > 0) {
        vector< pair<char, unsigned> > cfvec = make_freq_table(str);
        HuffmanTree *htree = build_tree(cfvec);
        codetable ctbl = build_lookup_table(htree);
        code_t t = encode(str, ctbl);
        string packed = bitvec_to_string(t);
        PACKED.push_back(packed);
        TREES.push_back(htree);
    }
    int LI = 0;
    int RI = N-1;
    string LS = "";
    string RS = "";
    int li = -1;
    int ri = -1;
    int SLI = 0;
    int SRI = PACKED.size() - 1;
    while(LI < RI) {
        if (li == -1) {
            code_t t1 = string_to_bitvec(PACKED[SLI]);
            LS = decode(t1, TREES[SLI]);
            li = 0;
            SLI++;
        }
        
        if (ri == -1) {
            code_t t1 = string_to_bitvec(PACKED[SRI]);
            RS = decode(t1, TREES[SRI]);
            ri = RS.size() - 2;
            SRI--;
        }

        if (LS[li] != RS[ri])
            return false;
        
        LI++;
        RI--;
        li++;
        ri--;
        if (li == LS.size()-1)
            li = -1;
    }
    return true;
}

int N;
int main(int argc, const char * argv[]) {
    scanf("%d",&N);
    bool pal = false;
    if (N > 0) {
        if (N > MAX_WORD_SIZE) {
            pal = solveWithHashing(N);
        } else {
            //pal = solveWithHashing(N);
            pal = solve1(N);
        }
    } else { //when zero
        pal = readAll();
    }
    printf("%s\n",pal?"TAK":"NIE");
    return 0;
}