Created
August 11, 2014 21:55
-
-
Save brandonc/8e817b66efc6baef1841 to your computer and use it in GitHub Desktop.
This file contains 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
class String | |
def self.similarity(a, b) | |
if a == b | |
return a.length | |
end | |
result = 0 | |
while a[result] == b[result] | |
result += 1 | |
end | |
result | |
end | |
def each_suffix | |
self.length.downto(1) do |i| | |
yield self[self.length-i..self.length] | |
end | |
end | |
end | |
lines = ARGF.readlines | |
test_cases = lines.shift.to_i | |
test_cases.times do |index| | |
input = lines.shift | |
aggregate = 0 | |
input.each_suffix do |suffix| | |
aggregate += String.similarity(input.chomp, suffix.chomp) | |
end | |
puts aggregate | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment