Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Enter your code here. Read input from STDIN. Print output to STDOUT
- N = gets.to_i
- words = []
- while words.length < N do
- words << gets.sub(/\n$/, '').strip
- end
- words.each do |word|
- count = 0
- (word.length).times do |i|
- sub = word[i..-1]
- j=0
- while j < sub.length && sub[j] == word[j] do
- count += 1
- j+=1
- end
- end
- puts count
- end
- def zarray(str):
- Z = [0]*len(str)
- Z[0] = len(str)
- left, right, i = 0, 0, 1
- while i < len(str):
- if i > right:
- j, k = 0, i
- while k < len(str) and str[j] == str[k]:
- j += 1
- k += 1
- Z[i] = j
- if j > 0:
- left, right = i, i+j-1
- else:
- z = Z[i-left]
- s = right-i+1
- if z < s:
- Z[i] = z
- else:
- j, k = s, s+i
- while k < len(str) and str[j] == str[k]:
- j += 1
- k += 1
- Z[i] = j
- left, right = i, i+j-1
- i += 1
- return Z
- def similarity(s):
- return sum(zarray(s))
- S[i + Z[i-left]] = S[i-left + Z[i-left]] ≠ S[Z[i-left]]
- S[i + k] = S[i-left + k] = S[k] for 0 <= k < Z[i-left]
Add Comment
Please, Sign In to add comment