Posted at

オフラインリアルタイムどう書くE23の問題をRubyで解く

More than 1 year has passed since last update.

問題はこちら

http://nabetani.sakura.ne.jp/hena/orde23nokoch/

def solve(q)

n_s,ab = q.split(',')
n = n_s.to_i
cv = {'0a'=>'0+-0','+a'=>'+-0+','-a'=>'-0+-','0b'=>'0+0-0','+b'=>'+-+0+','-b'=>'-0-+-'}
lines = ab.split('').inject('0'){|r,c| r.split('').map{|e| cv[e+c]}.join}
lines.size < n ? 'x' : lines[n-1]
end

DATA.readlines.each do |line|
no,q,a = line.strip.split(/\s+/)
ans = solve(q)
print no + "\t" + ans
puts ans == a ? ' o' : ' x'
end
__END__
0 120,aabb 0
1 100,a x
2 3,a -
3 3,b 0
4 9,aa -
5 10,bb +
6 11,ab -
7 12,ba 0
8 7,aaa 0
9 17,baa +
10 28,bba -
11 82,bba +
12 35,baa -
13 254,babb +
14 462,abba x
15 226,bbba 0
16 345,bbba 0
17 256,aaaa 0
18 11,aaab -
19 241,abaab -
20 490,aabaa 0
21 1305,bbbaa 0
22 1102,ababa -
23 1077,abbab -
24 281,aabaa -
25 2218,abbaaa +
26 4095,bbabbb +
27 2750,abbaab +
28 5573,bbaaba +
29 6644,aaabba x
30 8109,bbbbba +
31 3860,aaaabbb +
32 59222,bbbbbba 0
33 14956,baabbab -
34 14894,ababbba +
35 3163,aaaaaab -
36 21917,babaaaa +
37 178620,aabbbaab x
38 96709,babbaaaa 0
39 74116,abababaa -
40 22025,abbbbabb 0
41 8612,aaaabbaa -
42 153868,bbbabbab -
43 747769,abbabaaba x
44 541589,baabbbbab -
45 787443,ababbbbab -
46 129549,ababaaaaa 0
47 837323,aabbbabab x
48 140592,bbbbabbab +
49 219669,ababbabbab -
50 500261,bbababaabb -
51 966503,aaabababbb 0
52 443603,baabaababb +
53 3912,aabbababaa 0
54 2926358,bbabbbbaba 0
55 18104279,bbbaababbab -
56 3849980,aaabaaaaaba 0
57 9276072,baabaabaaab 0
58 11202113,baaaaabbbba 0
59 5432578,abaabbaaaaa -
60 17363025,bbaabababbb 0
61 24147656,baabaabbbbab 0
62 1078733,bbbaaaabbbbb +
63 38623426,abaabababbaa -
64 19312285,bbaababbaaba +
65 11485959,baaaaababaaa -
66 36831104,babbbbbbabab +