求助
查看原帖
求助
160839
Prean楼主2021/4/7 21:41

RT,学AC机,样例总是输出TAK,对着题解看过好多遍了

有无dalao能帮忙看看/kel

#include<cstdio>
#include<cctype>
#include<queue>
const int M=30005;
int n,cnt,chi[M][2],fail[M];char s[M];bool iq[M],tag[M],vis[M];
void Insert(char*s){
	int now=0,i=0;
	while(isalpha(s[i])){
		if(chi[now][s[i]-48])chi[now][s[i]-48]=++cnt;
		now=chi[now][s[i]-48];s[i]=0;
	}
	tag[now]=true;
}
void Build(){
	register int i,u;
	std::queue<int>q;
	if(chi[0][0])q.push(chi[0][0]);
	if(chi[0][1])q.push(chi[0][1]);
	while(!q.empty()){
		u=q.front();q.pop();
		for(i=0;i<2;++i){
			if(!chi[u][i]){
				chi[u][i]=chi[fail[u]][i];continue;
			}
			if(tag[fail[chi[u][i]]=chi[fail[u]][i]])tag[chi[u][i]]=true;
			q.push(chi[u][i]);
		}
	}
}
bool DFS(int u){
	iq[u]=true;
	for(int i=0;i<2;++i){
		int v=chi[u][i];
		if(iq[v])return true;
		if(vis[v]||tag[v])continue;
		vis[v]=true;
		if(DFS(v))return true;
	}
	iq[u]=false;
	return false;
}
signed main(){
	scanf("%d",&n);
	while(n--)scanf("%s",s),Insert(s);
	Build();
	printf("%s",DFS(0)?"TAK":"NIE");
}
2021/4/7 21:41
加载中...