gpt4 book ai didi

ruby - 算法:字符串相似度

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:47:50 24 4
gpt4 key购买 nike

<分区>

我正在尝试解决 InterviewStreet 上的这个挑战:https://www.interviewstreet.com/challenges/dashboard/#problem/4edb8abd7cacd

我已经有了一个可用的算法,但我想提高它的性能。您对如何操作有什么建议吗?

# 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

谢谢,格雷格

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com