luogu2444 [poi2000]病毒

1
2
trie图上面跑环
先因为trie树多加continue跪了,然后发现没有把fail指向危险节点的点标为危险的(后缀中有危险的,说明自身也是危险的)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#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;
}
}
int u,v;
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(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;
}

错误代码:将没有的设为-1
反例:
1
0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#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;
}