forked from c910335/LCS-Problem-in-Linear-Space
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjudge.cr
43 lines (39 loc) · 901 Bytes
/
judge.cr
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
class String
def subsequence_of?(text)
return true if empty?
i = 0
text.each_char do |char|
if char == self[i]
i += 1
break if i == size
end
end
i == size
end
end
def lcs_size(xs, ys)
c = Array(Array(Int32)).new(xs.size + 1) { [0] * (ys.size + 1) }
xs.each_char_with_index do |x, i|
ys.each_char_with_index do |y, j|
c[i + 1][j + 1] = {x == y ? (c[i][j] + 1) : 0, c[i + 1][j], c[i][j + 1]}.max
end
end
c[xs.size][ys.size]
end
exit if ARGV.size != 2
test_data, output = ARGV.map { |file| File.open file }
pass = true
t = test_data.gets.to_s.to_i
t.times do |i|
xs, ys = test_data.gets.to_s.chomp.split
lcs = output.gets.to_s
if lcs_size(xs, ys) != lcs.size || !(lcs.subsequence_of?(xs) && lcs.subsequence_of?(ys))
puts "Failed at case #{i + 1}"
pass = false
end
end
if pass
puts "Passed"
else
exit 1
end