/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 33ms 17.395 MiB
#2 Accepted 34ms 17.293 MiB
#3 Accepted 179ms 23.48 MiB
#4 Accepted 85ms 19.562 MiB
#5 Accepted 59ms 19.055 MiB
#6 Accepted 60ms 24.992 MiB
#7 Accepted 65ms 27.754 MiB
#8 Accepted 105ms 20.082 MiB
#9 Accepted 116ms 19.641 MiB
#10 Accepted 83ms 18.785 MiB
#11 Accepted 94ms 20.621 MiB
#12 Accepted 68ms 21.492 MiB
#13 Accepted 60ms 24.156 MiB
#14 Accepted 49ms 22.727 MiB
#15 Accepted 71ms 28.578 MiB
#16 Accepted 56ms 26.039 MiB
#17 Accepted 73ms 28.906 MiB
#18 Accepted 81ms 30.773 MiB
#19 Accepted 73ms 39.301 MiB
#20 Accepted 70ms 28.266 MiB
#21 Accepted 66ms 27.789 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-10-21 09:59:43
Judged By
Score
100
Total Time
179ms
Peak Memory
39.301 MiB