/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 37ms 17.422 MiB
#2 Accepted 37ms 17.395 MiB
#3 Accepted 194ms 21.797 MiB
#4 Accepted 96ms 19.57 MiB
#5 Accepted 66ms 19.176 MiB
#6 Accepted 72ms 25.125 MiB
#7 Accepted 70ms 27.891 MiB
#8 Accepted 114ms 20.184 MiB
#9 Accepted 116ms 19.645 MiB
#10 Accepted 98ms 18.84 MiB
#11 Accepted 105ms 20.648 MiB
#12 Accepted 63ms 21.613 MiB
#13 Accepted 66ms 24.039 MiB
#14 Accepted 56ms 22.77 MiB
#15 Accepted 79ms 28.461 MiB
#16 Accepted 63ms 26.023 MiB
#17 Accepted 80ms 28.945 MiB
#18 Accepted 85ms 30.793 MiB
#19 Accepted 81ms 39.41 MiB
#20 Accepted 77ms 28.242 MiB
#21 Accepted 70ms 27.996 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-12-17 11:26:33
Judged By
Score
100
Total Time
194ms
Peak Memory
39.41 MiB