-
Notifications
You must be signed in to change notification settings - Fork 0
/
5670.py
62 lines (61 loc) · 1.02 KB
/
5670.py
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
import sys
def ordd(x):
return ord(x)-ord('a')
def ssum(x):
ans=0
for i in x:
if i==None:
continue
else:
ans+=1
return ans
class Trie:
def __init__(self,c):
self.c=c
self.nodelist=[None]*26
self.terminal=False
def insert(self, s):
cur=self
while True:
index=ordd(s[0])
if not cur.nodelist[index]:
cur.nodelist[index]=Trie(index)
if len(s)==1:
cur.nodelist[index].terminal=True
break
cur=cur.nodelist[index]
s=s[1:]
def find(self,s):
ans=0
cnt=0
cur=self
while cnt<len(s):
index=ordd(s[cnt])
if cnt==0:
cur=cur.nodelist[index]
cnt+=1
ans+=1
continue
if cur.terminal:
cur = cur.nodelist[index]
cnt += 1
ans += 1
continue
z= ssum(cur.nodelist)
cur=cur.nodelist[index]
cnt+=1
if z!=1:
ans+=1
return ans
def solution(n,l):
x=Trie("")
ans=0
for i in l:
x.insert(i)
for i in l:
ans+=x.find(i)
print("{:0.2f}".format(round(ans/len(l),2)))
for line in sys.stdin:
n=int(line)
l=[input() for _ in range(n)]
solution(n,l)