/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 33ms 17.387 MiB
#2 Accepted 105ms 21.191 MiB
#3 Accepted 90ms 18.855 MiB
#4 Accepted 72ms 18.406 MiB
#5 Accepted 94ms 19.41 MiB
#6 Accepted 90ms 24.281 MiB
#7 Accepted 94ms 24.328 MiB
#8 Accepted 113ms 24.242 MiB
#9 Accepted 105ms 24.438 MiB
#10 Accepted 107ms 24.242 MiB
#11 Accepted 106ms 24.422 MiB
#12 Accepted 116ms 24.336 MiB
#13 Accepted 104ms 24.434 MiB
#14 Accepted 104ms 24.285 MiB
#15 Accepted 109ms 24.242 MiB
#16 Accepted 107ms 24.242 MiB
#17 Accepted 108ms 24.273 MiB
#18 Accepted 113ms 24.398 MiB
#19 Accepted 102ms 24.277 MiB
#20 Accepted 110ms 24.242 MiB

Code

import sys

def solve():
    """
    Solves a single test case for the alphabetical substring problem.
    """
    try:
        # Read N (length of string) and K (max replacements)
        line1 = sys.stdin.readline()
        if not line1: return
        n, k = map(int, line1.split())
        
        # Read the string S
        s = sys.stdin.readline().strip()
    except (IOError, ValueError):
        # Handle potential empty lines or malformed input.
        return

    # 1. Transform the string S into an array of "base values".
    #    s_prime[i] = (ord(S[i]) - ord('a') - i) % 26
    s_prime = []
    for i in range(n):
        # Python's % operator handles negative results appropriately for this problem.
        # e.g., (0 - 2) % 26 = -2 % 26 = 24.
        val = (ord(s[i]) - ord('a') - i) % 26
        s_prime.append(val)

    global_max_len = 0

    # 2. Iterate through all 26 possible target base values.
    for target_val in range(26):
        # Use a sliding window to find the longest subarray that can be converted
        # to target_val with at most K replacements.
        left = 0
        cost = 0  # Number of replacements needed in the current window [left, right]

        for right in range(n):
            # Expand the window to the right.
            if s_prime[right] != target_val:
                cost += 1

            # If cost exceeds K, shrink the window from the left.
            while cost > k:
                # If the element being removed was a mismatch, decrement cost.
                if s_prime[left] != target_val:
                    cost -= 1
                left += 1

            # The current window [left, right] is valid. Update the max length.
            current_len = right - left + 1
            if current_len > global_max_len:
                global_max_len = current_len
    
    # 3. Print the final result.
    print(global_max_len)


def main():
    """
    Main function to handle multiple test cases.
    """
    try:
        num_test_cases_str = sys.stdin.readline()
        if not num_test_cases_str: return
        num_test_cases = int(num_test_cases_str)
        
        for _ in range(num_test_cases):
            solve()
    except (IOError, ValueError):
        # Handles cases where input is empty or malformed.
        pass

if __name__ == "__main__":
    main()

Information

Submit By
Type
Submission
Problem
P1068 G. Alphabetical substring
Language
PyPy 3 (Python 3.9.18 PyPy 7.3.15)
Submit At
2025-07-15 02:36:42
Judged At
2025-07-15 02:36:42
Judged By
Score
100
Total Time
116ms
Peak Memory
24.438 MiB