-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontacts.py.txt
50 lines (40 loc) · 1.07 KB
/
contacts.py.txt
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
class contacts:
def __init__(self):
self.T = Trie()
def add(self, s):
a = self.T
for c in s:
na = a.addchar(c)
a = na
a.words += 1
def find(self, s):
count = 0
a = self.T
for c in s:
if a.find(c):
na = a.find(c)
a = na
else:
return 0
return a.countchild()
class Trie:
def __init__(self):
self.a = {}
self.words = 0
def addchar(self, c):
if c not in self.a:
self.a[c] = Trie()
return self.a[c]
def find(self, c):
if c in self.a:
return self.a[c]
def countchild(self):
return self.words
n = int(input().strip())
c = contacts()
for i in range(n):
s = input().split(' ')
if s[0].strip() == 'add':
c.add(s[1].strip())
if s[0].strip() == 'find':
print(c.find(s[1].strip()))