Python/strings/

Python/strings/

1、levenshtein_distance.py

second_word=“fjkdjfng”
for j, c2 in enumerate(second_word):
print (j,"-",c2)

0 - f
1 - j
2 - k
3 - d
4 - j
5 - f
6 - n
7 - g

c1=‘a’
c2=‘b’
substitutions = 5 + (c1 != c2)
substitutions
6

(c1 != c2)
True

4+(c1 != c2)
5

min(5, 2, 17)
2

current_row = [5]
current_row
[5]

current_row.append(min(5, 2, 17))
current_row
[5, 2]

2、knuth_morris_pratt.py
######continue 语句用来告诉Python跳过当前循环的剩余语句,然后继续进行下一轮循环。

continue语句用在while和for循环中。

def get_failure_array(pattern):
failure = [0]
i=0
j=1
while j<len(pattern):
if pattern[i]==pattern[j]:
i+=1
elif i>0:
i=failure[i-1]
continue######这里看出continue的作用是继续进行elif循环,直到i>0的条件不再满足
j+=1
failure.append(i)
return failure

pattern = “AAAB”
text = “ABAAAAAB”
get_failure_array(pattern)
[0, 1, 2, 0]

failure = [0]
i=0
j=1
len(pattern)
4

pattern[0]
‘A’

pattern[1]
‘A’

i+=1
i
1

j+=1
j
2

failure.append(1)
failure
[0, 1]

pattern[1]
‘A’

pattern[2]
‘A’

i+=1
i
2

j+=1
j
3

failure.append(i)
failure
[0, 1, 2]

pattern[i]
‘A’

pattern[j]
‘B’

i
2

i=failure[i-1]
i
1

j+=1
j
4

failure.append(i)
failure
[0, 1, 2, 1]

i=failure[i-1]
i
0

failure.pop(-1)
1

failure
[0, 1, 2]

failure.append(i)######这里看出continue的作用是继续进行elif循环
failure
[0, 1, 2, 0]

3、manacher.py

n = input()
XYG

n[0]
‘X’

n[1]
‘Y’

n[2]
‘G’

n[:2]
‘XY’

n[:3]
‘XYG’

for i in range(5):
print(i)

0
1
2
3
4

def palindromic_length( center, diff, string):
if center-diff == -1 or center+diff == len(string) or string[center-diff] != string[center+diff] :
return 0
return 1 + palindromic_length(center, diff+1, string)

new_input_string
‘X|Y|G’

palindromic_length(2, 1, new_input_string)
1

4、min_cost_string_conversion.py

m
6

n
10

[0 for _ in range(n+1)]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

[[0 for _ in range(n+1)] for _ in range(m+1)]
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]##重复7次,是lambda函数

ops
[[0, ‘IA’, ‘Il’, ‘Ig’, ‘Io’, ‘Ir’, ‘Ii’, ‘It’, ‘Ih’, ‘Im’, ‘Is’], [‘DP’, ‘RPA’, ‘RPl’, ‘RPg’, ‘RPo’, ‘RPr’, ‘RPi’, ‘RPt’, ‘RPh’, ‘RPm’, ‘RPs’], [‘Dy’, ‘RyA’, ‘Ryl’, ‘Ryg’, ‘Ryo’, ‘Ryr’, ‘Ryi’, ‘Ryt’, ‘Ryh’, ‘Rym’, ‘Rys’], [‘Dt’, ‘RtA’, ‘Rtl’, ‘Rtg’, ‘Rto’, ‘Rtr’, ‘Rti’, ‘Ct’, ‘Ih’, ‘Im’, ‘Is’], [‘Dh’, ‘RhA’, ‘Rhl’, ‘Rhg’, ‘Rho’, ‘Rhr’, ‘Rhi’, ‘Rht’, ‘Ch’, ‘Im’, ‘Is’], [‘Do’, ‘RoA’, ‘Rol’, ‘Rog’, ‘Co’, ‘Ror’, ‘Roi’, ‘Rot’, ‘Do’, ‘Rom’, ‘Ros’], [‘Dn’, ‘RnA’, ‘Rnl’, ‘Rng’, ‘Dn’, ‘Rnr’, ‘Rni’, ‘Rnt’, ‘Rnh’, ‘Rnm’, ‘Rns’]]

i,j
(6, 10)

ops[i][j]
‘Rns’

ops[i][j][0]
‘R’

cost
10

str(cost)
‘10’

5、naive_String_Search.py

for i in range(5):
match_found=True
for j in range(3):
if 4!=9:
match_found=False
print(match_found)
break
if match_found:
print (“yes”)

False
False
False
False
False

6、rabin_karp.py

猜你喜欢

转载自blog.csdn.net/qq_39306047/article/details/94545680