Skip to content

Instantly share code, notes, and snippets.

@brandonc
Created August 11, 2014 21:55
Show Gist options
  • Save brandonc/8e817b66efc6baef1841 to your computer and use it in GitHub Desktop.
Save brandonc/8e817b66efc6baef1841 to your computer and use it in GitHub Desktop.
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