about summary refs log tree commit diff
path: root/others/dict/dict.py
diff options
context:
space:
mode:
Diffstat (limited to 'others/dict/dict.py')
-rwxr-xr-xothers/dict/dict.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/others/dict/dict.py b/others/dict/dict.py
new file mode 100755
index 0000000..59724cd
--- /dev/null
+++ b/others/dict/dict.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python3
+
+from bisect import bisect_left as bisect
+
+words = []
+
+
+with open('dict.inp') as fi, open('dict.out', 'w') as fo:
+    for _ in range(int(fi.readline())):
+        w = fi.readline().strip()
+        i = bisect(words, w)
+        if i == len(words) or w != words[i]:
+            words.insert(i, w)
+
+    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))