Codeforces Round #156 (Div. 2)

http://www.codeforces.com/contest/255

A,B問題だけ。

A. Greg's Workout

n = gets.chomp.to_i
a = gets.chomp.split(" ").map{|n| n.to_i}

chest = 0
biceps = 0
back = 0

0.step(n, 3) do |idx|
  chest += a[idx] if not a[idx].nil?
  biceps += a[idx + 1] if not a[idx + 1].nil?
  back += a[idx + 2] if not a[idx + 2].nil?
end

if chest > biceps and chest > back
  puts "chest"
elsif biceps > chest and biceps > back
  puts "biceps"
else
  puts "back"
end

B. Code Parsing

説明されているアルゴリズムをそのまま実装してしまうと、時間的に無理っぽ。

アルゴリズムを単純化し、xとyの個数を数えて、多い方の文字 * (多い方の文字の個数 - 少ない方の文字の個数)を出力すればいい。

s = gets.chomp

x = s.count('x')
y = s.count('y')

if x > y
  puts "x" * (x - y)
else
  puts "y" * (y - x)
end