forked from Kans29/Programming-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemacs.py
57 lines (54 loc) · 1.04 KB
/
emacs.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
"""
Algoritmo Knuth-Morris-Pratt para busqueda de una cadena adentro de otra
complejidad temporal/espacial = O(n+m) / O(n)
"""
from sys import stdin
def arrayBuild(P):
N = len(P)
A = [0 for x in range(N)]
i,j = 1,0
while i < N:
if P[i] == P[j] or P[i] == '*' or P[j] == '*':
j=j+1
A[i] = j
i+=1
else:
if j != 0:
j = A[j-1]
else:
A[i] = j
i+=1
if N >= 2:
if P[N-2] == '*': A[N-1] = N-1
return A
def patternSearch(A,B):
M = len(A)
B = B.strip('*')
N = len(B)
array = arrayBuild(B)
i,j,ans = 0,0, "yes"
while j < N and i < M:
if A[i] == B[j]:
i,j=i+1,j+1
else:
if B[j] == '*':
j+=1
elif j != 0 and B[j-1] != '*':
j = array[j-1]
else:
i +=1
if j != N:
ans = "no"
return ans
def main():
inp = stdin
N = inp.readline().strip()
while len(N)>0:
cases = int(N)
word = inp.readline().strip()
while cases > 0:
search = inp.readline().strip()
print(patternSearch(word,search))
cases -=1
N = inp.readline().strip()
main()