Problem:


Score : 400 points

Problem Statement

Snuke has N strings. The i-th string is s_i.

Let us concatenate these strings into one string after arranging them in some order.
Find the maximum possible number of occurrences of AB in the resulting string.

Constraints

1 \leq N \leq 10^{4}

2 \leq |s_i| \leq 10

s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

For example, if we concatenate ABCA, BAD and XBAZ in this order, the resulting string ABCABADXBAZ has two occurrences of AB.

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4



Sample input:
3
ABCA
XBAZ
BAD


Sample output:
2

Submission status: Wrong Answer

Solution code:
N = int(input())

cntab = 0
cns = 0
cne = 0
cnse = 0
for _ in range(N):
    S = input()
    if S[0] == 'B' and S[-1] == 'A':
        cnse += 1
    elif S[0] == 'B':
        cns += 1
    elif S[-1] == 'A':
        cne += 1
    while 'AB' in S:
        S = S.replace('AB', '', 1)
        cntab += 1


if N == cnse:
    print(cntab + N - 1)
elif cnse == 0:
    print(cntab + min(cns, cne))
else:
    if (cns == 0) and (cne == 0):
        print(cntab + cnse - 1)
    elif (cns == 0) or (cne == 0):
        print(cntab + cnse)
    else:
        print(cntab + cnse + 1 + min(cns-1, cne-1))

Submission status: Wrong Answer

Solution code:
N = int(input())

cntab = 0
cns = 0
cne = 0
cnse = 0
for _ in range(N):
    S = input()
    while 'AB' in S:
        S = S.replace('AB', '', 1)
        cntab += 1
    if len(S) == 0:
        continue
    if S[0] == 'B' and S[-1] == 'A':
        cnse += 1
    elif S[0] == 'B':
        cns += 1
    elif S[-1] == 'A':
        cne += 1

if N == cnse:
    print(cntab + N - 1)
elif cnse == 0:
    print(cntab + min(cns, cne))
else:
    if (cns == 0) and (cne == 0):
        print(cntab + cnse - 1)
    elif (cns == 0) or (cne == 0):
        print(cntab + cnse)
    else:
        print(cntab + cnse + 1 + min(cns-1, cne-1))

Submission status: Wrong Answer

Solution code:
N = int(input())

cntab = 0
cns = 0
cne = 0
cnse = 0
for _ in range(N):
    S = input()
    while 'AB' in S:
        S = S.replace('AB', '', 1)
        cntab += 1
    if len(S) == 0:
        continue
    if S[0] == 'B' and S[-1] == 'A':
        cnse += 1
    elif S[0] == 'B':
        cns += 1
    elif S[-1] == 'A':
        cne += 1

if N == cnse:
    print(cntab + N - 1)
elif cnse == 0:
    print(cntab + min(cns, cne))
else:
    if cns == cne == 0:
        print(cntab + cnse - 1)
    elif cns == 0 or cne == 0:
        print(cntab + cnse)
    else:
        print(cntab + cnse + 1 + min(cns-1, cne-1))

Submission status: Wrong Answer

Solution code:
N = int(input())

cntab = 0
cns = 0
cne = 0
cnse = 0
for _ in range(N):
    S = input()
    while 'AB' in S:
        S = S.replace('AB', '', 1)
        cntab += 1
    if len(S) == 0:
        continue
    if S[0] == 'B' and S[-1] == 'A':
        cnse += 1
    elif S[0] == 'B':
        cns += 1
    elif S[-1] == 'A':
        cne += 1

if N == cnse:
    print(cntab + N - 1)
elif cnse == 0:
    print(cntab + min(cns, cne))
else:
    if cns == cne == 0:
        print(cntab + cnse - 1)
    if cns == 0 or cne == 0:
        print(cntab + cnse)
    else:
        print(cntab + cnse + 1 + min(cns-1, cne-1))

Submission status: Wrong Answer

Solution code:
N = int(input())

cntab = 0
cns = 0
cne = 0
cnse = 0
for _ in range(N):
    S = input()
    while 'AB' in S:
        S = S.replace('AB', '', 1)
        cntab += 1
    if len(S) == 0:
        continue
    if S[0] == 'B' and S[-1] == 'A':
        cnse += 1
    elif S[0] == 'B':
        cns += 1
    elif S[-1] == 'A':
        cne += 1

if N == cnse:
    print(cntab + N - 1)
else:
    print(cntab + cnse - 1 + min(cns, 1) + min(cne, 1) + max(0, min(cns-1, cne-1)))

Submission status: Wrong Answer

Solution code:
N = int(input())

cntab = 0
cns = 0
cne = 0
cnse = 0
for _ in range(N):
    S = input()
    while 'AB' in S:
        S = S.replace('AB', '', 1)
        cntab += 1
    if len(S) == 0:
        continue
    if S[0] == 'B' and S[-1] == 'A':
        cnse += 1
    elif S[0] == 'B':
        cns += 1
    elif S[-1] == 'A':
        cne += 1

if N == cnse:
    print(cntab + N -1)
else:
    print(cntab + cnse + min(cns, cne))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = 0
b = 0
c = 0
ans = 0
for _ in range(n):
    s = input()
    if s[0]=='B' and s[-1]=='A':
        c+=1
    elif s[0]=='B':
        b+=1
    elif s[-1]=='A':
        a+=1
    
    for i in range(len(s)-1):
        if s[i:i+2] == 'AB':
            ans += 1
if c>0:
    ans += c-1
    a += 1
    b += 1
ans += min(a,b)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = 0
b = 0
ans = 0
for _ in range(n):
    s = input()
    if s[0]=='B':
        b+=1
    if s[-1]=='A':
        a += 1
    
    for i in range(len(s)-1):
        if s[i:i+2] == 'AB':
            ans += 1
ans += min(a,b)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())

headB=0
tailA=0
middleAB=0
for _ in range(n):
    s=list(input())

    for i in range(1,len(s)):
        if s[i-1]=="A" and s[i]=="B":
            middleAB+=1
    if s[0]=="B":
        headB+=1
    if s[-1]=="A":
        tailA+=1

if headB==tailA and headB>0:
    print(middleAB+min(headB,tailA)-1)
else:
    print(middleAB+min(headB,tailA))

Submission status: Wrong Answer

Solution code:
n=int(input())

headB=0
tailA=0
middleAB=0
for _ in range(n):
    s=list(input())

    for i in range(1,len(s)):
        if s[i-1]=="A" and s[i]=="B":
            middleAB+=1
    if s[0]=="B":
        headB+=1
    if s[-1]=="A":
        tailA+=1

if headB==tailA:
    print(middleAB+min(headB,tailA)-1)
else:
    print(middleAB+min(headB,tailA))

Submission status: Wrong Answer

Solution code:
n=int(input())

headB=0
tailA=0
middleAB=0
for _ in range(n):
    s=list(input())

    for i in range(1,len(s)):
        if s[i-1]=="A" and s[i]=="B":
            middleAB+=1
    if s[0]=="B":
        headB+=1
    if s[-1]=="A":
        tailA+=1

if headB==tailA:
    print(middleAB+min(headB,tailA))
else:
    print(middleAB+min(headB,tailA)-1)

Submission status: Wrong Answer

Solution code:
n=int(input())

headB=0
tailA=0
middleAB=0
for _ in range(n):
    s=list(input())

    for i in range(1,len(s)):
        if s[i-1]=="A" and s[i]=="B":
            middleAB+=1
    if s[0]=="B":
        headB+=1
    if s[-1]=="A":
        tailA+=1

print(middleAB+min(headB,tailA))

Submission status: Wrong Answer

Solution code:
n = int(input())

a_end = 0
b_start = 0
cnt = 0

for _ in range(n):
    s = input()
    cnt += s.count("AB")
    if s[0] == "B":
        b_start += 1
    if s[-1] == "A":
        a_end += 1
print(cnt + min(b_start, a_end, n-1))

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N = int(input())
    S = [input().strip() for _ in range(N)]
    a = 0
    b = 0
    ab = 0
    ans = 0
    for s in S:
        if ab == 0 and s[0] == 'B' and s[-1] == 'A':
            ab = 1

        ans += s.count("AB")

        if s[-1] == 'A':
            a += 1
        if s[0] == 'B':
            b += 1

    if ab == 1:
        print(ans + min(max(a, b) - 1, min(a, b)))
    else:
        print(ans + min(a, b))


    
    
    
    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def cal_single(S,N):
    output = 0
    for i in range(N):
        output += S[i].count("AB")
    return output

def cal_cont(S,N):
    a,b,ab = 0,0,0
    for i in range(N):
        if S[i][0] == "B" and S[i][-1] == "A":
            ab += 1
        elif S[i][0] == "B":
            b += 1
        elif S[i][-1] == "A":
            a += 1

    output = min(a,b)
    if ab == 0:
        return output
    elif abs(a-b) == 0:
        output += (ab-1)
        return output
    else:
        output += ab
        return output

def main():
    N = int(input())
    S = [input() for _ in range(N)]

    ans = cal_single(S,N)+cal_cont(S,N)

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def cal_single(S,N):
    output = 0
    for i in range(N):
        output += S[i].count("AB")
    return output

def cal_cont(S,N):
    a,b,ab = 0,0,0
    for i in range(N):
        if S[i][0] == "B" and S[i][-1] == "A":
            ab += 1
        elif S[i][0] == "B":
            b += 1
        elif S[i][-1] == "A":
            a += 1

    output = 0
    min_a_b = min(a,b)
    output += min_a_b
    a -= min_a_b
    b -= min_a_b
    if ab == 0:
        return output
    else:
        output += (ab-1)
        ab = 1
        if max(a,b) > 0:
            output += 1
        return output

def main():
    N = int(input())
    S = [input() for _ in range(N)]

    ans = cal_single(S,N)+cal_cont(S,N)

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())

type1 = type2 = type3 = 0

ans = 0
while n > 0:
    s = input()
    ans += s.count("AB")

    if s[0] == "B" and s[-1] == "A":
        type3 += 1
    elif s[-1] == "A":
        type1 += 1
    elif s[0] == "B":
        type2 += 1

    n -= 1

ans += min(type1, type2)

if type3 > 0:
    if type1 == type2:
        ans += type3 - 1
    else:
        ans += type3

print(ans)

Submission status: Wrong Answer

Solution code:
def cal_single(S,N):
    output = 0
    for i in range(N):
        output += S[i].count("AB")
    return output

def cal_cont(S,N):
    output = 0
    end_A,st_B,eAsB = set(),set(),set()
    for i in range(N):
        if S[i][0] == "B" and S[i][len(S[i])-1] == "A":
            eAsB.add(i)
        elif S[i][0] == "B":
            st_B.add(i)
        elif S[i][len(S[i])-1] == "A":
            end_A.add(i)
    a,b,ab = len(end_A),len(st_B),len(eAsB)

    output += max(ab-1,0)
    if a == b:
        output += a
    else:
        if ab >= 1:
            output += (min(a,b)+1)
        else:
            output += min(a,b)
    return output

def main():
    N = int(input())
    S = [input() for _ in range(N)]

    ans = cal_single(S,N)+cal_cont(S,N)

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def cal_single(S,N):
    output = 0
    for i in range(N):
        output += S[i].count("AB")
    return output

def cal_cont(S,N):
    output = 0
    end_A,st_B,eAsB = set(),set(),set()
    for i in range(N):
        if S[i][0] == "B" and S[i][len(S[i])-1] == "A":
            eAsB.add(i)
        elif S[i][0] == "B":
            st_B.add(i)
        elif S[i][len(S[i])-1] == "A":
            end_A.add(i)
    a,b,ab = len(end_A),len(st_B),len(eAsB)

    output += max(ab-1,0)
    if ab >= 1:
        output += min(a,b)+1
    else:
        output += min(a,b)
    return output

def main():
    N = int(input())
    S = [input() for _ in range(N)]

    ans = cal_single(S,N)+cal_cont(S,N)

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
# import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    b_start = 0
    a_end = 0
    b_and_a = 0
    ans = 0

    for _ in range(n):
        s = input()
        ans += s.count("AB")
        flgB = s[0] == "B"
        flgA = s[-1] == "A"
        if flgB and flgA:
            b_and_a += 1
        elif flgB:
            b_start += 1
        elif flgA:
            a_end += 1

    remain_a = max(0, a_end - b_and_a)
    remain_b = max(0, b_start - b_and_a)
    remain_ab_a = max(0, b_and_a - b_start)
    remain_ab_b = max(0, b_and_a - a_end)

    ans += min(b_start, b_and_a)
    ans += min(a_end, b_and_a)
    ans += min(remain_a, remain_b)

    if remain_ab_a == remain_ab_b == b_and_a:
        ans += min(remain_ab_a, remain_ab_b) - 1
    else:
        ans += min(remain_ab_a, remain_ab_b)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
# import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    b_start = 0
    a_end = 0
    b_and_a = 0
    ans = 0

    for _ in range(n):
        s = input()
        ans += s.count("AB")
        flgB = s[0] == "B"
        flgA = s[-1] == "A"
        if flgB and flgA:
            b_and_a += 1
        elif flgB:
            b_start += 1
        elif flgA:
            a_end += 1

    remain_a = max(0, a_end - b_and_a)
    remain_b = max(0, b_start - b_and_a)
    remain_ab_a = max(0, b_and_a - b_start)
    remain_ab_b = max(0, b_and_a - a_end)

    ans += min(b_start, b_and_a)
    ans += min(a_end, b_and_a)
    ans += min(remain_a, remain_b)

    if remain_ab_a == remain_ab_b:
        if min(remain_ab_a, remain_ab_b) > 0:
            ans += min(remain_ab_a, remain_ab_b) - 1
    else:
        ans += min(remain_ab_a, remain_ab_b)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
# import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    b_start = 0
    a_end = 0
    b_and_a = 0
    ans = 0

    for _ in range(n):
        s = input()
        ans += s.count("AB")
        flgB = s[0] == "B"
        flgA = s[-1] == "A"
        if flgB and flgA:
            b_and_a += 1
        elif flgB:
            b_start += 1
        elif flgA:
            a_end += 1

    remain_a = max(0, a_end - b_and_a)
    remain_b = max(0, b_start - b_and_a)

    ans += min(b_start, b_and_a)
    ans += min(a_end, b_and_a)
    ans += min(remain_a, remain_b)
    ans += max(0, b_and_a - max(a_end, b_start))
    # if b_and_a - a_end == b_and_a - b_start and b_and_a - b_start > 0:
    #     ans -= 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
# import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    b_start = 0
    a_end = 0
    b_and_a = 0
    ans = 0

    for _ in range(n):
        s = input()
        ans += s.count("AB")
        flgB = s[0] == "B"
        flgA = s[-1] == "A"
        if flgB and flgA:
            b_and_a += 1
        elif flgB:
            b_start += 1
        elif flgA:
            a_end += 1

    remain_a = max(0, a_end - b_and_a)
    remain_b = max(0, b_start - b_and_a)

    ans += min(b_start, b_and_a)
    ans += min(a_end, b_and_a)
    ans += min(remain_a, remain_b)
    ans += max(0, b_and_a - max(a_end, b_start))
    if b_and_a - a_end == b_and_a - b_start and b_and_a - b_start > 0:
        ans -= 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

def main():
  n, *S = sys.stdin.read().split()
  a = b = ba = 0
  cnt = 0
  for s in S:
    cnt += s.count('AB')
    bl2 = s[0] == 'B'
    bl1 = s[-1] == 'A'
    a += bl1
    b += bl2 
    ba += bl1 & bl2 
  
  if a == b:
    cnt += a - (a == ba)
  else:
    cnt += min(a, b)
    
  print(cnt)

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque


def read():
    N = int(input().strip())
    S = []
    for i in range(N):
        s = input().strip()
        S.append(s)
    return N, S


def solve(N, S):
    count = 0
    for s in S:
        for j in range(len(s)):
            if s[j:j+2] == "AB":
                count += 1
    head_b = []
    tail_a = []
    for i, s in enumerate(S):
        if s[0] == "B":
            head_b.append(i)
        if s[-1] == "A":
            tail_a.append(i)
    if len(head_b) == 0 or len(tail_a) == 0:
        return count
    if len(head_b) == 1 and len(tail_a) == 1 and tail_a[0] == head_b[0]:
        return count
    return count + min(len(head_b), len(tail_a))



if __name__ == '__main__':
    inputs = read()
    print(solve(*inputs))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque


def read():
    N = int(input().strip())
    S = []
    for i in range(N):
        s = input().strip()
        S.append(s)
    return N, S


def solve(N, S):
    count = 0
    for s in S:
        for j in range(len(s)):
            if s[j:j+2] == "AB":
                count += 1
    head_b = []
    tail_a = []
    for i, s in enumerate(S):
        if s[0] == "B":
            head_b.append(i)
        if s[-1] == "A":
            tail_a.append(i)
    if len(head_b) == 0 or len(tail_a) == 0:
        return count
    if len(head_b) == 1 and head_b[0] in tail_a:
        return count
    if len(tail_a) == 1 and tail_a[0] in head_b:
        return count
    return count + min(len(head_b), len(tail_a))



if __name__ == '__main__':
    inputs = read()
    print(solve(*inputs))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque


def read():
    N = int(input().strip())
    S = []
    for i in range(N):
        s = input().strip()
        S.append(s)
    return N, S


def solve(N, S):
    count = 0
    for s in S:
        for j in range(len(s)):
            if s[j:j+2] == "AB":
                count += 1
    head_b = deque()
    tail_a = deque()
    for i, s in enumerate(S):
        if s[0] == "B":
            head_b.append(i)
        if s[-1] == "A":
            tail_a.append(i)
    
    while head_b and tail_a:
        h = head_b.popleft()
        t = tail_a.popleft()
        if h == t:
            if len(head_b) > 0:
                head_b.append(h)
                h = head_b.popleft()
            elif len(tail_b) > 0:
                tail_b.append(t)
                t = tail_a.popleft()
            else:
                return count
        count += 1
    return count



if __name__ == '__main__':
    inputs = read()
    print(solve(*inputs))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
from math import sqrt
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline()[:-1]

# n = int(input())



def solve():
    n = int(input())
    ans = 0
    a = 0
    b = 0
    c = 0
    for _ in range(n):
        s = input()
        a += s[-1] == 'A' and s[0] != 'B'
        b += s[0] == 'B' and s[-1] != 'A'
        c += s[0] == 'B' and s[-1] == 'A'
        ans += s.replace('AB', "*").count('*')
    print(ans + c + min(a, b))
    

t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()




"""




"""

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
from math import sqrt
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline()[:-1]

# n = int(input())



def solve():
    n = int(input())
    ans = 0
    a = 0
    b = 0
    for _ in range(n):
        s = input()
        a += s[-1] == 'A'
        b += s[0] == 'B'
        ans += s.replace('AB', "*").count('*')
    print(ans + min(a, b))
    

t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()




"""




"""

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
from math import sqrt
def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline()[:-1]

# n = int(input())



def solve():
    n = int(input())
    ans = 0
    a = 0
    b = 0
    for _ in range(n):
        s = input()
        a += s[-1] == 'A'
        b += s[0] == 'B'
        ans += s.replace('AB', "*").count('*')
    print(ans + min(a, b))
    

t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()




"""




"""

Submission status: Wrong Answer

Solution code:
n,*s=open(0).read().split()
a=0;b=0;ab=0;ans=0
for s_i in s:
  b+=(s_i[0]=='B')*(not s_i[-1]=='A')
  a+=(not s_i[0]=='B')*(s_i[-1]=='A')
  ab+=(s_i[0]=='B')*(s_i[-1]=='A')
  ans+=s_i.count('AB')
print(ans+ab+(min(a,b) if a+b>0 else -1))

Submission status: Wrong Answer

Solution code:
n,*s=open(0).read().split()
a=0;b=0;ans=0
for s_i in s:
  a+=(s_i[0]=='B')
  b+=(s_i[-1]=='A')
  ans+=s_i.count('AB')
print(ans+min(a,b))

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
lastA = 0
fastB = 0
for i in range(n):
  s = input()
  ans += s.count("AB")
  if s[-1] == "A" :
    lastA += 1
  if s[0] == "B":
    fastB += 1
 
comb = min(lastA,fastB)
print(ans+comb)

Submission status: Wrong Answer

Solution code:
n = int(input())

cntAB = 0
cntA = 0
cntB = 0
cntBA = 0
for i in range(n):
    s = input()
    if s[0] != 'B' and s[-1] == 'A':
        cntA += 1
    if s[0] == 'B' and s[-1] != 'A':
        cntB += 1
    if s[0] == 'B' and s[-1] == 'A':
        cntBA += 1
    cntAB += s.count('AB')

print(cntAB + min(cntA+cntBA, cntB+cntBA))

Submission status: Wrong Answer

Solution code:
n = int(input())

cntAB = 0
cntA = 0
cntB = 0
for i in range(n):
    s = input()
    if s[-1] == 'A':
        cntA += 1
    if s[0] == 'B':
        cntB += 1
    cntAB += s.count('AB')

print(cntAB + min(cntA, cntB))

Submission status: Wrong Answer

Solution code:
n = int(input())

cntAB = 0
cntA = 0
cntB = 0
for i in range(n):
    s = input()
    if s[-1] == 'A':
        cntA += 1
    if s[0] == 'B':
        cntB += 1
    if 'AB' in s:
        cntAB += 1

print(cntAB + min(cntA, cntB))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for i in range(n)]

ab = 0
a = 0
b = 0

d = 0
for i in s:
	d += i.count('AB')
	if i[0] == 'B' and i[-1] == 'A':
		ab += 1
	elif i[-1] == 'A':
		a += 1
	elif i[0] == 'B':
		b += 1

if ab == 1:
	if a > 0 and b > 0:
		d += 1

d += min(a,b)
d += max(ab-1,0)
if ab > 0:
	if abs(a-b) > 0:
		d += 1
print (d)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for i in range(n)]

ab = 0
a = 0
b = 0

d = 0
for i in s:
	d += i.count('AB')
	if i[0] == 'B' and i[-1] == 'A':
		ab += 1
	elif i[-1] == 'A':
		a += 1
	elif i[0] == 'B':
		b += 1

d += min(a,b)
d += max(ab-1,0)
if ab > 0:
	if abs(a-b) > 0:
		d += 1
print (d)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for i in range(n)]

ab = 0
a = 0
b = 0

d = 0
for i in s:
	d += i.count('AB')
	if i[0] == 'B' and i[-1] == 'A':
		ab += 1
	elif i[-1] == 'A':
		a += 1
	elif i[0] == 'B':
		b += 1

d += min(a,b)
d += max(ab-1,0)
if abs(a-b) > 0:
	d += 1
print (d)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for i in range(n)]

ab = 0
a = 0
b = 0

d = 0
for i in s:
	d += i.count('AB')
	if i[0] == 'B' and i[-1] == 'A':
		ab += 1
	elif i[-1] == 'A':
		a += 1
	elif i[0] == 'B':
		b += 1

if ab == 1:
	if a > 0:
		d += 1
		a -= 1
	if b > 0:
		d += 1
		b -= 1


if ab-1 > 0:
	a += 1
	b += 1
d += max((ab-1),0)+min(a,b)
print (d)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for i in range(n)]

ab = 0
a = 0
b = 0

d = 0
for i in s:
	d += i.count('AB')
	if i[0] == 'B' and i[-1] == 'A':
		ab += 1
	elif i[-1] == 'A':
		a += 1
	elif i[0] == 'B':
		b += 1

if ab > 0:
	a += 1
	b += 1
d += max((ab-1),0)+min(a,b)
print (d)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N = int(readline())
    start_B = 0
    end_A = 0
    start_B_end_A = 0
    AB = 0
    for _ in range(N):
        S = readline().decode('utf-8').strip()
        if S[0]=='B' and S[-1]=='A':
            start_B_end_A += 1
        elif S[0]=='B':
            start_B += 1
        elif S[-1] == 'A':
            end_A += 1
        AB += S.count('AB')
    if start_B_end_A>=2:
        ans = AB + (start_B_end_A - 1) + min(min(start_B,end_A)+1,max(start_B,end_A))
    elif start_B_end_A==1:
        ans = AB + min(min(start_B,end_A)+1,max(start_B,end_A))
    else:
        ans = AB + min(min(start_B,end_A),max(start_B,end_A))
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
res = 0
sboth = 0
sa = 0
sb = 0
for i in range(n):
    s = input()
    res += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        sboth += 1
        continue
    if s[0] == "B":
        sb += 1
        continue
    if s[-1] == "A":
        sa += 1
        continue

num = min(sboth, sa)
res += num
sboth -= num

num = min(sboth, sb)
res += num
sboth -= num

num = min(sa, sb)
res += num
sa -= num
sb -= num


num = min(sboth, sa)
res += num
sboth -= num

num = min(sboth, sb)
res += num
sboth -= num

num = min(sa, sb)
res += num
sa -= num
sb -= num



if sboth > 0:
    sboth = 1
    res += sboth - 1



print(res)

Submission status: Wrong Answer

Solution code:
n = int(input())
res = 0
sboth = 0
sa = 0
sb = 0
for i in range(n):
    s = input()
    res += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        sboth += 1
        continue
    if s[0] == "B":
        sb += 1
        continue
    if s[-1] == "A":
        sa += 1
        continue


num = min(sa, sb)
res += num
sa -= num
sb -= num


num = min(sboth, sa)
res += num
sboth -= num

num = min(sboth, sb)
res += num
sboth -= num

if sboth > 0:
    sboth = 1
    res += sboth - 1

num = min(sa, sb)
res += num
sa -= num
sb -= num


num = min(sboth, sa)
res += num
sboth -= num

num = min(sboth, sb)
res += num
sboth -= num

if sboth > 0:
    sboth = 1
    res += sboth - 1




print(res)

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

N = I()
s = []
for i in range(N):
    s.append(list(input()))

count = 0
enda = 0
startb = 0
both = 0
for i in range(N):
    for j in range(len(s[i])-1):
        if s[i][j] == 'A' and s[i][j+1] == 'B':
            count += 1
    if s[i][-1] == 'A':
        enda += 1
    if s[i][0] == 'B':
        startb += 1
    if s[i][-1] == 'A' and s[i][0] == 'B':
        both += 1

temp = count+min(enda,startb)
if enda == startb == both:
    temp -= 1
print(temp)

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

N = I()
s = []
for i in range(N):
    s.append(list(input()))

count = 0
enda = 0
startb = 0
both  = 0
for i in range(N):
    for j in range(len(s[i])-1):
        if s[i][j] == 'A' and s[i][j+1] == 'B':
            count += 1
    if s[i][-1] == 'A' and s[i][0] == 'B':
        both += 1
    elif s[i][-1] == 'A':
        enda += 1
    elif s[i][0] == 'B':
        startb += 1
    else:
        pass

left = max(enda,startb)-min(enda,startb)
print(count+min(enda,startb)+max(0,left+both-1))

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

N = I()
s = []
for i in range(N):
    s.append(list(input()))

count = 0
enda = 0
startb = 0
for i in range(N):
    for j in range(len(s[i])-1):
        if s[i][j] == 'A' and s[i][j+1] == 'B':
            count += 1
    if s[i][-1] == 'A':
        enda += 1
    if s[i][0] == 'B':
        startb += 1

print(count+min(enda,startb))

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline


def main():
    N = int(readline())
    cnt = 0; leftB = 0; rightA = 0
    for _ in range(N):
        s = readline()[:-1]
        for i in range(len(s)-1):
            subs = s[i:i+2]
            cnt += subs == 'AB'
        leftB += s[0] == 'B'
        rightA += s[-1] == 'A'
    
    ans = cnt + min(N-1, leftB, rightA)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[input() for i in range(n)]
lastA=0
firstB=0
lastAandfirstB=0
ABs=0
for si in s:
    ABs+=si.count("AB")
    firstB+=(si[0]=="B")
    lastA+=(si[-1]=="A")
    lastAandfirstB+=(si[0]=="B")*(si[-1]=="A")

can=min(firstB,lastA)
# print(lastA,firstB,lastAandfirstB)
# if lastAandfirstB==1 and firstB==1 and lastA==1:
#     print(ABs)
#     exit()
if can==n or lastAandfirstB==firstB==lastA:
    can=can-1
print(can+ABs)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[input() for i in range(n)]
lastA=0
firstB=0
lastAandfirstB=0
ABs=0
for si in s:
    ABs+=si.count("AB")
    firstB+=(si[0]=="B")
    lastA+=(si[-1]=="A")
    lastAandfirstB+=(si[0]=="B")*(si[-1]=="A")

can=min(firstB,lastA)
print(lastA,firstB,lastAandfirstB)
if lastAandfirstB==1 and firstB==1 and lastA==1:
    print(ABs)
    exit()
if can==n or lastAandfirstB==firstB==lastA:
    can=can-1
print(can+ABs)