Skip to content

Instantly share code, notes, and snippets.

@dashohoxha
Created May 12, 2013 14:35
Show Gist options
  • Save dashohoxha/5563781 to your computer and use it in GitHub Desktop.
Save dashohoxha/5563781 to your computer and use it in GitHub Desktop.
# Problem A. Consonants, of Round 1C 2013, of CodeJam
# https://code.google.com/codejam/contest/2437488/dashboard#s=p0
$vowels = ['a', 'e', 'i', 'o', 'u']
def vowel?(c)
$vowels.include?(c)
end
def consonant?(c)
not vowel?(c)
end
def nvalue(name, n)
# find segments of consecutive consonants
# with length >= n
name1 = name + 'a'
con_segments = []
start_pos = ( vowel?(name1[0]) ? nil : 0 )
for i in 1...name1.length
if consonant?(name1[i-1]) and vowel?(name1[i])
if not start_pos.nil?
if i - start_pos >= n
con_segments << { s: start_pos, e: i-1 }
end
end
start_pos = nil
elsif vowel?(name1[i-1]) and consonant?(name1[i])
start_pos = i
end
end
# find all the valid substrings
substrings = {}
con_segments.each do |seg|
start_pos = seg[:s]
end_pos = seg[:e]
for k in 0..(end_pos - start_pos - n + 1)
for i in 0..(start_pos + k)
for j in (start_pos + k + n - 1)...name.length
substr = "#{i},#{j}"
substrings[substr] = true
end
end
end
end
return substrings.length
end
T = gets.to_i
for t in 1..T
name, n = gets.chomp.split
n = n.to_i
nv = nvalue(name, n)
puts "Case ##{t}: #{nv}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment