题目
是否能构造出无限长的且不存在模式串的文本串
题解
AC自动机建好后,发现文本串就是在Tire图上一直乱跑,我们需要找到一个环,使它经过根节点,且环中没有终止节点
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+10;
int t[N][3],all;
int f[N];bool val[N];
void putin(char *s)
{
int p=0;
for(int i=0;s[i];i++)
{
int now=s[i]-'0';
if(!t[p][now])t[p][now]=++all;
p=t[p][now];
}val[p]=1;
}
void get_fail()
{
queue<int>q;
int p=0;
for(int i=0;i<=1;i++)
if(t[p][i])q.push(t[p][i]);
else q.push(t[p][i]=++all);
while(!q.empty())
{
int u=q.front();q.pop();
for(int i=0;i<=1;i++)
{
p=t[u][i];
if(!p)
{
t[u][i]=t[f[u]][i];
continue;
}
q.push(p);
int pre=f[u];
while(pre&&!t[pre][i])pre=f[pre];
f[p]=t[pre][i];
}
val[u]|=val[f[u]];
}
}
int n;
char s[N];
bool vis[N],ex[N];
void dfs(int u)
{
if(vis[u]){printf("TAK\n");exit(0);}
if(val[u]||ex[u])return ;
vis[u]=ex[u]=1;
for(int i=0;i<=1;i++)
if(t[u][i])dfs(t[u][i]);
vis[u]=0;
}
int main()
{
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
putin(s);
}
get_fail();
dfs(0);
printf("NIE");
}
Hack:
2
1
11
请自行思考如何解(ka)决(guo)