-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathfirst.cpp
104 lines (95 loc) · 2.56 KB
/
first.cpp
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
#include <iostream>
#include <string>
#include <utility>
#include <sstream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <fstream>
using namespace std;
#define INF 1000000000
#define LL_INF 4500000000000000000
#define LSOne(S) (S & (-S))
#define EPS 1e-9
#define A first
#define B second
#define mp make_pair
#define PI acos(-1.0)
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
struct node {
node *children[26];
bool endPoint;
node() {
for (int i = 0; i < 26; i++) children[i] = nullptr;
endPoint = false;
}
void insert(const string &s, int idx) {
if (idx == s.length()) {
endPoint = true;
return;
}
char c = s[idx] - 'a';
if (children[c] == nullptr) children[c] = new node();
children[c]->insert(s, idx+1);
}
};
bool adjMatrix[26][26];
bool visited[26];
bool inStack[26];
bool isValid(int node) {
visited[node] = true;
inStack[node] = true;
for (int i = 0; i < 26; i++) {
if (!adjMatrix[node][i]) continue;
if (!visited[i] && !isValid(i)) return false;
if (visited[i] && inStack[i]) return false;
}
inStack[node] = false;
return true;
}
int main() {
freopen("first.in", "r", stdin);
freopen("first.out", "w", stdout);
int n;
cin >> n;
string strings[n]; for (int i = 0; i < n; i++) cin >> strings[i];
node trie;
for (int i = 0; i < n; i++) trie.insert(strings[i], 0);
vector<string> valid;
for (int i = 0; i < n; i++) {
memset(adjMatrix, false, sizeof adjMatrix);
node *curNode = ≜
int wordIdx = 0;
while (wordIdx < strings[i].length()) {
int charIdx = strings[i][wordIdx++] - 'a';
if (curNode->endPoint) goto endPoint;
for (int j = 0; j < 26; j++) {
if (j == charIdx) {
continue;
}
if (curNode->children[j] != nullptr) {
adjMatrix[charIdx][j] = true;
}
}
curNode = curNode->children[charIdx];
}
memset(visited, false, sizeof visited);
memset(inStack, false, sizeof inStack);
for (int j = 0; j < 26; j++) if (!visited[j] && !isValid(j)) goto endPoint;
valid.push_back(strings[i]);
endPoint:
continue;
}
cout << valid.size() << endl;
for (string s : valid) cout << s << endl;
return 0;
}