Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Created April 22, 2017 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whatalnk/f89974b683570f394ca500b3a932a6b1 to your computer and use it in GitHub Desktop.
Save whatalnk/f89974b683570f394ca500b3a932a6b1 to your computer and use it in GitHub Desktop.
Codeforces #410 Div2
s = gets.chomp
n = s.length
ret = 0
if n == 1 then
puts "YES"
exit
elsif n.odd? then
((n - 1) / 2).times do |i|
if s[i] != s[n - 1 - i] then
ret += 1
end
end
if ret <= 1 then
puts "YES"
else
puts "NO"
end
else
(n / 2).times do |i|
if s[i] != s[n - 1 - i] then
ret += 1
end
end
if ret == 1 then
puts "YES"
else
puts "NO"
end
end
# WA
n = gets.chomp.to_i
a = []
h = Hash.new{|hash, key| hash[key] = Array.new()}
n.times do
s = gets.chomp
a << s
end
nn = a[0].length
a.each do |s|
nn.times do |i|
ss = s[i...nn] + s[0...i]
h[ss] << i
end
end
if h.size != nn then
puts -1
else
puts h.values.map{|v| v.inject(:+)}.min
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment