P2444: [POI2000] Virus — Solution
Main Idea
This problem is definitely going to be implemented with an Aho–Corasick automaton. After we put all these strings into the automaton with the insert operation, how do we modify the build_AC_automation operation to handle this problem?
First, we notice that the fail pointers turn the whole trie into a graph (every node gets an extra virtual edge). So we find that as long as we can find a cycle in this graph such that none of the nodes on the cycle is the end node of some unsafe code, we can conclude that an infinitely long safe code exists. The reason is very simple to explain: if there is such a safe cycle, then the infinitely long code must loop around this cycle forever, and since the cycle is safe, this long string of code is guaranteed to be safe too.
See the code for details.
Code
// P2444.cpp
#include <iostream>
#include <queue>
#include <cmath>
#include <cstring>
#include <cstdio>
using namespace std;
const int MX_N = 30020;
struct node
{
node *nxt[2], *fail;
bool sum, ins, vis;
node() { nxt[0] = nxt[1] = fail = NULL, sum = 0, ins = vis = false; }
};
node *root;
int n, longest;
void insert(string str)
{
int siz = str.length();
node *p = root;
for (int i = 0; i < siz; i++)
p = (p->nxt[str[i] - '0'] == NULL) ? (p->nxt[str[i] - '0'] = new node()) : p->nxt[str[i] - '0'];
p->sum = true;
}
void build_ac_automation()
{
queue<node *> q;
q.push(root);
while (!q.empty())
{
node *curt = q.front();
q.pop();
for (int i = 0; i < 2; i++)
if (curt->nxt[i] != NULL)
{
if (curt == root)
curt->nxt[i]->fail = root;
else
{
node *p = curt->fail;
while (p)
if (p->nxt[i] != NULL)
{
curt->nxt[i]->fail = p->nxt[i];
curt->nxt[i]->sum |= p->nxt[i]->sum;
break;
}
else
p = p->fail;
if (p == NULL)
curt->nxt[i]->fail = root;
}
q.push(curt->nxt[i]);
}
else if (curt->fail != NULL)
curt->nxt[i] = curt->fail->nxt[i];
}
}
bool dfs(node *curt)
{
if (curt->ins)
return true;
if (curt->vis || curt->sum > 0)
return false;
curt->vis = curt->ins = true;
for (int i = 0; i < 2; i++)
if (curt->nxt[i] != NULL && dfs(curt->nxt[i]))
return true;
curt->ins = false;
return false;
}
char buff[30020];
int main()
{
scanf("%d", &n);
root = new node();
for (int i = 1; i <= n; i++)
scanf("%s", buff), insert(buff);
build_ac_automation();
if (dfs(root))
printf("TAK");
else
printf("NIE");
return 0;
}