Created
April 13, 2012 06:01
-
-
Save Eric-Bro/2374228 to your computer and use it in GitHub Desktop.
Problem PA-3
This file contains 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
substr = $stdin.readline | |
main = $stdin.readline | |
mlen = main.length-1 | |
slen = substr.length-1 | |
glob_count = 0 | |
results = Array.new() | |
for i in 0...(mlen - slen+1) do | |
tmp_count = 0 | |
field = 0 | |
if (main[i] != substr[0]) then | |
next if ((main[i+1] != substr[1]) && slen !=1) | |
field = 1 | |
end | |
# first char is already OK | |
tmp_count += 1 | |
for k in 1..slen-1 do | |
if (main[i+k] == substr[k]) then | |
tmp_count += 1 | |
else | |
break if (field != 0) | |
field = 1 | |
tmp_count += 1 | |
end | |
end #for k in 0..slen-1 | |
if (tmp_count == slen) then | |
results.push(i+1) | |
glob_count += 1 | |
end | |
end # for i in 0..(mlen - slen + 1) | |
puts glob_count.to_s + "\n" | |
results.each{|x| print x.to_s + " "} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment