forked from Hawstein/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
20.8.cpp
60 lines (54 loc) · 1.12 KB
/
20.8.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
#include <iostream>
#include <cstring>
using namespace std;
class Trie{
public:
static const int MAX_N = 100 * 100;// 100为主串长度
static const int CLD_NUM = 26;
int size;
int trie[MAX_N][CLD_NUM];
Trie(const char* s);
void insert(const char* s);
bool find(const char* s);
};
Trie::Trie(const char* s){
memset(trie[0], -1, sizeof(trie[0]));
size = 1;
while(*s){
insert(s);
++s;
}
}
void Trie::insert(const char* s){
int p = 0;
while(*s){
int i = *s - 'a';
if(-1 == trie[p][i]){
memset(trie[size], -1, sizeof(trie[size]));
trie[p][i] = size++;
}
p = trie[p][i];
++s;
}
}
bool Trie::find(const char* s){
int p = 0;
while(*s){
int i = *s - 'a';
if(-1 == trie[p][i])
return false;
p = trie[p][i];
++s;
}
return true;
}
int main(){
Trie tree("mississippi");
string patt[] = {
"is", "sip", "hi", "sis", "mississippa"
};
int n = 5;
for(int i=0; i<n; ++i)
cout<<tree.find((char*)&patt[i][0])<<endl;
return 0;
}