-
Notifications
You must be signed in to change notification settings - Fork 0
/
dvap.py
138 lines (112 loc) · 3.2 KB
/
dvap.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import sys
from bisect import bisect_right
class TrieNode:
# Trie node class
def __init__(self):
self.children = [None] * 26
self.wordIndices = []
# isEndOfWord is True if node represent the end of the word
self.isEndOfWord = False
class Trie:
# Trie data structure class
def __init__(self):
self.root = self.getNode()
def getNode(self):
# Returns new trie node (initialized to NULLs)
return TrieNode()
def _charToIndex(self, ch):
# private helper function
# Converts key current character into index
# use only 'a' through 'z' and lower case
return ord(ch) - ord('a')
def insert(self, key, wordIndex):
# If not present, inserts key into trie
# If the key is prefix of trie node,
# just marks leaf node
pCrawl = self.root
length = len(key)
for level in range(length):
index = self._charToIndex(key[level])
# if current character is not present
if not pCrawl.children[index]:
pCrawl.children[index] = self.getNode()
pCrawl = pCrawl.children[index]
pCrawl.wordIndices.append(wordIndex)
# mark last node as leaf
pCrawl.isEndOfWord = wordIndex
def search(self, key):
# Search key in the trie
# Returns true if key presents
# in trie, else false
pCrawl = self.root
length = len(key)
for level in range(length):
index = self._charToIndex(key[level])
if not pCrawl.children[index]:
return False
pCrawl = pCrawl.children[index]
return pCrawl.isEndOfWord
def cost(self, key, wordIndex=40000):
cost = wordIndex + 1
pCrawl = self.root
length = len(key)
for level in range(length):
index = self._charToIndex(key[level])
if not pCrawl.children[index]:
return cost
pCrawl = pCrawl.children[index]
cost += bisect_right(pCrawl.wordIndices, wordIndex)
return cost
# wordlist = {}
# T = Trie()
# n = int(input())
# for i in range(n):
# s = input()
# wordlist[s] = i
# T.insert(s, i)
#
# Q = int(input())
# for _ in range(Q):
# query = input()
# if query in wordlist:
# print(T.cost(query, wordlist[query]))
# else:
# print(T.cost(query, n - 1))
# for line in sys.stdin:
while True:
sol = ''
try:
line = input()
except EOFError:
break
case = input()
l = 0
le = len(line)
while True:
t = case.find(line, l)
# print(t)
if t == -1:
break
else:
l = t + 1
print(t, end=' ')
if l >= len(case):
break
print('')
# for line in sys.stdin:
# sol = ''
# # line = input()
# case = input()
# l = 0
# le = len(line)
# while True:
# t = case.find(line, l)
# # print(t)
# if t == -1:
# break
# else:
# l = t + le
# sol += ' ' + str(t)
# if l >= len(case):
# break
# print(sol[1:]) if len(sol) > 1 else print(sol)