亚洲精品不卡AV在线播放|国产性爱无码高清视频|国产成人 ,97人人色,国产免费一区二区三区,日本一区二区在线

內(nèi)容中心

返回列表
2026年質(zhì)量好的冷凍薯條/法式冷凍薯條年度熱度排名
2026-02-11 09:40:44

To solve this problem, we need to determine the minimum number of operations required to make a given number (represented as a string) divisible by 25. The key insight here is that any number divisible by 25 must end in one of the following pairs: "00", "25", "50", or "75". Additionally, the number zero itself is divisible by 25, so we need to handle this case separately.

Approach

  1. Check Valid Endings: For each valid ending pair ("00", "25", "50", "75"), find the longest subsequence of the input string that ends with this pair. This is done by first finding the rightmost occurrence of the second digit in the pair, then the rightmost occurrence of the first digit before the second digit.
  2. Handle Zero: If the input string contains at least one '0', then zero is a valid number (divisible by 25), so we consider this case.
  3. Calculate Minimum Operations: The minimum number of operations is the length of the input string minus the length of the longest valid subsequence (either from the valid endings or the zero case). If no valid subsequence exists, return -1.

Solution Code

def minimal_operations(s):
    n = len(s)
    max_len = 0
    targets = ["00", "25", "50", "75"]

    for t in targets:
        first, second = t[0], t[1]
        # Find the rightmost occurrence of the second character in the target pair
        idx_second = s.rfind(second)
        if idx_second == -1:
            continue
        # Find the rightmost occurrence of the first character before idx_second
        idx_first = s.rfind(first, 0, idx_second)
        if idx_first == -1:
            continue
        # Length of the subsequence is (digits up to idx_first) + 1 (second character)
        current_length = idx_first + 2
        if current_length > max_len:
            max_len = current_length

    # Check if there is any zero (valid as a single digit)
    if '0' in s:
        max_len = max(max_len, 1)

    if max_len == 0:
        return -1
    return n - max_len

Explanation

  1. Valid Endings: For each target pair, we find the rightmost occurrence of the second digit and then the rightmost occurrence of the first digit before it. This gives us the longest possible subsequence ending with the pair.
  2. Zero Handling: If the input string contains a '0', we can always form the number zero (which is divisible by 25) by removing all other digits.
  3. Result Calculation: The minimum operations are the number of digits we need to remove to get the longest valid subsequence. If no valid subsequence exists, return -1.

This approach efficiently finds the solution by leveraging the properties of numbers divisible by 25 and ensures we handle all edge cases (like zero) correctly. The time complexity is O(n) for each target pair, which is O(4n) = O(n) overall, making it optimal for large input sizes.

Answer: The code provided above is the solution to the problem. For a given input string s, it returns the minimal number of operations to make the number divisible by 25, or -1 if it's impossible. The final answer is the function minimal_operations as written.



(免責(zé)聲明:本文為本網(wǎng)站出于傳播商業(yè)信息之目的進(jìn)行轉(zhuǎn)載發(fā)布,不代表本網(wǎng)站的觀點及立場。本文所涉文、圖、音視頻等資料的一切權(quán)利和法律責(zé)任歸材料提供方所有和承擔(dān)。本網(wǎng)站對此資訊文字、圖片等所有信息的真實性不作任何保證或承諾,亦不構(gòu)成任何購買、投資等建議,據(jù)此操作者風(fēng)險自擔(dān)。) 本文為轉(zhuǎn)載內(nèi)容,授權(quán)事宜請聯(lián)系原著作權(quán)人,如有侵權(quán),請聯(lián)系本網(wǎng)進(jìn)行刪除。

在線客服

在線留言
您好,很高興為您服務(wù),可以留下您的電話或微信嗎?