Last active
January 29, 2022 05:51
-
-
Save zunda/17e687f99feff1d484ff9ec2f8432253 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/ruby | |
# | |
# Usage: | |
# $ echo わたしたわしわたしたわ | ruby koikekeiko.rb | |
# 11/1 | |
# $ echo こしたんたん | ruby koikekeiko.rb | |
# 3/2 | |
# | |
# Calculates the "saiwaisa" of the input as defined in | |
# 2022年度 大学入学共通テスト 本試験 情報関係基礎 | |
# | |
# Copytight 2022 by zunda <zundan at gmail.com> | |
# | |
# Permission is granted for use, copying, modification, distribution, | |
# and distribution of modified versions of this work as long as the | |
# above copyright notice is included. | |
class KoikeKeiko < String | |
def splits(n) | |
r = [] | |
KoikeKeiko.each_split_ary(n, self.length, []) do |ary| | |
i = 0 | |
x = [] | |
ary.each do |d| | |
x << KoikeKeiko.new(self[i...(i+d)]) | |
i += d | |
end | |
r << x | |
end | |
return r | |
end | |
def kaibun? | |
self.reverse == self | |
end | |
def min_kaibun_split | |
1.upto(self.length) do |s| | |
if self.splits(s).map{|split| not split.find{|sub| not sub.kaibun?}}.find{|e| e} | |
return s | |
end | |
end | |
end | |
def saiwaisa | |
Rational(self.length, min_kaibun_split) | |
end | |
private | |
def KoikeKeiko.each_split_ary(n, l, lefts, &block) | |
if n <= 1 | |
yield [*lefts, l] | |
else | |
1.upto(l+1-n) do |x| | |
KoikeKeiko.each_split_ary(n-1, l-x, lefts + [x], &block) | |
end | |
end | |
end | |
end | |
puts KoikeKeiko.new(ARGF.read.strip).saiwaisa |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment