[POI2000]病毒

题面

传送门

Sol

建出AC自动机后DFS能走的点,如果能走回来就可行

# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(3e5 + 5);

int ch[2][_], tot, fail[_], n, vis[_], in[_];
bool cnt[_];
char w[_];
queue <int> Q;

IL void Insert(){
    RG int l = strlen(w), x = 0;
    for(RG int i = 0; i < l; ++i){
        RG int c = w[i] - '0';
        if(!ch[c][x]) ch[c][x] = ++tot;
        x = ch[c][x];
    }
    cnt[x] = 1;
}

IL void GetFail(){
    for(RG int i = 0; i < 2; ++i) if(ch[i][0]) Q.push(ch[i][0]);
    while(!Q.empty()){
        RG int fa = Q.front(); Q.pop();
        for(RG int i = 0; i < 2; ++i)
            if(ch[i][fa]) fail[ch[i][fa]] = ch[i][fail[fa]], Q.push(ch[i][fa]);
            else ch[i][fa] = ch[i][fail[fa]];
		cnt[fa] |= cnt[fail[fa]];
    }
}

IL void Dfs(RG int x){
	in[x] = vis[x] = 1;
	for(RG int i = 0; i < 2; ++i){
		if(in[ch[i][x]]){  puts("TAK"); exit(0);  }
		if(vis[ch[i][x]] || cnt[ch[i][x]]) continue;
		Dfs(ch[i][x]);
	}
	in[x] = 0;
}

int main(RG int argc, RG char* argv[]){
    scanf("%d", &n);
    for(RG int i = 1; i <= n; ++i) scanf(" %s", w), Insert();
    GetFail();
	Dfs(0);
	puts("NIE");
    return 0;
}

原文地址:https://www.cnblogs.com/cjoieryl/p/8331492.html