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:
#!/usr/bin/env python3
n = int(input())
a = b = c = ans = 0
for _ in range(n):
    s = input()
    ans += s.count("AB")
    a += s[-1] == "A"
    b += s[0] == "B"
    c += s[-1] + s[0] == "AB"
if a == c and b==c:
    print(ans + c -1)
else:
    print(ans + min(a, b))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n = int(input())
a = b = c = ans = 0
for _ in range(n):
    s = input()
    ans += s.count("AB")
    a += s[-1] == "A"
    b += s[0] == "B"
    c += s[-1] + s[0] == "AB"
print(ans + min(a, b) - (a == c and b == c))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n = int(input())
a = b = c = ans = 0
for _ in range(n):
    s = input()
    ans += s.count("AB")
    a += s[-1] == "A"
    b += s[0] == "B"
    c += s[-1] + s[0] == "AB"
print(ans + min(a, b) - 1*(a == b == c))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n = int(input())
a = b = c = ans = 0
for _ in range(n):
    s = input()
    ans += s.count("AB")
    a += s[-1] == "A"
    b += s[0] == "B"
    c += s[-1] + s[0] == "AB"
print(ans + min(a, b) - (a == b == c))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n = int(input())
a = 0
b = 0
ans = 0
for _ in range(n):
    s = input()
    ans += s.count("AB")
    a += s[-1] == "A"
    b += s[0] == "B"
print(ans + min(a, b,n-1))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n = int(input())
a = 0
b = 0
ans = 0
for _ in range(n):
    s = input()
    ans += s.count("AB")
    a += s[-1] == "A"
    b += s[0] == "B"
print(ans + min(a, b))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n = int(input())
a = 0
b = 0
ans = 0
for _ in range(n):
    s = input()
    ans += 'AB' in s
    a += s[-1] == 'A'
    b += s[0] == 'B'
print(ans + min(a, b))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
internal_AB = 0
B_A = 0
B_ = 0
_A = 0
for i in range(n):
    s = input()
    for j in range(len(s)-1):
        if s[j] == 'A' and s[j+1] == 'B':
            internal_AB += 1
    if s[0] == 'B' and s[-1] == 'A':
        B_A += 1
    elif s[0] == 'B':
        B_ += 1
    elif s[-1] == 'A':
        _A += 1
if _A == 0 and B_ == 0:
    ans = max(0,B_A - 1)
elif _A == 0 or B_ == 0:
    ans = B_A
else:
    ans = B_-1 + _A-1 + max(B_A-1, 0)
print(ans + internal_AB)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans,a,b,ab = 0,0,0,0

for i in range(N):
    s = input()
    ans += s.count("AB")
    if s[-1] == "A" and s[0] == "B":
        ab += 1
    elif s[-1] == "A":
        a += 1
    elif s[0] == "B":
        b += 1

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def resolve():
    '''
    code here
    '''
    N = int(input())
    Ss = [input() for _ in range(N)]

    num_ab = 0
    num_a = 0
    num_b = 0
    num_b_a = 0
    # ３つの関係で解の条件分け
    

    for line in Ss:
        prev = ''
        for j in range(len(line)):
            if prev == 'A' and line[j] == 'B':
                num_ab += 1
            prev = line[j]
        
        if line[0] == 'B' and line[-1] == 'A':
            num_b_a += 1
        elif line[-1] == 'A':
            num_a += 1
        elif line[0] == 'B':
            num_b += 1
        
    res = num_ab
    if num_a >= num_b:
        res += num_b
        delta = num_a - num_b
        if delta > 0:
            res += num_b_a
        elif delta == 0:
            if num_b_a > 0:
                res += num_b_a -1
        else:
            res += num_b_a -1
        
    else:
        res += num_a
        delta = num_b - num_a
        if delta > 0:
            res += num_b_a
        elif delta == 0:
            if num_b_a > 0:
                res += num_b_a -1
        else:
            res += num_b_a -1

    print(res)

        




if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    '''
    code here
    '''
    N = int(input())
    Ss = [input() for _ in range(N)]

    num_ab = 0
    num_a = 0
    num_b = 0
    num_b_a = 0
    # ３つの関係で解の条件分け
    

    for line in Ss:
        prev = ''
        for j in range(len(line)):
            if prev == 'A' and line[j] == 'B':
                num_ab += 1
            prev = line[j]
        
        if line[0] == 'B' and line[-1] == 'A':
            num_b_a += 1
        elif line[-1] == 'A':
            num_a += 1
        elif line[0] == 'B':
            num_b += 1
        
    res = num_ab

    if num_a > num_b:
        res += num_b
        res += num_b_a

    else:
        res += num_a
        res += num_b_a -1


    print(res)

        




if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    '''
    code here
    '''
    N = int(input())
    Ss = [input() for _ in range(N)]

    num_ab = 0
    num_a = 0
    num_b = 0
    num_b_a = 0

    for line in Ss:
        prev = ''
        for j in range(len(line)):
            if prev == 'A' and line[j] == 'B':
                num_ab += 1
            prev = line[j]
        
        if line[0] == 'B' and line[-1] == 'A':
            num_b_a += 1
        elif line[-1] == 'A':
            num_a += 1
        elif line[0] == 'B':
            num_b += 1
        
    if num_a > num_b:
        res = num_ab
        res += num_b
        delta = num_a - num_b
        res += min(num_b_a, delta)
        if num_b_a > delta:
            res += num_b_a - delta
        
    else:
        res = num_ab
        res += num_a
        delta = num_b - num_a
        res += min(num_b_a, delta)
        if num_b_a > delta:
            res += num_b_a - delta

    print(res)

        




if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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

cnt = 0
cnta = 0
cntb = 0
cntab = 0
for i in range(n):
    if 'AB' in s[i]:
        cnt += s[i].count('AB')
    if s[i][0] == 'B' and s[i][-1] == 'A':
        cntab += 1
    elif s[i][0] == 'B':
        cntb += 1
    elif s[i][-1] == 'A':
        cnta += 1
    
if max(cnta, cntb) == cntab: cnta -= 1
cnt += min(cnta, cntb)
print(cnt)

Submission status: Wrong Answer

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

cnt = 0
cnta = 0
cntb = 0
cntab = 0
for i in range(n):
    if 'AB' in s[i]:
        cnt += 1
    if s[i][0] == 'B' and s[i][-1] == 'A':
        cntab += 1
    elif s[i][0] == 'B':
        cntb += 1
    elif s[i][-1] == 'A':
        cnta += 1
    
if cntab == 0:
    cnt += min(cnta, cntb)
else:
    if cnta + cntb > 0:
        cnt += cntab + min(cnta, cntb)
    else:
        cnt += cntab - 1
print(cnt)

Submission status: Wrong Answer

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

cnt = 0
cnta = 0
cntb = 0
cntab = 0
for i in range(n):
    if 'AB' in s[i]:
        cnt += 1
    if s[i][0] == 'B':
        cntb += 1
    if s[i][-1] == 'A':
        cnta += 1
    if s[i][0] == 'B' and s[i][-1] == 'A':
        cntab += 1
if max(cnta, cntb) == cntab: cnta -= 1
cnt += min(cnta, cntb)
print(cnt)

Submission status: Wrong Answer

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

cnt = 0
cnta = 0
cntb = 0
for i in range(n):
    if 'AB' in s[i]:
        cnt += 1
    if s[i][0] == 'B':
        cntb += 1
    if s[i][-1] == 'A':
        cnta += 1
cnt += min(min(cnta, cntb), n-1)
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
ba = 0 # bで始まりaで終わる
a = 0 # 末尾がa
b = 0 # 先頭がb
sss = []
for i in range(n):
    s = input()
    for j in range(len(s)-1):
        if s[j:j+2] == 'AB':
            ans+=1
    if s[0] == 'B' and s[-1] == 'A': ba += 1
    else:
        if s[0] == 'B': b+=1
        if s[-1] == 'A': a+=1
"""
貪欲で解けると思う
まずbaを並べて、可能な限りその先頭にa、末尾にbをつける
a,bが残るようであれば、ペアにする
"""
if a>0 and b>0:
    ans += ba+1
    a-=1; b-=1
else:
    if a>0:
        # a ba ba と並べる
        ans += ba
        a-=1
    elif b>0:
        # ba ba b と並べる
        ans += ba
        b-=1
    else:
        # ba ba と並べる
        ans += ba-1
ans += min(a, b)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s_ls = [0] * n
start_b = 0
end_a = 0
start_b_and_end_a = 0
internal_AB = 0
for i in range(n):
    s = input()
    s_ls[i] = s
    if s[0] == 'B' and s[-1] != 'A':
        start_b += 1
    elif s[0] != 'B' and s[-1] == 'A':
        end_a += 1
    elif s[0] == 'B' and s[-1] == 'A':
        start_b_and_end_a += 1
    for j in range(len(s)-1):
        if s[j:j+2] == 'AB':
            internal_AB += 1
ans = min(start_b,end_a)
diff = start_b_and_end_a - ans
if diff >= 0:
    ans += ans
    start_b_and_end_a -= diff
ans += max(0,start_b_and_end_a-1)
print(ans+internal_AB)

Submission status: Wrong Answer

Solution code:
n = int(input())
s_ls = [0] * n
start_b = 0
end_a = 0
start_b_and_end_a = 0
internal_AB = 0
for i in range(n):
    s = input()
    s_ls[i] = s
    if s[0] == 'B' and s[-1] != 'A':
        start_b += 1
    elif s[0] != 'B' and s[-1] == 'A':
        end_a += 1
    elif s[0] == 'B' and s[-1] == 'A':
        start_b_and_end_a += 1
    for j in range(len(s)-1):
        if s[j:j+2] == 'AB':
            internal_AB += 1
ans = min(start_b,end_a)
diff = start_b_and_end_a - ans
if diff >= 0:
    ans += ans
    ans += max(0,diff-1)
else:
    ans += start_b_and_end_a
print(ans+internal_AB)

Submission status: Wrong Answer

Solution code:
n = int(input())
s_ls = [0] * n
start_b = 0
end_a = 0
start_b_and_end_a = 0
internal_AB = 0
for i in range(n):
    s = input()
    s_ls[i] = s
    if s[0] == 'B' and s[-1] != 'A':
        start_b += 1
    elif s[0] != 'B' and s[-1] == 'A':
        end_a += 1
    elif s[0] == 'B' and s[-1] == 'A':
        start_b_and_end_a += 1
    for j in range(len(s)-1):
        if s[j:j+2] == 'AB':
            internal_AB += 1
ans = min(start_b,end_a)+internal_AB
diff = start_b_and_end_a - ans
if diff >= 0:
    ans += ans
    ans += max(0,diff-1)
else:
    ans += start_b_and_end_a
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s_ls = [0] * n
start_b = 0
end_a = 0
start_b_and_end_a = 0
internal_AB = 0
for i in range(n):
    s = input()
    s_ls[i] = s
    if s[0] == 'B' and s[-1] != 'A':
        start_b += 1
    elif s[0] != 'B' and s[-1] == 'A':
        end_a += 1
    elif s[0] == 'B' and s[-1] == 'A':
        start_b_and_end_a += 1
    for j in range(len(s)-1):
        if s[j:j+2] == 'AB':
            internal_AB += 1
ans = 0
if min(start_b,end_a) == n:
    ans += n-1+internal_AB
else:
    ans += min(start_b,end_a)+internal_AB
diff = start_b_and_end_a - ans
if diff >= 0:
    ans += ans
    ans += max(0,diff-1)
else:
    ans += start_b_and_end_a
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s_ls = [0] * n
start_b = 0
end_a = 0
internal_AB = 0
for i in range(n):
    s = input()
    s_ls[i] = s
    if s[0] == 'B':
        start_b += 1
    if s[-1] == 'A':
        end_a += 1
    for j in range(len(s)-1):
        if s[j:j+2] == 'AB':
            internal_AB += 1
if min(start_b,end_a) != n:
    print(min(start_b,end_a)+internal_AB)
else:
    print(n-1+internal_AB)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np    # cumsum
# from bisect import bisect_left, bisect_right

def solve():
    N = II()
    A = []
    B = []
    BA = []
    cnt = 0
    for i in range(N):
        s = input()
        l = list(s)
        if l[0] == 'B' and l[-1] == 'A':
            BA.append(i)
        elif l[0] == 'B':
            B.append(i)
        elif l[-1] == 'A':
            A.append(i)
        cnt += s.count('AB')
    # print(A, B, BA, cnt)
    la = len(A)
    lb = len(B)
    lba = len(BA)

    if la > 0 and lb > 0:
        cnt += min(la, lb) + lba
    else:
        cnt += lba
    print(cnt)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
N = int(input())
both = 0 
cnt = 0
gA = 0
sB = 0
for _ in range(N):
    s = input()
    cnt += s.count('AB')
    if s[-1] == 'A': gA += 1
    if s[0] == 'B': sB += 1
    if s[0] == 'B' and s[-1] == 'A': both += 1
ans = min(gA, sB) + cnt
if both == gA and gA == sB:
    ans -= 1
print(ans)
# print(cnt, gA, sB, both)

Submission status: Wrong Answer

Solution code:
N = int(input())
both = 0 
cnt = 0
gA = 0
sB = 0
for _ in range(N):
    s = input()
    cnt += s.count('AB')
    if s[-1] == 'A': gA += 1
    if s[0] == 'B': sB += 1
    if s[0] == 'B' and s[-1] == 'A': both += 1
ans = min(gA, sB) + cnt
if both == min(gA, sB):
    ans -= 1
print(ans)
# print(cnt, gA, sB)

Submission status: Wrong Answer

Solution code:
N = int(input())
cnt = 0
gA = 0
sB = 0
for _ in range(N):
    s = input()
    cnt += s.count('AB')
    if s[-1] == 'A': gA += 1
    if s[0] == 'B': sB += 1

ans = min(gA, sB) + cnt
print(ans)
# print(cnt, gA, sB)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = 0
b = 0
c = 0
total = 0
for _ in range(n):
    s = input()
    total += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        c+=1
    elif s[-1] == 'A':
        a+=1
    elif s[0] == 'B':
        b+=1

if a+b == 0:
    print(max(0, total + c - 1))
else:
    print(total + min(a, b) + c)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = 0
b = 0
total = 0
for _ in range(n):
    s = input()
    total += s.count('AB')
    if s[0] == 'B':
        b+=1
    if s[-1] == 'A':
        a+=1

if min(a, b) == n:
    print(total + min(a, b) - 1)
else:
    print(total + min(a, b))

Submission status: Wrong Answer

Solution code:
a = 0
b = 0
total = 0
for _ in range(int(input())):
    s = input()
    total += s.count('AB')
    if s[0] == 'B':
        b+=1
    if s[-1] == 'A':
        a+=1
print(total + min(a, b))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=0
b=0
ab=0
num=0
for i in range(n):
    s=input()
    if s[0]=="B" and s[-1]=="A":
        ab+=1
    elif s[0]=="B":
        b+=1
    elif s[-1]=="A":
        a+=1
    for j in range(len(s)-1):
        if s[j]=="A" and s[j+1]=="B":
            num+=1
if a!=0 and b!=0:
    num+=ab+1
    a-=1
    b-=1
    num+=min(a,b)
elif a!=0:
    num+=ab
elif b!=0:
    num+=ab
else:
    num+=ab-1
print(num)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=0
b=0
ab=0
num=0
for i in range(n):
    s=input()
    if s[0]=="B" and s[-1]=="A":
        ab+=1
    elif s[0]=="B":
        b+=1
    elif s[-1]=="A":
        a+=1
    for i in range(len(s)-1):
        if s[i]=="A" and s[i+1]=="B":
            num+=1
if a!=0 and b!=0:
    num+=ab+1
    a-=1
    b-=1
    num+=min(a,b)
elif a!=0:
    num+=ab
elif b!=0:
    num+=ab
else:
    num+=ab-1
print(num)

Submission status: Wrong Answer

Solution code:
import sys,math,collections,itertools
input = sys.stdin.readline

N = int(input())
Aend = []
Bstart = []
Ae_Bs = []
cnt = 0

for i in range(N):
    S = input().rstrip()
    if S[0]=='B' and S[-1] =='A':
        Ae_Bs.append(S)
    elif S[0] == 'B':
        Bstart.append(S)
    elif S[-1] == 'A':
        Aend.append(S)
    cnt += S.count('AB')

lAe_Bs = len(Ae_Bs)
lBstart = len(Bstart)
lAend = len(Aend)

if lAe_Bs >0:
    cnt += lAe_Bs -1
    
cnt += min(lBstart,lAend)
cnt += (abs(lBstart-lAend)>0 and lAe_Bs >0)

print(cnt)

Submission status: Wrong Answer

Solution code:
import sys,math,collections,itertools
input = sys.stdin.readline

N = int(input())
Aend = []
Bstart = []
Ae_Bs = []
cnt = 0

for i in range(N):
    S = input().rstrip()
    if S[0]=='B' and S[-1] =='A':
        Ae_Bs.append(S)
    elif S[0] == 'B':
        print(S)
        Bstart.append(S)
    elif S[-1] == 'A':
        Aend.append(S)
    cnt += S.count('AB')

lAe_Bs = len(Ae_Bs)
lBstart = len(Bstart)
lAend = len(Aend)

if lAe_Bs >0:
    cnt += lAe_Bs -1
    
cnt += min(lBstart,lAend)
cnt += (abs(lBstart-lAend)>0 and lAe_Bs >0)

print(cnt)

Submission status: Wrong Answer

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    s = [SS() for _ in range(N)]

    ans = sum([i.count('AB') for i in s])

    # .*AとB.*をいくつくっつけられるか
    # .*AにB.*をくっつけた後、間にB.*Aを挟む方針 挟み方はどうでもいい
    tA = 0
    fB = 0
    fBtA = 0
    for i in s:
        if i[-1] == 'A' and i[0] == 'B':
            fBtA += 1
        elif i[-1] == 'A':
            tA += 1
        elif i[0] == 'B':
            fB += 1

    if tA == fB == 0:
        ans += min(fBtA - 1, 0)
    else:
        ans += min(tA, fB) + fBtA

    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    s = [SS() for _ in range(N)]

    ans = sum([i.count('AB') for i in s])

    # .*AとB.*をいくつくっつけられるか
    # .*AにB.*をくっつけた後、間にB.*Aを挟む方針 挟み方はどうでもいい
    tA = 0
    fB = 0
    fBtA = 0
    for i in s:
        if i[-1] == 'A' and i[0] == 'B':
            fBtA += 1
        elif i[-1] == 'A':
            tA += 1
        elif i[0] == 'B':
            fB += 1

    if tA == fB == 0:
        ans += fBtA - 1
    else:
        ans += min(tA, fB) + fBtA

    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    s = [SS() for _ in range(N)]

    ans = sum([i.count('AB') for i in s])

    # .*AとB.*をいくつくっつけられるか
    # .*AにB.*をくっつけた後、間にB.*Aを挟む方針 挟み方はどうでもいい
    tA = 0
    fB = 0
    fBtA = 0
    for i in s:
        if i[-1] == 'A' and i[0] == 'B':
            fBtA += 1
        elif i[-1] == 'A':
            tA += 1
        elif i[0] == 'B':
            fB += 1

    ans += min(tA, fB) + fBtA

    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
ans2=0
A = 0
B = 0
for i in range(n):
  flg=False
  s = input()
  ans+=s.count("AB")
  if s[0]=="B":
    flg=True
    if A>=1:
      ans2+=1
      A-=1
      B-=1
  if s[-1]=="A":
    if B>=1:
      ans2+=1
      A-=1
      B-=1
    A+=1
  if flg:
    B+=1
#if ans2==n:
#  ans2-=1
print(ans+ans2)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
A = 0
B = 0
for i in range(n):
  flg=False
  s = input()
  ans+=s.count("AB")
  if s[0]=="B":
    flg=True
    if A>=1:
      ans+=1
      A-=1
      B-=1
  if s[-1]=="A":
    if B>=1:
      ans+=1
      A-=1
      B-=1
    A+=1
  if flg:
    B+=1

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
cntAb,fBlA,lA,fB=0,0,0,0
for _ in range(n):
  s=input()
  cntAb+=s.count("AB")
  if s[0]=="B" and s[-1]=="A":
    fBlA+=1
  elif s[0]=="B":
    fB+=1
  elif s[-1]=="A":
    lA+=1
ans=cntAb
a,b,c=min(fB,lA),max(fB,lA),fBlA
if a==b:
  ans+=a
  ans+=c//2
else:
  ans+=a
  ans+=min(b-a,c)
  ans+=(c-min(b-a,c))//2
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()

    n = int(input())
    s_table = [0]*3
    ans = 0
    for i in range(n):
        s = input()
        ans += s.count('AB')
        if s[0]=='B' and s[-1] == 'A':
            s_table[0] += 1
        elif s[0] == 'B' and s[-1] != 'A':
            s_table[1] += 1
        elif s[0] != 'B' and s[-1] == 'A':
            s_table[2] += 1
    
    m = min(s_table[1:])
    s_table[0] += m
    s_table[1] -= m
    s_table[2] -= m
    ans += s_table[0]-1 + min(1, sum(s_table[1:])) 
    print(ans)
        
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())

    cnt_BA = cnt_A = cnt_B = cnt_AB = 0
    for _ in range(n):
        s = input().rstrip()
        cnt_AB += s.count("AB")

        if s[0] == "B" and s[-1] == "A":
            cnt_BA += 1
        elif s[0] == "B":
            cnt_B += 1
        elif s[-1] == "A":
            cnt_A += 1

    res = cnt_AB + (cnt_BA - 1) + min(cnt_A, cnt_B)
    cnt_BA = 1
    cnt_A = cnt_A - min(cnt_A, cnt_B)
    cnt_B = cnt_B - min(cnt_A, cnt_B)
    if cnt_A != 0 and cnt_BA != 0:
        res += min(cnt_A, cnt_BA)
    elif cnt_B != 0 and cnt_BA != 0:
        res += min(cnt_B, cnt_BA)
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
N = int(input())
cnt = 0
st_cnt = 0
end_cnt =0
same_cnt =0

for k in range(N):
    s = input()
    cnt +=s.count("AB")

    if (s[0] =='B') & (s[-1]=='A'):
        same_cnt += 1
    elif (s[0] =='B'):
        st_cnt +=1
    elif (s[-1] == 'A'):
        end_cnt += 1

if (st_cnt==0) and (end_cnt==0):
    print(cnt+same_cnt-1)
else:print(cnt+same_cnt+min(st_cnt,end_cnt))

Submission status: Wrong Answer

Solution code:
N = int(input())
cnt = 0
st_cnt = 0
end_cnt =0
same_cnt =0

for k in range(N):
    s = input()
    i = len(s)-1 
    cnt +=s.count("AB")

    if (s[0] =='B') & (s[i]=='A'):
        same_cnt += 1
    elif (s[0] =='B'):
        st_cnt +=1
    elif (s[i] == 'A'):
        end_cnt += 1

if (st_cnt==0) and (end_cnt==0):
    print(cnt+same_cnt-1)
else:print(cnt+min(st_cnt,end_cnt))

Submission status: Wrong Answer

Solution code:
N = int(input())
cnt = 0
st_cnt = 0
end_cnt =0
same_cnt =0

for k in range(N):
    s = input()
    for i in range(len(s)):
        if i == (len(s)-1):
            if s[i]=='A':
                end_cnt += 1
        elif (i == 0) & (s[i]=='B'):
            st_cnt += 1
        elif s[i]=='A':
            if s[i+1]=='B':
                cnt += 1

    if (s[0] =='B') & (s[i]=='A'):
        same_cnt += 1

if ((st_cnt-same_cnt)==0) and ((end_cnt-same_cnt)==0):
    print(cnt+same_cnt-1)
else:print(cnt+min(st_cnt,end_cnt))

Submission status: Wrong Answer

Solution code:
N = int(input())
cnt = 0
st_cnt = 0
end_cnt =0
same_cnt =0

for k in range(N):
    s = input()
    for i in range(len(s)):
        if i == (len(s)-1):
            if s[i]=='A':
                end_cnt += 1
        elif (i == 0) & (s[i]=='B'):
            st_cnt += 1
        elif s[i]=='A':
            if s[i+1]=='B':
                cnt += 1
    
    if (s[0] =='B') & (s[i]=='A'):
        same_cnt += 1
        
if st_cnt == end_cnt:
    print(cnt+st_cnt-same_cnt)
else:print(cnt+min(st_cnt,end_cnt))

Submission status: Wrong Answer

Solution code:
N = int(input())
cnt = 0
st_cnt = 0
end_cnt =0

for k in range(N):
    s = input()
    for i in range(len(s)):
        if i == (len(s)-1):
            if s[i]=='A':
                end_cnt += 1
        elif (i == 0) & (s[i]=='B'):
            st_cnt += 1
        elif s[i]=='A':
            if s[i+1]=='B':
                cnt += 1

print(cnt+min(st_cnt,end_cnt))

Submission status: Wrong Answer

Solution code:
n = int(input())

ab = 0
a = 0
b = 0
c = 0
for i in range(n):
    s = input()
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1
    if s[-1] == "A" and s[0] == "B":
        c += 1
    for i in range(len(s)-1):
        if s[i] == "A" and s[i+1] == "B":
            ab += 1
ans = ab + min(a,b)
if a == b == c:
    ans = ab + min(a,b) - 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

ab = 0
a = 0
b = 0
c = 0
for i in range(n):
    s = input()
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1
    if s[-1] == "A" and s[0] == "B":
        c += 1
    for i in range(len(s)-1):
        if s[i] == "A" and s[i+1] == "B":
            ab += 1
ans = ab + min(a,b)
if min(a,b) == c:
    ans = ab + min(a,b) - 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

ab = 0
a = 0
b = 0
c = 0
for i in range(n):
    s = input()
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1
    if s[-1] == "A" and s[0] == "B":
        c += 1
    for i in range(len(s)-1):
        if s[i] == "A" and s[i+1] == "B":
            ab += 1
ans = ab + min(min(a,b), ab + a+b-c-1)
if a == b == c == 1:
    ans = ab
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
ans=0
A=0
B=0
ab=0
for _ in range(N):
    S=input()
    ans+=S.count('AB')
    A+=S[-1]=='A'
    B+=S[0]=='B'
    ab+=(S[-1]=='A' and S[0]=='B')

if ab==A==B:
    ans-=1
print(ans+min(A,B))

Submission status: Wrong Answer

Solution code:
N=int(input())
ans=0
A=0
B=0
for _ in range(N):
    S=input()
    ans+=S.count('AB')
    A+=S[-1]=='A'
    B+=S[0]=='B'
print(ans+min(A,B))

Submission status: Wrong Answer

Solution code:
n=int(input())
c1=0
c2=0
c3=0
ans=0
for i in range(n):
    s=input()
    if s[0]=="B" and s[-1]=="A":
        c1+=1
    if s[0]=="B" and s[-1]!="A":
        c2+=1
    if s[0]!="B" and s[-1]=="A":
        c1+=1
    for j in range(len(s)-1):
        if s[j]=="A"and s[j+1]=="B":
            ans+=1
if c1==0:
    ans+=min(n-1,min(c2,c3))
elif c1!=0:
    if c2+c3>0:
        ans+=c1+min(c2,c3)
    if c2+c3==0:
        ans+=c1-1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from typing import Counter


N = int(input())
ss = []
for _ in range(N):
    s = input()
    ss.append(s)

inherent = 0
headB = 0
tailA = 0
bothBA = 0
for i in range(N):
    inherent += ss[i].count("AB")
    #print(i,ss[i][0],ss[i][-1])
    if(ss[i][0] == 'B'):
        if(ss[i][-1] == 'A'):
            bothBA += 1
        else:
            headB += 1
    else:
        if(ss[i][-1] == 'A'):
            tailA += 1

ans = inherent
if(min(tailA,headB) > 0):
    ans += bothBA + 1
    tailA -= 1
    headB -= 1
    bothBA = 0
if(headB > 0 and bothBA > 0):
    ans += bothBA
    headB -=1
if(tailA > 0 and bothBA > 0):
    ans += bothBA
    tailA -= 1

ans += min(headB,tailA)

print(ans)

Submission status: Wrong Answer

Solution code:
from typing import Counter


N = int(input())
ss = []
for _ in range(N):
    s = input()
    ss.append(s)

inherent = 0
headB = 0
tailA = 0
bothBA = 0
for i in range(N):
    if(len(ss[i]) > 2):
        inherent += ss[i].count("AB")
    #print(i,ss[i][0],ss[i][-1])
    if(ss[i][0] == 'B'):
        if(ss[i][-1] == 'A'):
            bothBA += 1
        else:
            headB += 1
    else:
        if(ss[i][-1] == 'A'):
            tailA += 1

ans = inherent
ans += 2 * min(headB,tailA,bothBA)
ans += max(bothBA - min(headB,tailA),0)
ans += max(min(headB,tailA) - bothBA,0)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
both = 0
start_B = 0
end_A = 0
for _ in range(N):
    s = input()
    ans += s.count("AB")
    isB = s.startswith("B")
    isA = s.endswith("A")
    if isA and isB:
        both += 1
    if isA:
        end_A += 1
    if isB:
        start_B += 1
ans += min(start_B, end_A) - both
ans += both
if start_B == both and both == end_A:
    ans = max(0, ans - 1)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
both = 0
start_B = 0
end_A = 0
for _ in range(N):
    s = input()
    ans += s.count("AB")
    isB = s.startswith("B")
    isA = s.endswith("A")
    if isA and isB:
        both += 1
    if isA:
        end_A += 1
    if isB:
        start_B += 1
ans += min(start_B, end_A) - both
ans += both
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
ans=0
cnt_a,cnt_b,cnt_c=0,0,0
for i in range(N):
    s=input()
    ans+=s.count("AB")
    s=s.replace("AB","")
    if s[0]=="B" and s[-1]=="A":
        cnt_c+=1
    elif s[0]=="B":
        cnt_b+=1
    elif s[-1]=="A":
        cnt_a+=1
for i in range(cnt_c+1):
    if cnt_a>cnt_b:
        cnt_b+=1
    else:
        cnt_a+=1
print(ans+min(cnt_a,cnt_b))