Created
July 26, 2010 14:13
-
-
Save astropanic/490592 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
#Time limit exceeded | |
class Palindrome | |
def compute(value) | |
@input_array = value | |
@size = @input_array.size | |
@even = @size % 2 == 0 | |
if @even | |
@pos1 = (@size/2)-1 | |
@pos2 = @pos1+1 | |
else | |
@pos1 = (((@size+1)/2)-1) | |
@pos2 = @pos1 | |
end | |
@mirrored = @input_array[0..@pos1] | |
@start = @even ? @pos1 : @pos2-1 | |
@start.downto(0) do |i| | |
@mirrored << @input_array[i] | |
end | |
if @mirrored.join <= value.join | |
nxt(@pos1, @pos2) | |
end | |
@mirrored.each do |c| | |
print c | |
end | |
end | |
def nxt(pos1, pos2) | |
@pos1 = @mirrored[pos1] | |
if pos1 < 0 | |
@mirrored[@size-1] = 1 | |
@mirrored.unshift(1) | |
else | |
if @pos1 < 9 | |
@mirrored[pos1] = @mirrored[pos2] = @pos1 + 1 | |
else | |
@mirrored[pos1] = @mirrored[pos2] = 0 | |
nxt(pos1-1, pos2+1) | |
end | |
end | |
end | |
end | |
t = gets.to_i | |
p = Palindrome.new | |
1.upto(t) do |z| | |
k = STDIN.gets.unpack("c*").map { |c| c-48} | |
k = k[0...-1] | |
p.compute(k) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment