-
Notifications
You must be signed in to change notification settings - Fork 1
/
b.py
67 lines (55 loc) · 2.16 KB
/
b.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
def calculate_total_distance(good_string, name):
# ASCII range is from 32 to 126
ASCII_MIN = 32
ASCII_MAX = 126
# Initialize a list to store the nearest good letter for each ASCII value
closest_good = [-1] * (ASCII_MAX + 1)
# Create a sorted list of ASCII values for the characters in the good string
good_chars = sorted(ord(ch) for ch in good_string)
# Precompute the closest good character for each possible ASCII value
for i in range(ASCII_MIN, ASCII_MAX + 1):
# Binary search to find the closest good character
pos = binary_search_closest(good_chars, i)
closest_good[i] = good_chars[pos]
total_distance = 0
prev_good_char = ord(good_string[0]) # Start with the first character of the good string
# Now, calculate the total distance for the name
for ch in name:
if ord(ch) >= ASCII_MIN and ord(ch) <= ASCII_MAX:
current_char = ord(ch)
if current_char in good_chars:
# If the current character is already a good character, no need to change it
continue
else:
# Get the nearest good character from the precomputed list
nearest_good_char = closest_good[current_char]
total_distance += abs(prev_good_char - nearest_good_char)
prev_good_char = nearest_good_char
return total_distance
def binary_search_closest(arr, target):
"""
Perform binary search to find the closest element to the target in the sorted array arr.
"""
low, high = 0, len(arr) - 1
if target <= arr[low]:
return low
if target >= arr[high]:
return high
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
# Check which of arr[low] or arr[high] is closer to the target
if abs(arr[low] - target) < abs(arr[high] - target):
return low
else:
return high
# Example Usage
good_string = input().strip()
name = input().strip()
result = calculate_total_distance(good_string, name)
print(result)