about summary refs log tree commit diff
path: root/others/other/dict.py
blob: 76b841437d3b172fd73ecd00b1790ebcf41b9e25 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/env python3

from itertools import islice
from bisect import bisect_left as bisect


with open('dict.inp') as fi, open('dict.out', 'w') as fo:
    words = list(islice(fi, int(fi.readline())))
    words.sort()

    for _ in range(int(fi.readline())):
        s = fi.readline().strip()
        i = bisect(words, s)
        count = 0
        while i + count < len(words) and words[i + count].startswith(s):
            count += 1
        fo.write("{}\n".format(count))