/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 38ms 17.391 MiB
#2 Accepted 38ms 17.363 MiB
#3 Accepted 198ms 21.91 MiB
#4 Accepted 101ms 19.758 MiB
#5 Accepted 68ms 19.066 MiB
#6 Accepted 69ms 25.113 MiB
#7 Accepted 72ms 27.973 MiB
#8 Accepted 125ms 20.191 MiB
#9 Accepted 120ms 19.828 MiB
#10 Accepted 98ms 18.941 MiB
#11 Accepted 112ms 20.637 MiB
#12 Accepted 70ms 21.664 MiB
#13 Accepted 70ms 23.977 MiB
#14 Accepted 56ms 22.68 MiB
#15 Accepted 81ms 28.695 MiB
#16 Accepted 65ms 26.152 MiB
#17 Accepted 85ms 28.785 MiB
#18 Accepted 90ms 30.785 MiB
#19 Accepted 79ms 39.363 MiB
#20 Accepted 79ms 28.375 MiB
#21 Accepted 79ms 27.93 MiB

Code

def main():
    t = int(input())  # Number of test cases
    for _ in range(t):
        n, k = map(int, input().split())  # Read n and k
        s = input()  # Read string s
        ans = 0
        temp = k
        p = list(s)  # Copy the string to p as a list of characters (for mutability)
        
        cntA = s.count('A')  # Count occurrences of 'A' in the string
        occA = []
        
        # Traverse the string from the back and replace '?' with 'B'
        for i in range(n-1, -1, -1):
            if temp == 0:
                break
            if s[i] == 'A':
                cntA -= 1
            if s[i] == '?':
                p[i] = 'B'
                temp -= 1
                occA.append(cntA)
        
        # Count 'B's in the modified string p
        cntB = p.count('B')
        tempB = cntB
        
        # Calculate the initial answer
        for i in range(n):
            if p[i] == 'B':
                tempB -= 1
            if s[i] == 'A':
                ans += tempB
        
        answer = ans
        pos = len(occA) - 1
        newA = 0

        # Update the answer based on occurrences of '?'
        for i in range(n):
            if pos < 0:
                break
            if s[i] == 'B':
                cntB -= 1
            if s[i] == '?':
                ans -= occA[pos]
                cntB -= 1
                pos -= 1
                ans += cntB
                ans -= newA
                newA += 1
            answer = max(answer, ans)
        
        print(answer)


if __name__ == "__main__":
    main()

Information

Submit By
Type
Submission
Problem
P1110 Subsequence of AB
Language
PyPy 3 (Python 3.9.18 PyPy 7.3.15)
Submit At
2024-10-21 09:59:43
Judged At
2024-11-11 02:36:15
Judged By
Score
100
Total Time
198ms
Peak Memory
39.363 MiB