-
Notifications
You must be signed in to change notification settings - Fork 0
/
save.rb
100 lines (97 loc) · 1.68 KB
/
save.rb
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
def kmp_search(s, w)
m = i = 0
slen = s.length
wlen = w.length
found = [0]*slen
t = kmp_table(w)
puts " #{t}"
while m+i < slen
#puts " #{i} #{w[i]}"
#puts " #{m+i} #{s[m+i]}"
#found[m+i-t[i]] = [i-t[i],m+i-t[i]].max
if w[i] == s[m+i]
i += 1
found[m] = [i,found[m]].max
if i == wlen
puts " #{i} #{m}"
m += i - t[i]
i = [0, t[i]].max
found[m] = [i,found[m]].max
puts " #{i} #{m}"
end
else
#puts "#{t} #{i}"
m += i - t[i]
i = [0, t[i]].max
#found[m] = [i,found[m]].max
end
end
found[0..slen-1]
end
def kmp_table(w)
pos = 2
cnd = 0
t = [-1, 0]
wlen = w.length
while pos <= wlen
if w[pos-1] == w[cnd]
cnd += 1
t[pos] = cnd
pos += 1
elsif cnd > 0
cnd = t[cnd]
else
t[pos] = 0
pos += 1
end
end
t.push cnd
t
end
def kmp(t,p)
n = t.length-1
m = p.length-1
pi = pref(p)
ret = [0]*(n+1)
q = -1
for i in 0..n-1
while q>-1 and p[q+1] != t[i]
q=pi[q]
end
if p[q+1] = t[i]
q+=1
end
ret[i-q] = q
q = pi[q]
end
ret
end
def pref(p)
m = p.length
pi = [-1]
k = -1
for q in 1..m-1
while k > -1 and p[k+1] != p[q]
k = pi[k]
end
k += 1 if p[k+1] == p[q]
pi[q] = k
end
return pi
end
n = gets.to_i
n.times do
sa = gets.strip
sb = gets.strip
pre = kmp_search(sa,sb)
pre2 = kmp_search(sa.reverse, sb.reverse).reverse
puts "#{pre}"
puts "#{pre2}"
a = sa.length
b = sb.length
for i in (0..(a-b))
print "#{i} " if pre[i] == b || (pre[i] or 0)+(pre2[i+b-1] or 0) == b-1
end
puts
gets
end