2023 HUAWEI OD machine test real question Python implementation [find the length of the path passing through a specific point]

Table of contents 

topic

train of thought

Code


topic

Enter a string, which is composed of uppercase letters, and the distance between each adjacent is 1. Enter a string in the second line, indicating the point that must be passed. Explain that each point can be passed multiple times. What is the minimum distance to solve these must-pass points?

Example 1  The input and output examples are only for debugging, and the background judgment data generally does not include examples

enter

ANTSEDXQOKPUVGIFWHJLYMCRZB

ABC

output

28

train of thought

1: The first step is to count the index position of the input mandatory uppercase letters in the first line of strings. Then use DFS to solve all possible paths that pass through the points, and calculate the distance of these paths, that is, calculate the absolute value of the difference between two adjacent points, and then add them. Finally, sort all path distances and find the smallest distance.

Code

 

Guess you like

Origin blog.csdn.net/misayaaaaa/article/details/131724187