ALDS1_3_D: Areas on the Cross-Section Diagram

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_D
水たまりの面積を求める問題。
単なるパズル。
Rubyはまだ不慣れなのでC++で書くより長くなったかも、、、

def f(str,base,add)
n=str.size
a=[base]
h=base
n.times do |i|
	case str[i]
	when '\\'
		h-=add
	when '/'
		h+=add
	end
	a.push(h)
end
return a
end


# your code goes here
str=STDIN.gets.chomp

a=f(str,0,1)
n=a.size
b=f(str.reverse,a[n-1],-1)


h1=a[0]
h2=b[0]
c=[]
n.times do |i|
h1=[a[i],h1].max
h2=[b[i],h2].max
c.push(h1)
b[i]=h2
end
b.reverse!
hit=false
ans=[]
sum=0
n.times do |i|
d=[c[i],b[i]].min-a[i]
if d>0
	sum=sum+d
else
	if sum>0
		ans.push(sum)
	end
	sum=0
end
end

if ans.size==0
puts 0
puts 0
else
sum=0
ans.size.times do |i|
	sum+=ans[i]
end
puts sum
print ans.size
ans.size.times do |i|
	print " ",ans[i]
end

puts ""
end
最終更新:2016年05月30日 08:56