#include<bits/stdc++.h> #define mp make_pair #define pb push_back using namespace std; typedef long long LL; typedef pair<int,int> PII; inline LL read() { LL x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} return x*f; } const int N=30008; int n; char s[N]; struct node{ int ch[2],fail; bool num; }t[N]; int sz; inline void inser() { int u=0,n=strlen(s+1),c; for(int i=1;i<=n;++i){ c=s[i]-'0'; if(!t[u].ch[c]){ t[u].ch[c]=++sz; } u=t[u].ch[c]; } t[u].num=1; } int q[N],he,ta; inline void getAC() { he=1;ta=1; for(int i=0;i<2;++i){ if(t[0].ch[i]){ q[ta]=t[0].ch[i]; ++ta; } else{ t[0].ch[i]=-1; } } int u,v,x; while(he!=ta){ u=q[he]; ++he; for(int i=0;i<2;++i){ v=t[u].ch[i]; if(!v){ t[u].ch[i]=t[t[u].fail].ch[i]; continue; } t[v].fail=t[t[u].fail].ch[i]; t[v].num|=t[t[t[u].fail].ch[i]].num; q[ta]=v; ++ta; } } } bool ins[N],vis[N]; inline bool dfs(int x) { ins[x]=1; vis[x]=1; for(int i=0;i<2;++i){ if(t[x].ch[i]==-1) continue; if(ins[t[x].ch[i]]) return 1; if(!t[t[x].ch[i]].num&&!vis[t[x].ch[i]]){ if(dfs(t[x].ch[i])) return 1; } } ins[x]=0; return 0; } int main() { n=read(); for(int i=1;i<=n;++i){ scanf("%s",s+1); inser(); } getAC(); if(dfs(0)){ puts("TAK"); } else{ puts("NIE"); } return 0; }
|
近期评论