Skip to content

Instantly share code, notes, and snippets.

@bshyong
Created January 9, 2011 06:55
Show Gist options
  • Save bshyong/771499 to your computer and use it in GitHub Desktop.
Save bshyong/771499 to your computer and use it in GitHub Desktop.
my solution to facebook's hacker cup 'studious student' problem
# (c) 2010 Benjamin Shyong
#
# Studious Student
# You've been given a list of words to study and memorize. Being a diligent student of language and the arts, you've decided to not study them at all and instead make up pointless games based on them. One game you've come up with is to see how you can concatenate the words to generate the lexicographically lowest possible string.
# Input
# As input for playing this game you will receive a text file containing an integer N, the number of word sets you need to play your game against. This will be followed by N word sets, each starting with an integer M, the number of words in the set, followed by M words. All tokens in the input will be separated by some whitespace and, aside from N and M, will consist entirely of lowercase letters.
# Output
# Your submission should contain the lexicographically shortest strings for each corresponding word set, one per line and in order.
# Constraints
# 1 <= N <= 100
# 1 <= M <= 9
# 1 <= all word lengths <= 10
# Example input
# 5
# 6 facebook hacker cup for studious students
# 5 k duz q rc lvraw
# 5 mybea zdr yubx xe dyroiy
# 5 jibw ji jp bw jibw
# 5 uiuy hopji li j dcyi
# Example output
# cupfacebookforhackerstudentsstudious
# duzklvrawqrc
# dyroiymybeaxeyubxzdr
# bwjibwjibwjijp
# dcyihopjijliuiuy
output = File.new("studious_student_output.txt", "a+")
File.open("input.txt").each do |l|
if l.length == 1
next
end
temp = l.split( / */ ).drop(1)
arr = Array.new
l.split( / */ )[0].to_i.times do
x=temp[0]
for i in (0..(temp.length-2))
if ((x<=>temp[i+1])==1)
x=temp[i+1]
end
end
arr << x
temp.delete(x)
end
arr.each do |a|
unless a.nil?
a = a.chomp
end
output.print(a)
end
output.puts(" ")
arr.clear
end
output.close
@compcankit
Copy link

Look at this great blog explaining the solutions of qualification round of Hacker Cup.
Double Squares: http://itbhu.ac.in/codefest/blog/?p=159
Peg Game: http://itbhu.ac.in/codefest/blog/?p=172
Studious Student : http://itbhu.ac.in/codefest/blog/?p=180
Hope you will love this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment