-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuilding_A_String.py
56 lines (38 loc) · 1 KB
/
Building_A_String.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
#!/bin/python3
import os
import sys
import re
#
# Complete the buildString function below.
#
def buildString(a, b, s):
N = len(s)
cost = [0]*N
cost[0] = a
index = 1
for i in range(1,N):
if s[index:i+1] not in s[:index] or index == -1:
index = findLongestOccurence(s[:i+1], i)
if index==-1:
cost[i] = cost[i-1] + a
else:
cost[i] = min(cost[i-1]+a, cost[index-1]+b)
return cost[-1]
def findLongestOccurence(s, index):
indexHasChanged = False
while s[index:] in s[:index]:
index -= 1
indexHasChanged = True
return -1 if not indexHasChanged else index + 1
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
t = int(input())
for t_itr in range(t):
nab = input().split()
n = int(nab[0])
a = int(nab[1])
b = int(nab[2])
s = input()
result = buildString(a, b, s)
fptr.write(str(result) + '\n')
fptr.close()