/*
Copyright 2013 Marek "p2004a" Rusinowski
Dictionary of basic factors
*/
#include <cstdio>
#include <cstring>
#include <vector>
#include <utility>
#include <map>
using namespace std;
class DBF {
vector<vector<int> > t;
int max_len;
public:
explicit DBF(const char *str) {
int len = strlen(str);
t.push_back(vector<int>());
t[0].resize(len);
for (int i = 0; i < len; ++i) {
t[0][i] = str[i];
}
for (int k = 0, width = 1; width < len; ++k, width <<= 1) {
map<pair<int, int>, int> pairs;
for (int i = 0; i < len - width; ++i) {
pairs[make_pair(t[k][i], t[k][i + width])] = i;
}
if ((int) pairs.size() == len - width) {
max_len = (width << 1) - 1;
break;
}
t.push_back(vector<int>());
t[k + 1].resize(len - width);
for (int i = 0; i < len - width; ++i) {
t[k + 1][i] = pairs[make_pair(t[k][i], t[k][i + width])];
}
}
}
bool cmp(int beg1, int beg2, int len) {
if (beg1 == beg2) return true;
if (len > max_len) return false;
int i = 0;
while (1 << (i + 1) < len) ++i;
return t[i][beg1] == t[i][beg2]
&& t[i][beg1 + len - (1 << i)] == t[i][beg2 + len - (1 << i)];
}
};
int main() {
DBF dbf("abbaabbabababbaaaba");
printf("%s\n", dbf.cmp(3, 15, 3) ? "tak" : "nie");
printf("%s\n", dbf.cmp(3, 15, 4) ? "tak" : "nie");
return 0;
}
LyoKICBDb3B5cmlnaHQgMjAxMyBNYXJlayAicDIwMDRhIiBSdXNpbm93c2tpCiAgRGljdGlvbmFyeSBvZiBiYXNpYyBmYWN0b3JzCiovCiNpbmNsdWRlIDxjc3RkaW8+CiNpbmNsdWRlIDxjc3RyaW5nPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8dXRpbGl0eT4KI2luY2x1ZGUgPG1hcD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpjbGFzcyBEQkYgewogIHZlY3Rvcjx2ZWN0b3I8aW50PiA+IHQ7CiAgaW50IG1heF9sZW47CiBwdWJsaWM6CiAgCiAgZXhwbGljaXQgREJGKGNvbnN0IGNoYXIgKnN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