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
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
import re
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    s = SR(n)
    a = 0
    b = 0
    ab = 0
    ans = 0
    for si in s:
        if si[0] == "B" and si[-1] == "A":
            ab += 1
        elif si[0] == "B":
            b += 1
        elif si[-1] == "A":
            a += 1
        ans += si.count("AB")
    ans += min(a, b)
    x = max(a, b) - min(a, b)
    ans += min(x, ab)
    ab = ab - x
    if ab:
        ans += ab - 1
    print(ans)

    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
import re
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    s = SR(n)
    a = 0
    b = 0
    ab = 0
    ans = 0
    for si in s:
        if si[0] == "B" and si[-1] == "A":
            ab += 1
        elif si[0] == "B":
            b += 1
        elif si[-1] == "A":
            a += 1
        ans += si.count("AB")
    ans += min(a, b)
    x = max(a, b) - min(a, b)
    ans += min(x,ab)
    print(ans)

    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys


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


def main():
    ans = 0
    A = []
    B = []
    N = int(input())

    for _ in range(N):
        s = input()
        l = len(s)
        for i in range(l - 1):
            if s[i : i + 2] == "AB":
                ans += 1
        if s[0] == "B":
            B.append(1)
        else:
            B.append(0)
        if s[-1] == "A":
            A.append(1)
        else:
            A.append(0)
    if A == B:
        ans += sum(A) - 1
    elif N == 1:
        ans = ans
    else:
        ans += min(sum(A), sum(B))
    return ans


if __name__ == "__main__":
    ans = main()
    print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
import re
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    s = SR(n)
    a = 0
    b = 0
    ans = 0
    for si in s:
        if si[0] == "B":
            if a:
                a -= 1
                ans += 1
        elif si[-1] == "A":
            if b:
                b -= 1
                ans += 1
        a += si[-1] == "A"
        b += si[0] == "B"
        ans += si.count("AB")
    print(ans)

    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
import re
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    s = SR(n)
    a = 0
    b = 0
    ans = 0
    for si in s:
        if si[0] == "B":
            if a:
                a -= 1
                ans += 1
        if si[-1] == "A":
            if b:
                b -= 1
                ans += 1
        a += si[-1] == "A"
        b += si[0] == "B"
        ans += si.count("AB")
    print(ans)

    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys


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


def main():
    ans = 0
    A = []
    B = []
    N = int(input())

    for _ in range(N):
        s = input()
        l = len(s)
        for i in range(l - 1):
            if s[i : i + 2] == "AB":
                ans += 1
        if s[0] == "B":
            B.append(1)
        else:
            B.append(0)
        if s[-1] == "A":
            A.append(1)
        else:
            A.append(0)
    if A == B:
        ans += sum(A) - 1
    else:
        ans += min(sum(A), sum(B))
    return ans


if __name__ == "__main__":
    ans = main()
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys


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


def main():
    ans = 0
    A = 0
    B = 0
    N = int(input())

    for _ in range(N):
        s = input()
        l = len(s)
        for i in range(l - 1):
            if s[i : i + 2] == "AB":
                ans += 1
        if s[0] == "B":
            B += 1
        if s[-1] == "A":
            A += 1
    if A == B:
        ans += A - 1
    else:
        ans += min(A, B)
    return ans


if __name__ == "__main__":
    ans = main()
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys


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


def main():
    ans = 0
    A = 0
    B = 0
    N = int(input())

    for _ in range(N):
        s = input()
        l = len(s)
        for i in range(l - 1):
            if s[i : i + 2] == "AB":
                ans += 1
        if s[0] == "B":
            B += 1
        if s[-1] == "A":
            A += 1
    ans += min(A, B)
    return ans


if __name__ == "__main__":
    ans = main()
    print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
ans = 0
fin_with_a = 0
start_with_b = 0
for _ in range(n):
    tmp = input()
    s.append(tmp[0]+tmp[-1])
    for i in range(len(tmp)-1):
        if tmp[i:i+2] ==  'AB':
            ans += 1
    if tmp[0] == 'B':
        if fin_with_a > 0:
            ans += 1
            fin_with_a -= 1
        else:
            start_with_b += 1
    if tmp[-1] == 'A':
        if tmp[0] == 'B' and start_with_b == 1:
            fin_with_a += 1
            continue
        if start_with_b > 0:
            ans += 1
            start_with_b -= 1
        else:
            fin_with_a += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
ans = 0
fin_with_a = 0
start_with_b = 0
for _ in range(n):
    tmp = input()
    s.append(tmp[0]+tmp[-1])
    for i in range(len(tmp)-1):
        if tmp[i:i+2] ==  'AB':
            ans += 1
    if tmp[0] == 'B':
        if fin_with_a > 0:
            ans += 1
            fin_with_a -= 1
        else:
            start_with_b += 1
    if tmp[-1] == 'A':
        if start_with_b > 0:
            ans += 1
            start_with_b -= 1
        else:
            fin_with_a += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
ret = 0
cnt_ab = 0
cnt_a = 0
cnt_b = 0

for i in range(n):
    s = input()
    ret += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        cnt_ab += 1
    elif s[0] == 'B':
        cnt_b += 1
    elif s[-1] == 'A':
        cnt_a += 1

ret += max((cnt_ab - 1), 0)
ret += min(cnt_a, cnt_b)
if cnt_ab > 0 and abs(cnt_a - cnt_b) > 0:
    ret += 1

print(ret)

Submission status: Wrong Answer

Solution code:
n = int(input())
naibu = 0
head = 0
tail = 0
both = 0

for _ in range(n):
    s = str(input())
    naibu += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        both += 1
    elif s[0] == 'B':
        head += 1
    elif s[-1] == 'A':
        tail += 1

ans = min(head, tail) + both + naibu
if head == 0 and tail == 0:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
naibu = 0
head = 0
tail = 0
both = 0

for _ in range(n):
    s = str(input())
    naibu += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        both += 1
    elif s[0] == 'B':
        head += 1
    elif s[-1] == 'A':
        tail += 1

ans = min(head, tail) + both + naibu
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

A = []
t = 0
a = 0
b = 0
for i in range(n):
    s = input()
    t += s.count("AB")
    if s[0] == 'B':
        b += 1
    if s[-1] == 'A':
        a += 1
if a != b:
    print(t + min(a, b))
else:
    print(t + min(a, b) - 1)

Submission status: Wrong Answer

Solution code:
N = int(input())

tail_a = 0
head_b = 0
cnt_ab = 0
for _ in range(N):
    s = input()
    if s[0] == 'B':
        head_b += 1
    if s[-1] == 'A':
        tail_a += 1
    cnt_ab += s.count('AB')

ret = cnt_ab + min(tail_a, head_b)
print(ret)

Submission status: Wrong Answer

Solution code:
N = int(input())

count_ab = 0
count_start_b = 0
count_finish_a = 0
count_start_b_finish_a = 0
for i in range(N):
    s = input()
    count_ab += s.count('AB')
    # print(s, s[0], s[-1])
    if s[0] == 'B' and s[-1] == 'A':
        count_start_b_finish_a += 1
    if s[0] == 'B':
        count_start_b += 1
    if s[-1] == 'A':
        count_finish_a += 1

if min(count_finish_a, count_start_b) == 1 and count_start_b_finish_a == 1:
    print(count_ab)
else:
    print(count_ab + min(count_finish_a, count_start_b))

Submission status: Wrong Answer

Solution code:
N = int(input())

count_ab = 0
count_start_b = 0
count_finish_a = 0
for i in range(N):
    s = input()
    count_ab += s.count('AB')
    # print(s, s[0], s[-1])
    if s[0] == 'B':
        count_start_b += 1
    if s[-1] == 'A':
        count_finish_a += 1
print(count_ab + min(count_finish_a, count_start_b))

Submission status: Wrong Answer

Solution code:
n = int(input())

ans = 0

BA = 0
XA = 0
BX = 0

for i in range(n):
    s = input()
    if 'AB' in s:
        ans += 1

    if s[0] == 'B' and s[-1] == 'A':
        BA += 1
    elif s[-1] == 'A':
        XA += 1
    elif s[0] == 'B':
        BX += 1

if BA > 0:
    ans += BA-1
    BA = 1
    if BX > 0:
        ans += 1
        BA = 0
    else:
        BX = 1
        BA = 0

ans += min(XA, BX)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

n, *s = sys.stdin.readlines()
a = 0
b = 0
ab = 0
ans = 0
for i in s:
    if i[0] == 'B' and i[-2] == 'A':
        ab += 1
    elif i[0] == 'B':
        b += 1
    elif i[-2] == 'A':
        a += 1
    ans += i.count('AB')
connect = min(a, b)
if a == 0 and b == 0:
    ab -= 1
ans += ab + connect
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

n, *s = sys.stdin.readlines()
a = 0
b = 0
ans = 0
for i in s:
    if i[0] == 'B':
        b += 1
    if i[-2] == 'A':
        a += 1
    ans += i.count('AB')
connect = min(a, b)
if a == 0 or b == 0:
    connect = 0
if connect == n:
    connect -= 1
ans += connect
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

n, *s = sys.stdin.readlines()
a = 0
b = 0
ans = 0
for i in s:
    if i[0] == 'B':
        b += 1
    if i[-2] == 'A':
        a += 1
    ans += i.count('AB')
connect = min(a, b)
if connect == n:
    connect -= 1
ans += connect
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = 0
B = 0
AB = 0
ans = 0
for _ in range(N):
    s = list(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:i + 2] == list("AB"):
            ans += 1
if A == 0 and B == 0:
    ans -= 1
print(ans + AB + min(A, B))

Submission status: Wrong Answer

Solution code:
n = int(input())
count = 0

a, b, ab = 0, 0, 0
for _ in range(n):
    s = input()
    if s[-1] == "A" and s[0] == "B":
        ab += 1
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1


    for i in range(len(s)-1):
        if s[i:i+2] == "AB":
            count += 1

count += min(a, b)
if max(a, b) == ab:
    count -= 1

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
count = 0

a, b, ab = 0, 0, 0
for _ in range(n):
    s = input()
    if s[-1] == "A" and s[0] == "B":
        ab += 1
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1

    for i in range(len(s)-1):
        if s[i:i+2] == "AB":
            count += 1
            
count += min(a, b)
if b == ab:
    count -= 1

print(count)

Submission status: Wrong Answer

Solution code:
import bisect
# from collections import Counter, defaultdict, deque, namedtuple
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
    n = int(input())
    b_ｓｔart_count = 0
    a_end_count = 0
    ab_count = 0

    for i in range(n):
        s = sys.stdin.readline().rstrip("\n")
        len_s = len(s)
        for j in range(len_s-1):
            if s[j] == "A":
                if s[j+1] == "B":
                    ab_count += 1
        if s[0] == "B":
            b_ｓｔart_count += 1
        if s[-1] == "A":
            a_end_count += 1

    ab_count += min(a_end_count, b_ｓｔart_count)
    print(ab_count)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
numA = 0
numB = 0
cnt = 0
sm = 0

for i in range(N):
    mm = input()
    if mm[0]=="B":
        numB += 1
    if mm[-1]=="A":
        numA += 1
    if mm[0]=="B" and mm[-1]=="A":
        sm += 1

    for i in range(len(mm)):
        if mm[i:i+2]=="AB":
            cnt += 1

if sm == numA and sm==numB:
    numA -= 1
    numB -= 1
mm2 = min(numA,numB,N-1)
print(cnt+mm2)

Submission status: Wrong Answer

Solution code:
N = int(input())
numA = 0
numB = 0
cnt = 0
sm = 0

for i in range(N):
    mm = input()
    if mm[0]=="B":
        numB += 1
    if mm[-1]=="A":
        numA += 1
    if mm[0]=="B" and mm[-1]=="A":
        sm = 1

    for i in range(len(mm)):
        if mm[i:i+2]=="AB":
            cnt += 1

mm2 = min(numA-sm,numB-sm,N-1)
print(cnt+mm2)

Submission status: Wrong Answer

Solution code:
N = int(input())
numA = 0
numB = 0
cnt = 0

for i in range(N):
    mm = input()
    if mm[0]=="B":
        numB += 1
    if mm[-1]=="A":
        numA += 1

    for i in range(len(mm)):
        if mm[i:i+2]=="AB":
            cnt += 1

mm2 = min(numA,numB,N-1)
print(cnt+mm2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())

S = []

for _ in range(N):
    S.append(input())


# internal ab count
internal_ab = 0
for s in S:
    for i in range(1,len(s)):
        if s[i-1] == 'A' and s[i] == 'B':
            internal_ab += 1

# edge
a_pool = 0
b_pool = 0
edge_ab = 0
for s in S:
    if s[0] == 'B' and s[-1] == 'A':
        if b_pool != 0 or a_pool != 0:
            edge_ab += 1
        else:
            a_pool += 1
            b_pool += 1
    elif s[0] == 'B':
        if a_pool != 0:
            edge_ab += 1
            a_pool -= 1
        else:
            b_pool += 1
    elif s[-1] == 'A':
        if b_pool != 0:
            edge_ab += 1
            b_pool -= 1
        else:
            a_pool += 1
    #print(f'{s=} {a_pool=} {b_pool=}')

#print(f'{edge_ab=}')
#print(f'{internal_ab=}')
print(internal_ab+edge_ab)

Submission status: Wrong Answer

Solution code:
N=int(input())
S=[input() for i in range(N)]
BXA=0;XA=0;BX=0;ans=0;
for s in S:
  ans+=s.count("AB")
  if(s[0]=='B' and s[-1]=='A'):
    BXA+=1
  elif(s[0]=='B'):
    BX+=1
  elif(s[-1]=='A'):
    XA+=1
#print(ans,min([XA,BX]))
if min([XA,BX])>0:
  ans+=min([XA,BX])+BXA
elif max([XA,BX])>0:
  ans+=BXA
else:
  ans+=BXA-1
print(ans)

Submission status: Wrong Answer

Solution code:
#降参

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

import re
cnt = 0
for i in range(n):#ABでなくAAだと無理だがこれで
    cnt += len(re.findall("AB",s[i]))
    si = s[i].replace("AB","")
    s[i] = si

ac= 0
bc = 0
abc = 0
for i in range(n):
    if s[i]:
        if s[i][-1]=="A":
            ac+=1
        if s[i][0]=="B":
            bc+=1
        if  s[i][-1]=="A" and s[i][0]=="B":
            abc+=1

#これ全然正しいこと分からんのだけど
ac-=abc
bc-=abc
cnt += min(ac,bc)
if min(ac,bc):
    cnt += abc
else:
    cnt += max(abc-1,0)
print(cnt)

Submission status: Wrong Answer

Solution code:
#降参

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

import re
cnt = 0
for i in range(n):#ABでなくAAだと無理だがこれで
    cnt += len(re.findall("AB",s[i]))
    si = s[i].replace("AB","")
    s[i] = si

ac= 0
bc = 0
abc = 0
for i in range(n):
    if s[i]:
        if s[i][-1]=="A":
            ac+=1
        if s[i][0]=="B":
            bc+=1
        if  s[i][-1]=="A" and s[i][0]=="B":
            abc+=1

#これ全然正しいこと分からんのだけど
ac-=abc
bc-=abc
cnt += min(ac,bc)
if min(ac,bc):
    cnt += abc
else:
    cnt += abc-1
print(cnt)

Submission status: Wrong Answer

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

import re
cnt = 0
for i in range(n):#ABでなくAAだと無理だがこれで
    cnt += len(re.findall("AB",s[i]))
    si = s[i].replace("AB","")
    s[i] = si

ac= 0
bc = 0
abc = 0
for i in range(n):
    if s[i]:
        if s[i][-1]=="A":
            ac+=1
        if s[i][0]=="B":
            bc+=1
        if  s[i][-1]=="A" and s[i][0]=="B":
            abc+=1
cnt += min(ac,bc)
#以下ないと輪になる
#これ1発で当てられる気がしない
if abc==bc==ac:
    cnt -= 1
print(cnt)

Submission status: Wrong Answer

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

import re
cnt = 0
for i in range(n):#ABでなくAAだと無理だがこれで
    cnt += len(re.findall("AB",s[i]))
    si = s[i].replace("AB","")
    s[i] = si

ac= 0
bc = 0
for i in range(n):
    if s[i]:
        if s[i][-1]=="A":
            ac+=1
        if s[i][0]=="B":
            bc+=1
cnt += min(ac,bc)
#以下ないと輪になる
if ac==bc:
    cnt -= 1
print(cnt)

Submission status: Wrong Answer

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

import re
cnt = 0
for i in range(n):#ABでなくAAだと無理だがこれで
    cnt += len(re.findall("AB",s[i]))
    si = s[i].replace("AB","")
    s[i] = si

ac= 0
bc = 0
for i in range(n):
    if s[i][-1]=="A":
        ac+=1
    if s[i][0]=="B":
        bc+=1
cnt += min(ac,bc)
#以下ないと輪になる
if min(ac,bc)==n:
    cnt -= 1
print(cnt)

Submission status: Wrong Answer

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

import re
cnt = 0
for i in range(n):#ABでなくAAだと無理だがこれで
    cnt += len(re.findall("AB",s[i]))
    si = s[i].replace("AB","")
    s[i] = si

ac= 0
bc = 0
for i in range(n):
    if s[i][-1]=="A":
        ac+=1
    if s[i][0]=="B":
        bc+=1
cnt += min(ac,bc)
print(cnt)

Submission status: Wrong Answer

Solution code:
N=int(input())
S=[input() for i in range(N)]
BA=0
B=0
A=0
cnt=0
for i in S:
  if i[0]=='B'and i[-1]=='A':
    BA+=1
  elif i[0]=='B':
    B+=1
  elif i[-1]=='A':
    A+=1
  for j in range(len(i)-1):
    if i[j]=='A' and i[j+1]=='B':
      cnt+=1
ans=BA-1
if B>0:
  ans+=1
  B-=1
if A>0:
  ans+=1
  A-=1
ans+=min(A,B)
print(max(ans+cnt,0))

Submission status: Wrong Answer

Solution code:
N=int(input())
S=[input() for i in range(N)]
BA=0
B=0
A=0
cnt=0
for i in S:
  if i[0]=='B'and i[-1]=='A':
    BA+=1
  elif i[0]=='B':
    B+=1
  elif i[-1]=='A':
    A+=1
  for j in range(len(i)-1):
    if i[j]=='A' and i[j+1]=='B':
      cnt+=1
ans=max(BA-1,0)
if B>0:
  ans+=1
  B-=1
if A>0:
  ans+=1
  A-=1
ans+=min(A,B)
print(ans+cnt)

Submission status: Wrong Answer

Solution code:
N=int(input())
S=[input() for i in range(N)]
BA=0
B=0
A=0
cnt=0
for i in S:
  if i[0]=='B'and i[-1]=='A':
    BA+=1
  elif i[0]=='B':
    B+=1
  elif i[-1]=='A':
    A+=1
  for j in range(len(i)-1):
    if i[j]=='A' and i[j+1]=='B':
      cnt+=1
ans=BA-1
if B>0:
  ans+=1
  B-=1
if A>0:
  ans+=1
  A-=1
ans+=min(A,B)
print(ans+cnt)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
cntAB = 0
cntA = 0
cntB = 0
BcntA = 0
for i in range(N):
  s = input()
  if s[0] == "B" and s[-1] == "A":
    BcntA += 1
  elif s[0] == "B" and s[-1] != "A":
    cntB += 1
  elif s[0] != "B" and s[-1] == "A":
    cntA += 1
  for j in range(1,len(s)):
    if s[j-1] == "A" and s[j] == "B":
      cntAB += 1
ans = 0
if cntA <= cntB:
  ans += cntA
  cntB -= cntA
  cntA = 0
else:
  ans += cntB
  cntA -= cntB
  cntB = 0
if BcntA!=0:
  if cntA+cntB>0:
    print(cntAB+ans+min(cntA,cntB)+BcntA)
  else:
    print(cntAB+ans+BcntA-1)
else:  
  print(cntAB+ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
cntAB = 0
cntA = 0
cntB = 0
BcntA = 0
for i in range(N):
  s = input()
  if s[0] == "B" and s[-1] == "A":
    BcntA += 1
  elif s[0] == "B" and s[-1] != "A":
    cntB += 1
  elif s[0] != "B" and s[-1] == "A":
    cntA += 1
  for j in range(1,len(s)):
    if s[j-1] == "A" and s[j] == "B":
      cntAB += 1
ans = 0
if cntA <= cntB:
  ans += cntA
  cntB -= cntA
  cntA = 0
else:
  ans += cntB
  cntA -= cntB
  cntB = 0
if BcntA >= max(cntA, cntB):
  if BcntA-max(cntA,cntB)>=1:
    print(cntAB+ans+max(cntA,cntB)+BcntA-1)
  else:
    print(cntAB+ans+max(cntA, cntB))
else:  
  print(cntAB+ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
cntAB = 0
cntA = 0
cntB = 0
BcntA = 0
for i in range(N):
  s = input()
  if s[0] == "B" and s[-1] == "A":
    BcntA += 1
  elif s[0] == "B" and s[-1] != "A":
    cntB += 1
  elif s[0] != "B" and s[-1] == "A":
    cntA += 1
  for j in range(1,len(s)):
    if s[j-1] == "A" and s[j] == "B":
      cntAB += 1
if BcntA >= 2:
  cntAB += BcntA//2
ans = 0
if cntA <= cntB:
  ans += cntA
  cntB -= cntA
  cntA = 0
else:
  ans += cntB
  cntA -= cntB
  cntB = 0
if BcntA >= max(cntA, cntB):
  print(cntAB+ans+max(cntA, cntB))
else:  
  print(cntAB+ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
cntAB = 0
cntA = 0
cntB = 0
BcntA = 0
for i in range(N):
  s = input()
  if s[0] == "B" and s[-1] == "A":
    BcntA += 1
  elif s[0] == "B" and s[-1] != "A":
    cntB += 1
  elif s[0] != "B" and s[-1] == "A":
    cntA += 1
  for j in range(1,len(s)):
    if s[j-1] == "A" and s[j] == "B":
      cntAB += 1
if BcntA >= 2:
  cntAB += BcntA//2
  BcntA //= 2
if min(cntA, cntB)+ BcntA <= max(cntA, cntB):
  print(cntAB+min(cntA,cntB)+BcntA)
else:  
  print(cntAB+min(cntA,cntB))

Submission status: Wrong Answer

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

ans,acnt,bcnt,abcnt=0,0,0,0
for s in S:
  ans+=s.count('AB')
  if s[-1]=='A': acnt+=1
  if s[0]=='B': bcnt+=1
  if s[-1]=='A' and s[0]=='B': abcnt+=1
if acnt==0:
  print(ans+min(bcnt,abcnt))
else:
  if bcnt+abcnt>0:
    print(ans+acnt+min(bcnt,abcnt))
  if bcnt+abcnt==0:
    print(ans+acnt-1)

Submission status: Wrong Answer

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

ans,acnt,bcnt,abcnt=0,0,0,0
for s in S:
  ans+=s.count('AB')
  if s[-1]=='A': acnt+=1
  if s[0]=='B': bcnt+=1
  if s[-1]=='A' and s[0]=='B': abcnt+=1
print(ans+min(acnt,bcnt,abcnt))

Submission status: Wrong Answer

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

ans,acnt,bcnt=0,0,0
for s in S:
  ans+=s.count('AB')
  if s[-1]=='A': acnt+=1
  if s[0]=='B': bcnt+=1
print(ans+min(acnt,bcnt))

Submission status: Wrong Answer

Solution code:
import sys
#input = sys.stdin.buffer.readline

def main():
    N = int(input())
    AB = [0,0,0]
    ans = 0
    for _ in range(N):
        s = input()
        l = len(s)
        if s[0] == "B" and s[-1] == "A":
            AB[2] += 1
        else:
            if s[0] == "B":
                AB[1] += 1
            if s[-1] == "A":
                AB[0] += 1
        for i in range(l-1):
            if s[i] == "A" and s[i+1] == "B":
                ans += 1

    if AB[0]+AB[1] == 0:
        print(ans+AB[2]-1)
    else:
        print(ans+min(AB[0],AB[1])+AB[2])
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
#input = sys.stdin.buffer.readline

def main():
    N = int(input())
    AB = [0,0]
    ans = 0
    for _ in range(N):
        s = input()
        l = len(s)
        if s[0] == "B":
            AB[1] += 1
        if s[-1] == "A":
            AB[0] += 1
        for i in range(l-1):
            if s[i] == "A" and s[i+1] == "B":
                ans += 1
    
    if AB[0] == AB[1] == N:
        print(ans+N-1)
    else:
        print(ans+min(AB))
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
#input = sys.stdin.buffer.readline

def main():
    N = int(input())
    AB = [0,0]
    ans = 0
    for _ in range(N):
        s = input()
        l = len(s)
        if s[0] == "B":
            AB[1] += 1
        if s[-1] == "A":
            AB[0] += 1
        for i in range(l-1):
            if s[i] == "A" and s[i+1] == "B":
                ans += 1
    
    print(ans+min(AB))
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
S=[input() for _ in range(N)]
AB=sum(s.count("AB") for s in S)
EA=sum(s.endswith("A") for s in S)
SB=sum(s.startswith("B") for s in S)
EASB=sum(s.endswith("A") and s.startswith("B") for s in S)
K=min(EA,SB)-(EA==SB==EASB)
print(AB+K)

Submission status: Wrong Answer

Solution code:
N=int(input())
S=[input() for _ in range(N)]
AB=sum(s.count("AB") for s in S)
EA=sum(s.endswith("A") for s in S)
SB=sum(s.startswith("B") for s in S)
K=min(N-1,EA,SB)
print(AB+K)

Submission status: Wrong Answer

Solution code:
N = int(input())

a, b, c, d = 0, 0, 0, 0
for _ in range(N):
    s = input()
    if s[0]=="B" and s[-1]=="A":
        a += 1
    elif s[0]=="B":
        b += 1
    elif s[-1]=="A":
        c += 1
    d += s.count("AB")

ans = d + max(0, a-1) + min(b, c)
if b!=c and a and b+c>0:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[input() for i in range(n)]
count=0
A=0
B=0
BA=0
for i in range(n):
    for j in range(len(s[i])-1):
        if j==0:
            if s[i][j]=='B':
                B+=1
        if j==len(s[i])-2:
            if s[i][j+1]=='A':
                A+=1
        if s[i][j:j+2]=='AB':
            count+=1
    if s[i][0]=='B' and s[i][-1]=='A':
        BA+=1
        A-=1
        B-=1
        
if BA==0:
    print(count+min(A,B))
else:
    ans=count+BA-1
    if A>=1 and B>=1:
        print(ans+2)
    elif A>=1 or B>=1:
        print(ans+1)
    else:
        print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

a, b, c, d = 0, 0, 0, 0
for _ in range(N):
    s = input()
    if s[0]=="B" and s[-1]=="A":
        a += 1
    elif s[0]=="B":
        b += 1
    elif s[-1]=="A":
        c += 1
    d += s.count("AB")

ans = d + max(0, a-1) + min(b, c)
if b!=c and a and b and c:
    ans += 1
print(ans)