Snippets
Created by
cia_rana
last modified
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | require "matrix"
class Matrix
# This monkey-patching is for Ruby 2.1.
# by https://github.com/ruby/ruby/blob/ruby_2_4/lib/matrix.rb#L337
def Matrix.hstack(x, *matrices)
raise TypeError, "Expected a Matrix, got a #{x.class}" unless x.is_a?(Matrix)
result = x.send(:rows).map(&:dup)
total_column_count = x.column_count
matrices.each do |m|
raise TypeError, "Expected a Matrix, got a #{m.class}" unless m.is_a?(Matrix)
if m.row_count != x.row_count
raise "The given matrices must have #{x.row_count} rows, but one has #{m.row_count}"
end
result.each_with_index do |row, i|
row.concat m.send(:rows)[i]
end
total_column_count += m.column_count
end
new result, total_column_count
end
# This monkey-patching is for Ruby 2.1.
# by https://github.com/ruby/ruby/blob/ruby_2_4/lib/matrix.rb#L316
def Matrix.vstack(x, *matrices)
raise TypeError, "Expected a Matrix, got a #{x.class}" unless x.is_a?(Matrix)
result = x.send(:rows).map(&:dup)
matrices.each do |m|
raise TypeError, "Expected a Matrix, got a #{m.class}" unless m.is_a?(Matrix)
if m.column_count != x.column_count
raise "The given matrices must have #{x.column_count} columns, but one has #{m.column_count}"
end
result.concat(m.send(:rows))
end
new result, x.column_count
end
def flipud
Matrix[*self.to_a.reverse]
end
def fliplr
Matrix[*self.to_a.map(&:reverse)]
end
end
def convertSeqToMV(seq)
seq.select{|e|e<1}.map{|e|e<0??m:?v}*""
end
def convertMatToMV(m)
if m.column_size == 1
mv = convertSeqToMV(m.column(0).to_a)
mv + mv.reverse
elsif m.row_size == 1
mv = convertSeqToMV(m.row(0).to_a)
mv + mv.reverse
else
convertSeqToMV(
m.row(0).to_a \
+ m.column(-1).to_a \
+ m.row(-1).to_a.reverse \
+ m.column(0).to_a.reverse
)
end
end
# -1: 折り目の山部分, 0: 折り目の谷部分, 1: 折り目のない面
# f(x) = ~xとすると、f(-1) = 0, f(0) = -1, f(f(x)) = x より
m = gets.reverse.chars.inject(nil){|mat, c|
if mat.nil?
case c
when ?L, ?R
Matrix[[1, 0, 1]]
when ?T, ?B
Matrix[[1, 0, 1]].t
end
else
case c
when ?L
Matrix.hstack(
mat.fliplr.map{|e|e>0?e:~e},
Matrix.build(mat.row_size, 1){0},
mat
)
when ?R
Matrix.hstack(
mat,
Matrix.build(mat.row_size, 1){0},
mat.fliplr.map{|e|e>0?e:~e}
)
when ?T
Matrix.vstack(
mat.flipud.map{|e|e>0?e:~e},
Matrix.build(1, mat.column_size){0},
mat
)
when ?B
Matrix.vstack(
mat,
Matrix.build(1, mat.column_size){0},
mat.flipud.map{|e|e>0?e:~e}
)
end
end
}
puts convertMatToMV(m)
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.