例如,我有列表:
a[0] = [1, 1, 1, 0, 0]
a[1] = [1, 1, 0, 0, 1]
a[2] = [0, 1, 1, 1, 0]
# and so on
Run Code Online (Sandbox Code Playgroud)
它们似乎是不同的,但如果假设开始和结束是连接的,那么它们是圆形相同的.
问题是,我拥有的每个列表的长度为55,并且只包含三个和52个零.没有循环条件,有26,235(55选3)列表.但是,如果条件"循环"存在,则存在大量循环相同的列表
目前我通过以下方式检查循环身份:
def is_dup(a, b):
for i in range(len(a)):
if a == list(numpy.roll(b, i)): # shift b circularly by i
return True
return False
Run Code Online (Sandbox Code Playgroud)
在最坏的情况下,该功能需要55次循环移位操作.并且有26,235个列表可以相互比较.简而言之,我需要55*26,235*(26,235 - 1)/ 2 = 18,926,847,225次计算.它差不多是20吉加!
有没有什么好方法可以用更少的计算来做到这一点?或者支持循环的任何数据类型?
我正在考虑in运营商如何实施
>>> s1 = 'abcdef'
>>> s2 = 'bcd'
>>> s2 in s1
True
Run Code Online (Sandbox Code Playgroud)
在CPython中,哪个算法用于实现字符串匹配,以及时间复杂度是多少?有关于此的官方文件或维基吗?
str.replacePython中函数的大符号是什么?
总是O(n)吗?
str = "this is string example"
print str.replace("is", "was")
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)thwas was string example
亲爱的 StackOverflowers,
我是算法的新手,我正在为以下问题苦苦挣扎:如果我有 2 个字符串s1和s2,其中s2是s1. 它需要找到从s1到的最小可能循环偏移s2。
让我举个例子:
s1 = 'I love cookies '
s2 = 'cookies I love '
答案在这里7。
最好采用线性时间。有我失败的试验:
def find_minimum_cyclic_shift(s1, s2):
if len(s1) != len(s2):
return -1
index = s2.index(s1[0])
if (index > -1):
if (s1==s2):
return 0;
#finalPosition = len(s2) - index
#print(finalPosition, " index=",index)
#return s2[0] == s1[finalPosition] and s1[finalPosition::]==s2[0:index]
return index
Run Code Online (Sandbox Code Playgroud)
但它不适用于以下情况:absabsabsf和absfabsabs。而不是 4 我有 0. 因为 …
我正在寻找一种有效的方法来检查短字符串是否在长字符串中。我在这个线程上看到了一些建议: Python effective way to check if very large string contains a substring
但是,我在那里没有看到 find() 的用法。使用 find() 函数是否昂贵?时间复杂度是多少?
我查看了 Wiki 页面,但没有找到 find() 。 https://wiki.python.org/moin/TimeComplexity