import timeit
import matplotlib.pyplot as plt
# 第一種演算法時間複雜度達到o(n^2)
def anagramsolution1(s1, s2):
'''查詢兩個字串是否是相互亂序, 也就是兩個字串的字元排序不一樣
'''alist = list(s2)
pos1 = 0
stillok = true
while pos1 < len(s1) and stillok:
pos2 = 0
found = false
# 從乙個字元當中每次取乙個字元依次在另乙個字串中查詢
while pos2 < len(s2) and not found:
if s1[pos1] == alist[pos2]:
found = true
else:
pos2 = pos2 + 1
# 如果查詢到該字元在另乙個字串中存在,就將該位置 的字元值為零,
# 以免出現例如"accc" 和 "aacc"相匹配的情況
if found:
alist[pos2] = none
else:
stillok = false
pos1 = pos1 + 1
return stillok
# 第二種演算法是先把所有的需要匹配的字元創排好序, 那麼對應排好序的字串應該都是一模一樣的
# 演算法時間也達到幾乎o(n^2),乙個來自於排序演算法,乙個來自於比較演算法
def anagramsolution2(s1, s2):
alist1 = list(s1)
alist2 = list(s2)
alist1.sort()
alist2.sort()
pos = 0
flag = true
while pos < len(s1) and flag:
if alist1[pos] == alist2[pos]:
pos += 1
else:
flag = false
return flag
# 第三種演算法是假設所有的字串都是包含小寫的字元,那麼就對每乙個字元出現的次數計數
# 然後對應的字元計數相比較,演算法時間複雜度o(n)
def anagramsolution3(s1, s2):
c1 = [0]*26
c2 = [0]*26
for i in range(len(s1)):
# 取得asci瑪的數值
pos = ord(s1[i]) - ord('a')
c1[pos] = c1[pos] + 1
for i in range(len(s2)):
# 取得asci瑪的數值
pos = ord(s2[i]) - ord('a')
c2[pos] = c2[pos] + 1
j, flag = 0, true
while j < 26 and flag:
if c1[j] == c2[j]:
j += 1
else:
flag = false
return flag
def test1():
l =
for i in range(1000):
l = l + [i]
def test2():
l =
for i in range(1000):
def test3():
l = [i for i in range(1000)]
def test4():
l = list(range(1000))
if __name__ == '__main__':
print(anagramsolution1('abcd', 'dcba'))
print(anagramsolution2('abcd', 'dcba'))
print(anagramsolution3('abcd', 'dcba'))
print("-----------------------------")
t1 = timeit.timer("test1()", "from __main__ import test1")
t2 = timeit.timer("test2()", "from __main__ import test2")
t3 = timeit.timer("test3()", "from __main__ import test3")
t4 = timeit.timer("test4()", "from __main__ import test4")
print("concat ", t1.timeit(number=1000), "millseconds")
print("comprehension ", t3.timeit(number=1000), "millseconds")
print("list range ", t4.timeit(number=1000), "millseconds")
print('-------------------------------')
popzero = timeit.timer('x.pop(0) ', 'from __main__ import x')
popend = timeit.timer('x.pop() ', 'from __main__ import x')
'''x = list(range(90000000))
print('pop zero ', popzero.timeit(number=1000), 'millseconds')
x = list(range(90000000))
print('pop end ', popend.timeit(number=1000), 'millseconds')
'''for i in range(100000, 100000001, 100000):
x = list(range(i))
pt = popend.timeit(number=1000)
x = list(range(i))
pz = popzero.timeit(number=1000)
i = len(x)
plt.scatter(i, int(pt), 'o', title='pop end')
plt.scatter(i, int(pz), '-', title='pop zero')
plt.grid(true)
plt.legend(loc='upper left')
plt.show()
print('pt = ', pt)
print('pz = ', pz)
使用GPU進行字串匹配 cuda程式設計實現
cuda程式的字尾為.cu,編譯時使用nvcc,其使用方法與gcc相似。例如nvcc test.cu o test nvcc的官方文件 1。首先遇到錯誤 fatal error cutil.h no such file or directorykmp.cu 62 error identifier c...
使用QStringBuilder進行字串連線
in c performance qtqt labs chinese comments qstring和qbytearray提供了非常便利的operator 以允許你寫這樣的 qstring directory name qstring datafile directory qlatin1char ...
Python 字串匹配 match
import reprint re.match abc abc 匹配,左邊第乙個開始算起來,print re.match xabc abc 匹配不成功返回none,匹配成功返回位置詳細資訊print re.match abc xabc print re.match abc abcx import r...