惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
J
Java Code Geeks
小众软件
小众软件
Jina AI
Jina AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hugging Face - Blog
Hugging Face - Blog
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
C
Check Point Blog
人人都是产品经理
人人都是产品经理
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - Franky
Apple Machine Learning Research
Apple Machine Learning Research
G
Google Developers Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The GitHub Blog
The GitHub Blog
腾讯CDC
T
The Blog of Author Tim Ferriss
大猫的无限游戏
大猫的无限游戏
量子位
M
MIT News - Artificial intelligence
Last Week in AI
Last Week in AI
L
LangChain Blog

Shiroha白羽的博客

Golang 踩坑 —— interface 为参数的时候传 nil 指针 Codeforces Round 925 (Div. 3) Codeforces Round 924 (Div. 2) Codeforces Round 923 (Div. 3) Codeforces Round 922 (Div. 2) Codeforces Round 921 (Div. 2) Educational Codeforces Round 161 (Rated for Div. 2) Codeforces Round 920 (Div. 3) Codeforces Round 919 (Div. 2) Hello 2024 Good Bye 2023 Codeforces Round 918 (Div. 4) 个人备份的常用 macOS 清理命令 Codeforces Round 917 (Div. 2) Pinely Round 3 (Div. 1 + Div. 2) Educational Codeforces Round 160 (Rated for Div. 2) Codeforces Round 915 (Div. 2) Codeforces Round 914 (Div. 2) Codeforces Round 913 (Div. 3) Educational Codeforces Round 159 (Rated for Div. 2) Codeforces Round 912 (Div. 2) Codeforces Round 911 (Div. 2) CodeTON Round 7 (Div. 1 + Div. 2, Rated, Prizes!) Educational Codeforces Round 158 (Rated for Div. 2) Codeforces Round 910 (Div. 2) Codeforces Round 909 (Div. 3) Codeforces Round 908 (Div. 2) Educational Codeforces Round 157 (Rated for Div. 2) C++自定义的字面量 Codeforces Round 907 (Div. 2)
【洛谷】P2444 [POI2000]病毒——AC自动机
Shiroha · 2020-01-05 · via Shiroha白羽的博客

题目链接

题目描述

二进制病毒审查委员会最近发现了如下的规律:某些确定的二进制串是病毒的代码。如果某段代码中不存在任何一段病毒代码,那么我们就称这段代码是安全的。现在委员会已经找出了所有的病毒代码段,试问,是否存在一个无限长的安全的二进制代码。

示例:

例如如果{011, 11, 00000}为病毒代码段,那么一个可能的无限长安全代码就是010101…。如果{01, 11, 000000}为病毒代码段,那么就不存在一个无限长的安全代码。

任务:

请写一个程序:

1.在文本文件WIR.IN中读入病毒代码;

2.判断是否存在一个无限长的安全代码;

3.将结果输出到文件WIR.OUT中。

输入格式

在文本文件WIR.IN的第一行包括一个整数n(n\le 2000)(n≤2000),表示病毒代码段的数目。以下的n行每一行都包括一个非空的01字符串——就是一个病毒代码段。所有病毒代码段的总长度不超过30000。

输出格式

在文本文件WIR.OUT的第一行输出一个单词:

TAK——假如存在这样的代码;

NIE——如果不存在。

输入输出样例

输入

3
01
11
00000

输出

NIE

分析

想办法让那串无限长的字符串不断的在树上失配,然后不断的走fail指针最后进入一个循环即可

即在树上dfs,保证不经过任何字符串尾节点使得找到一个树上环

AC code

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <bits/stdc++.h>

using namespace std;

/*
* AC 自动机:多个模式串去匹配一个串,求有多少个模式串与主串有匹配内容
*
* 使用操作:
* 1、把每一个模式串插入到树中 insert
* 2、build
* 3、使用 query 询问有多少个模式串匹配
*/

const int CHAR_NUM = 2;//仅小写
const int MAXN = 2100;//模式串个数
const int MAXM = 30100;//模式串最长长度
const int NUM = MAXN * MAXM;//空间=个数*长度,稳

struct Trie {
int c[NUM][CHAR_NUM], val[NUM], fail[NUM], cnt;

void insert(char *s) {
int len = strlen(s);
int now = 0;
for (int i = 0; i < len; i++) {
// int v = s[i] - 'a';
int v = s[i] - '0';
if (!c[now][v])c[now][v] = ++cnt;
now = c[now][v];
}
val[now]++;
}

void build() {
queue<int> q;
for (int i = 0; i < CHAR_NUM; i++)if (c[0][i])fail[c[0][i]] = 0, q.push(c[0][i]);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = 0; i < CHAR_NUM; i++)
if (c[u][i])fail[c[u][i]] = c[fail[u]][i], q.push(c[u][i]);
else c[u][i] = c[fail[u]][i];
}
}

int query(char *s) {
int len = strlen(s);
int now = 0, ans = 0;
for (int i = 0; i < len; i++) {
// now = c[now][s[i] - 'a'];
now = c[now][s[i] - '0'];
for (int t = now; t && ~val[t]; t = fail[t])ans += val[t], val[t] = -1;
}
return ans;
}
} AC;

char s[30100];
bool vis[NUM];

bool dfs(int cur) {
for (int i = 0; i < CHAR_NUM; ++i) {
int x = AC.c[cur][i];
if (!AC.val[x]) {
bool flag = false;
int t = x;
while (t) {
if (AC.val[t]) {
flag = true;
break;
}
t = AC.fail[t];
}
if (flag)
continue;
if (vis[x])
return true;
vis[x] = true;
if (dfs(x)) {
return true;
}
vis[x] = false;
}
}
return false;
}

void solve() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> s;
AC.insert(s);
}
AC.build();
vis[0] = true;
bool flag = dfs(0);
if (flag) {
cout << "TAK" << endl;
} else {
cout << "NIE" << endl;
}
}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifdef ACM_LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long long test_index_for_debug = 1;
char acm_local_for_debug;
while (cin >> acm_local_for_debug) {
cin.putback(acm_local_for_debug);
if (test_index_for_debug > 20) {
throw runtime_error("Check the stdin!!!");
}
auto start_clock_for_debug = clock();
solve();
auto end_clock_for_debug = clock();
cout << "Test " << test_index_for_debug << " successful" << endl;
cerr << "Test " << test_index_for_debug++ << " Run Time: "
<< double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
cout << "--------------------------------------------------" << endl;
}
#else
solve();
#endif
return 0;
}