【发布时间】:2013-03-27 18:24:48
【问题描述】:
我需要这个问题的帮助。我认为时间复杂度是 O(n),但我朋友坚持认为这是 O(n^2)。原因之一是因为fn = fn[index+1 ::]
#filename: string_expression_matcher.cc
#string: stxpm.c
#should return
import sys
string = "string_expression_matcher.cc"
subStr = "stxpm.c"
fn = list(string)
for i in subStr:
try:
index = fn.index(i)
fn = fn[index+1 ::]
except:
print ("can't dup")
sys.exit()
print ("found")
这是我的算法:
- 在 subStr 中:
- 循环开始于:“string_expression_matcher.cc”
- 此步骤剩余的字符串输出为:“tring_expression_matcher.cc”
- t in subStr
- 循环开始于:“tring_expression_matcher.cc”
- 剩下的是:“ring_expression_matcher.cc”
- x 在 subStr
- 循环开始于:“ring_expression_matcher.cc”
- 剩下的是:“pression_matcher.cc”
- p 在 subStr 中
- 循环开始于:“pression_matcher.cc”
- 剩下的是:“ression_matcher.cc”
以此类推到最后一步。
给定:
n = len(subStr)
m = len(string)`
这个程序的时间复杂度是多少? 谢谢大家,但我真的很想知道是 O(n) 还是 O(n^2)。我知道代码并不完美,但请关注时间复杂度.. 非常感谢
有谁知道 python 字符串复制是如何工作的?当我们执行 fn = fn[index+1 ::] 时会发生什么?
我问了一位杰出的工程师。他说结果是 O(m*n)。你呢?
【问题讨论】:
-
“如果我们在 m 或 n 中添加项目,时间复杂度是线性的......”到底是什么意思?
-
忘记了我的评论。我想知道这个程序的时间复杂度是多少。请帮忙...
标签: python string time match complexity-theory