-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTrie.cpp
120 lines (107 loc) · 2.34 KB
/
Trie.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <string.h>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <assert.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <sstream>
using namespace std;
#define FOR( i, L, U ) for(int i=(int)L ; i<=(int)U ; i++ )
#define FORD( i, U, L ) for(int i=(int)U ; i>=(int)L ; i-- )
#define SQR(x) ((x)*(x))
#define INF INT_MAX
#define READ(filename) freopen(filename, "r", stdin);
#define WRITE(filename) freopen(filename, "w", stdout);
#define NDEBUG
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<vector<int> > vvi;
typedef vector<vector<int> > vvc;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<int, string> mis;
typedef map<string, string> mss;
typedef map<string, char> msc;
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define N 500
#define NOLINK -2
#define R 26
int at,root,ans;
struct Trie{
int words;
int prefixes;
int link[R];
};
//vector<Trie>t;
Trie t [N];
void clear_trie_node(int n){
t[n].words = t[n].prefixes = 0;
FOR(i,0,R-1)t[n].link[i] = NOLINK;
}
int getNode(){
clear_trie_node(at);
return at++;
}
string asciToString(char ch){
stringstream ss;
string s;
ss<<ch;
ss>>s;
return s;
}
void addWord(const string& key){
int cur = root;
int lb;
FOR(i,0,key.length()-1){
lb = key[i]-'a';
if(t[cur].link[lb]==NOLINK){
t[cur].link[lb] = getNode();
}
cur = t[cur].link[lb];
t[cur].prefixes++;
}
t[cur].words++;
}
void traverse(int u,string key){
if(t[u].words)cout<<key<<endl;
FOR(i,0,R-1){
if(t[u].link[i]!=NOLINK)
traverse(t[u].link[i], key+asciToString(i+'a'));
}
}
int main()
{
//Attention this program does not work if NOEDGE is assigned 4,5,6 or 7 ... why describe
//READ("input.txt");
//WRITE("output.txt");
int words;
string key;
// t.resize(N);
while(scanf("%d",&words)){
at = 0;
root = getNode();
FOR(i,1,words){
cin>>key;
addWord(key);
}
traverse(root,"");
}
return 0;
}