-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsubstring-diff.py
68 lines (65 loc) · 1.95 KB
/
substring-diff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import time
def main():
t = int(raw_input())
for c in range(t):
read = raw_input()
k, str1, str2 = read.split(' ')
k = int(k)
length = len(str1)
maxres = 0
for i in range(length):
if (length - i < maxres):
break
x = 0
diff = []
while (x + i < length):
while (str1[x] == str2[x + i]):
x += 1
if (x + i == length):
break
if (x + i == length):
break
diff.append(x)
x += 1
count = len(diff)
if (count <= k):
res = length - i
else:
res = diff[k];
diff.append(x);
for j in range(count - k):
if (diff[j + k + 1] - diff[j] - 1 > res):
res = diff[j + k + 1] - diff[j] - 1
if res > maxres:
maxres = res
if res == 751:
print diff
for i in range(length):
if (length - i < maxres):
break
x = 0
diff = []
while (x + i < length):
while (str2[x] == str1[x + i]):
x += 1
if (x + i == length):
break
if (x + i == length):
break
diff.append(x)
x += 1
count = len(diff)
if (count <= k):
res = length - i
else:
res = diff[k];
diff.append(x);
for j in range(count - k):
if (diff[j + k + 1] - diff[j] - 1 > res):
res = diff[j + k + 1] - diff[j] - 1
if res > maxres:
maxres = res
print maxres
if __name__=='__main__':
main()