Created
September 24, 2012 10:02
-
-
Save zeptometer/3775256 to your computer and use it in GitHub Desktop.
AOJ 1001
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node | |
attr_reader :left, :right | |
def initialize(left,right) | |
@left = left | |
@right = right | |
end | |
def to_s | |
"(#{@left||""},#{@right||""})" | |
end | |
end | |
class Parser | |
def initialize(str) | |
@str = str | |
@idx = 0 | |
end | |
def read | |
if @str[@idx] == ?( | |
@idx += 1 | |
l = read | |
@idx += 1 | |
r = read | |
@idx += 1 | |
Node.new(l,r) | |
else | |
nil | |
end | |
end | |
end | |
def its (a,b) | |
if a && b | |
Node.new(its(a.left,b.left),its(a.right,b.right)) | |
else | |
nil | |
end | |
end | |
def uni (a,b) | |
if !a | |
b | |
elsif !b | |
a | |
else | |
Node.new(uni(a.left,b.left),uni(a.right,b.right)) | |
end | |
end | |
while line = gets | |
ch, a, b = line.split | |
c = Parser.new(a).read | |
d = Parser.new(b).read | |
if ch=="i" | |
puts its(c,d) | |
elsif ch=="u" | |
puts uni(c,d) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment