#define ch s[i] - '0' structAC_automaton { int nxt[2], fail; bool ext; } T[maxn]; int top = 1, rt = 1; voidinsert(char *s){ int now = rt, l = strlen(s); for (int i = 0; i < l; ++i) { if (!T[now].nxt[ch]) T[now].nxt[ch] = ++top; now = T[now].nxt[ch]; } T[now].ext = 1; }
queue<int> Q; voidinit_fail(){ for (int i = 0; i < 2; ++i) { int &u = T[rt].nxt[i]; if (!u) { u = rt; continue; } T[u].fail = rt; Q.push(u); } while (!Q.empty()) { int u = Q.front(); Q.pop(); for (int i = 0; i < 2; ++i) { int &v = T[u].nxt[i]; if (!v) { v = T[T[u].fail].nxt[i]; continue; } T[v].fail = T[T[u].fail].nxt[i]; T[v].ext |= T[T[v].fail].ext; Q.push(v); } } }
bool ins[maxn], vis[maxn]; booldfs(int u){ ins[u] = vis[u] = 1; for (int i = 0; i < 2; ++i) { int v = T[u].nxt[i]; if (ins[v]) return1; if (T[v].ext || vis[v]) continue; if (dfs(v)) return1; } ins[u] = 0; return0; }
intmain(){ cin >> n; for (int i = 1; i <= n; ++i) scanf("%s", s), insert(s); init_fail(); if (dfs(rt)) puts("TAK"); elseputs("NIE"); return0; }