Poi2000 #7 viruses (automatic machine)

Source: Internet
Author: User

Description

Binary viruses Investigation
Committee detected, that certain sequences of zeroes and ones are codes
Of viruses. The committee isolated a set of all the virus codes.
Sequence of zeroes and ones is called safe, if any of its segments (I. e.
Sequence of consecutive elements) is not a virus code. The committee is
Aiming at finding, whether an infinite, safe sequence of zeroes and
Ones exists.
Example
For a set of codes {011, 11,000 00}, the sample infinite safe Sequence
Is 010101... for a set of codes {01, 11,000 00} an infinite safe
Sequence of zeroes and ones does not exist.

Task
Write a program, which:

Reads virus codes from the text file wir. In,
Determines, whether an infinite, safe sequence of zeroes and ones exists
Writes the result to the text file wir. Out.

Input

The first line of the Input
File wir. In consists of one integer n standing for the number of all
Virus codes. Each of the next n lines consists of one non-empty word
Composed from 0 s and 1 S-A virus code. The total length of all words
The does not exceed 30000.

Output

In the first and the only line of the output file wir. out one shocould find a word:

Tak-if an infinite, safe sequence of zeroes and ones exists.
Nie-otherwise.

Sample Input

Sample output




Source

Poi1999/2000

#include<iostream>
#include<cstring>
#include<stdio.h>

using namespace std;

struct Node {
int next[2];
int cnt;
int suffix;
};

Node ptr[30003];
int que[30003];
char s[30003];
int tot, N;
int ind[30003], totnode;

void trie() {
tot = 1;
ptr[0].next[0] = ptr[0].next[1] = -1, ptr[0].cnt = 0;
scanf("%d", &N);
while (N--) {
scanf("%s", s);
int Len = strlen(s);
int cur = 0;
for (int i = 0; i < Len; i++) {
if (ptr[cur].cnt) break;
int k = s[i] - '0';
if (ptr[cur].next[k] == -1) {
ptr[cur].next[k] = tot;
ptr[tot].next[0] = ptr[tot].next[1] = -1;
ptr[tot].cnt = 0;
tot++;
}
cur = ptr[cur].next[k];
}
ptr[cur].cnt = 1;
}
// cout << tot << endl;

}

void Bfs() {
int l = 0, r = -1;
totnode = 1;
ptr[0].suffix = 0;
for (int i = 0; i < tot; i++) {
ind[i] = 0;
}
for (int i = 0; i < 2; i++) {
if (ptr[0].next[i] == -1) {
ptr[0].next[i] = 0, ind[0]++;
} else {
r++, que[r] = ptr[0].next[i], ptr[que[r]].suffix = 0, ind[que[r]]++;
}
}

while (l <= r) {
int cur = que[l];
int suffix = ptr[cur].suffix;
if (ptr[suffix].cnt) ptr[cur].cnt = 1;
if (!ptr[cur].cnt) {
totnode++;
for (int i = 0; i < 2; i++) {
if (ptr[cur].next[i] == -1) {
ptr[cur].next[i] = ptr[suffix].next[i];
} else {
r++, que[r] = ptr[cur].next[i], ptr[que[r]].suffix = ptr[suffix].next[i];
}
ind[ptr[cur].next[i]]++;
}

}
l++;
}
// for (int i = 0; i < tot; i++) cout << i << " " << ptr[i].next[0] << " " << ptr[i].next[1] << endl;
// for (int i = 0; i < tot; i++) cout << i << " " << ind[i] << endl;


}

void Topsort() {
int l = 0, r = -1;
if (ind[0] > 0) {
printf("TAK\n");
return;
}
// cout << totnode << endl;

r++, que[r] = 0;
while (l <= r) {
int cur = que[l];
for (int i = 0; i < 2; i++) {
int son = ptr[cur].next[i];
if (ptr[son].cnt) continue;
ind[son]--;
if (ind[son] == 0) {
r++, que[r] = son;
}
}
l++;
}
if (r == totnode - 1) printf("NIE\n");
else printf("TAK\n");
}

int main() {

trie();
Bfs();
Topsort();
return 0;
}


NIE
301 11 00000


[Transfer]: http://hi.baidu.com/liveroom/blog/item/dcd20ff44f52f5d0f3d3850c.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.