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())
s = [input() for _ in range(n)]

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

a -= b_a
b -= b_a
print(ab + min(a, b) + b_a)

Submission status: Wrong Answer

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

ab, a, b, b_a = 0, 0, 0, 0
for i in s:
  if 'AB' in i:
    ab += 1
  if i[-1] == 'A':
    a += 1
  if i[0] == 'B':
    b += 1
  if i[0] == 'B' and i[-1] == 'A':
    b_a +=1

a -= b_a
b -= b_a
print(ab + min(a, b) + b_a)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input().strip() for _ in range(N)]
C = {"A":0,"B":0,"BA":0}
cnt = 0
for i in range(N):
    s = S[i]
    for j in range(len(s)-2+1):
        if s[j:j+2]=="AB":
            cnt += 1
    if s[0]=="B" and s[-1]=="A":
        C["BA"] += 1
    elif s[0]=="B":
        C["B"] += 1
    elif s[-1]=="A":
        C["A"] += 1
if C["BA"]>0:
    cnt += C["BA"]-1
cnt += min(C["B"],C["A"])
if C["BA"]>0 and C["B"]!=C["A"]:
    cnt += 1
print(cnt)

Submission status: Wrong Answer

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

ab, a, b, b_a = 0, 0, 0, 0
for i in s:
  if 'AB' in i:
    ab += 1
  if i[-1] == 'A':
    a += 1
  if i[0] == 'B':
    b += 1
  if i[0] == 'B' and i[-1] == 'A':
    b_a +=1

a -= b_a
b -= b_a
sub = max(a, b) - min(a, b)
if sub < b_a:
  print(ab + max(a, b) + b_a-sub)
else:
  print(ab + min(a, b) + b_a)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
for i in range(N):
    A.append(input())


A_end = 0
B_begin = 0
ab_cnt = 0
for a in A:
    if 'AB' in a:
        ab_cnt += 1
    if a[-1] == 'A':
        A_end += 1
    if a[0] == 'B':
        B_begin += 1
ans = min(A_end, B_begin) + ab_cnt
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
A = 0; B = 0; AB = 0
for i in range(N):
  s = str(input()); n =len(s)
  for i in range(n-1):
    if s[i:i+2] == "AB":
      ans += 1  
  if s[0] == "B" and s[-1] == "A":
    AB += 1
  elif s[0] == "B":
    B += 1
  elif s[-1] == "A":
    A += 1

#print(ans,A,B,AB)
if AB > 0:
  ans += AB-1
  if A == 0:
    temp = min(B,AB)
  elif B == 0:
    temp = min(A,AB)
  else:
    temp = min(A,B)+1
  ans += temp

else:
  ans += min(A,B)

print(ans)

Submission status: Wrong Answer

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

ab, a, b, b_a = 0, 0, 0, 0
for i in s:
  if 'AB' in i:
    ab += 1
  if i[-1] == 'A':
    a += 1
  if i[0] == 'B':
    b += 1
  if i[0] == 'B' and i[-1] == 'A':
    b_a +=1

if b_a > 1:
  ab += b_a - 1
a -= b_a
b -= b_a
sub = max(a, b) - min(a, b)
if sub < b_a:
  print(ab + max(a, b) + (b_a-sub)//2)
else:
  print(ab + min(a, b) + b_a)

Submission status: Wrong Answer

Solution code:
n = int(input())
ab, tail_a, head_b, th_a = 0, 0, 0, 0

for _ in range(n):
    s = input()
    ab += s.count('AB')
    if s[-1] == 'A' and s[0] == 'B': th_a += 1
    elif s[-1] == 'A': tail_a += 1
    elif s[0] == 'B': head_b += 1

if min(tail_a, head_b) + th_a == n:
    print(ab + min(tail_a, head_b) + th_a - 1)
else:
    print(ab + min(tail_a, head_b) + th_a)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
A = 0; B = 0
for i in range(N):
  s = str(input()); n =len(s)
  for i in range(n-1):
    if s[i:i+2] == "AB":
      ans += 1  
  if s[0] == "B":
    B += 1
  if s[-1] == "A":
    A += 1

#print(ans,A,B)
ans += min(A,B)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
ab, tail_a, head_b = 0, 0, 0

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

if min(tail_a, head_b) == n:
    print(ab + min(tail_a, head_b) - 1)
else:
    print(ab + min(tail_a, head_b))

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし


N = I()

ans = 0
b_start,a_end = 0,0
for i in range(N):
    s = S()
    if s[0] == 'B':
        b_start += 1
    if s[-1] == 'A':
        a_end += 1
    for j in range(len(s)-1):
        if s[j] == 'A' and s[j+1] == 'B':
            ans += 1

print(ans+min(b_start,a_end))

Submission status: Wrong Answer

Solution code:
import sys
stdin=sys.stdin

ip=lambda: int(sp())
fp=lambda: float(sp())
lp=lambda:list(map(int,stdin.readline().split()))
sp=lambda:stdin.readline().rstrip()

n=ip()
a=[]
b=[]
ans=0
for i in range(n):
  s=sp()
  if s[0]=='B':
    b.append(i)
  if s[-1]=='A':
    a.append(i)
  for j in range(len(s)-1):
    if s[j]=='A' and s[j+1]=='B':
      ans+=1
      
c=len(list(set(a)&set(b)))
a_rest=len(a)-c
b_rest=len(b)-c
if c==0:
  omake=min(a_rest,b_rest)
else:
  if a_rest==b_rest==0:
    omake=c-1
  else:
    omake=c+min(a_rest,b_rest)+min((max(a_rest,b_rest)-min(a_rest,b_rest)),1)
  
print(ans+omake)

Submission status: Wrong Answer

Solution code:
import sys
stdin=sys.stdin

ip=lambda: int(sp())
fp=lambda: float(sp())
lp=lambda:list(map(int,stdin.readline().split()))
sp=lambda:stdin.readline().rstrip()

n=ip()
a=[]
b=[]
ans=0
for i in range(n):
  s=sp()
  if s[0]=='B':
    b.append(i)
  if s[-1]=='A':
    a.append(i)
  for j in range(len(s)-1):
    if s[j]=='A' and s[j+1]=='B':
      ans+=1
      
c=len(list(set(a)&set(b)))
a_rest=len(a)-c
b_rest=len(b)-c
if c==0:
  omake=min(a_rest,b_rest)
else:
  omake=c-1+min(a_rest,b_rest)+min((max(a_rest,b_rest)-min(a_rest,b_rest)),1)
  
print(ans+omake)

Submission status: Wrong Answer

Solution code:
n = int(input())
sm = 0
b_first = 0
a_end = 0
b_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":
            sm += 1
    
    if s[0]=="B": b_first += 1
    if s[-1]=="A": a_end += 1
    if s[0]=="B" and s[-1]=="A": b_a += 1

ans = sm + min(b_first, a_end)

if b_first==b_a and b_first==a_end:
    ans -=1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin=sys.stdin

ip=lambda: int(sp())
fp=lambda: float(sp())
lp=lambda:list(map(int,stdin.readline().split()))
sp=lambda:stdin.readline().rstrip()

n=ip()
a=[]
b=[]
ans=0
for i in range(n):
  s=sp()
  if s[0]=='B':
    b.append(i)
  if s[-1]=='A':
    a.append(i)
  for j in range(len(s)-1):
    if s[j]=='A' and s[j+1]=='B':
      ans+=1
      
c=len(list(set(a)&set(b)))
a_rest=len(a)-c
b_rest=len(b)-c
if c==0:
  omake=min(a_rest,b_rest)
else:
  omake=c-1+min(a_rest,b_rest)+min((max(a_rest,b_rest)-min(a_rest,b_rest)),1)
  
print(ans+omake)

Submission status: Wrong Answer

Solution code:
import sys
stdin=sys.stdin

ip=lambda: int(sp())
fp=lambda: float(sp())
lp=lambda:list(map(int,stdin.readline().split()))
sp=lambda:stdin.readline().rstrip()

n=ip()
a=[]
b=[]
ans=0
for i in range(n):
  s=sp()
  if s[0]=='B':
    b.append(i)
  if s[-1]=='A':
    a.append(i)
  for j in range(len(s)-1):
    if s[j]=='A' and s[j+1]=='B':
      ans+=1
      
if len(a)==1 and len(b)==1:
  if a[0]==b[0]:
    print(ans)
  else:
    print(ans+1)
    
else:
  print(ans+min(len(a),len(b)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,*s=open(0).read().split()
print(sum(S.count('AB')for S in s)+min(int(N)-1,sum(S[-1].count('A')for S in s),sum(S[0].count('B')for S in s)))

Submission status: Wrong Answer

Solution code:
N,*s=open(0).read().split()
print(sum(S.count('AB')for S in s)+min(sum(S[-1].count('A')for S in s),sum(S[0].count('B')for S in s)))

Submission status: Wrong Answer

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    
    n = I()
    ans = 0
    x, y, z = 0, 0, 0

    for _ in range(n):
        s = S()
        ans += s.count("AB")
        if s[0]=="B" or s[-1]=="A":
            if s[0]=="B" and s[-1]=="A":
                x += 1
            elif s[0]=="B":
                z += 1
            else:
                y += 1
    
    if x==0:
        ans += min(y,z)
    else:
        if y==0 or x==0:
            if y==0 and x==0:
                ans += x-1
            else:
                ans += x
        else:
            ans += x + 1 + min(y-1, z-1)

    print(ans)
                


main()

Submission status: Wrong Answer

Solution code:
n = int(input())

kosu = [0,0,0]
ans = 0
for i in range(n):
    s = input()
    ans += s.count('AB')
    if s[0]=="B" and s[-1]=="A":
        kosu[2] += 1
        continue
    if s[0] == "B":
        kosu[1] += 1
    if s[-1] == "A":
        kosu[0] += 1

ans += min(kosu[0], kosu[1])

if abs(kosu[0]-kosu[1]) > 0:
    ans += kosu[2]
else:
    ans += max(0,kosu[2]-1)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

kosu = [0,0,0]
ans = 0
for i in range(n):
    s = input()
    ans += s.count('AB')
    if s=="BA":
        kosu[2] += 1
        continue
    if s[0] == "B":
        kosu[1] += 1
    if s[-1] == "A":
        kosu[0] += 1
        
print(ans+min(kosu[0]+kosu[2]//2, kosu[1]+kosu[2]//2))

Submission status: Wrong Answer

Solution code:
n = int(input())

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

Submission status: Wrong Answer

Solution code:
from typing import List, Any


def read_int() -> int:
    return int(input().strip())


def read_ints() -> List[int]:
    return list(map(int, input().strip().split(' ')))


def ceil(A, B):
    if A%B == 0:
        return A//B
    return A//B+1

def solve() -> Any:
    N = read_int()
    answer = 0
    # BxA, Bx, xA
    BxA = 0
    Bx = 0
    xA = 0
    for _ in range(N):
        S = input()
        for i in range(1, len(S)):
            if S[i-1] == 'A' and S[i] == 'B':
                answer += 1
        if S[0] == 'B' and S[-1] == 'A':
            BxA += 1
        elif S[0] == 'B':
            Bx += 1
        elif S[-1] == 'A':
            xA += 1
    k = min(Bx, xA)
    Bx -= k
    xA -= k
    answer += k
    if BxA:
        answer += BxA-1
        if Bx:
            answer += 1
        if xA:
            answer += 1
    return answer


if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
from typing import List, Any


def read_int() -> int:
    return int(input().strip())


def read_ints() -> List[int]:
    return list(map(int, input().strip().split(' ')))


def ceil(A, B):
    if A%B == 0:
        return A//B
    return A//B+1

def solve() -> Any:
    N = read_int()
    answer = 0
    # BxA, Bx, xA
    BxA = 0
    Bx = 0
    xA = 0
    for _ in range(N):
        S = input()
        for i in range(1, len(S)):
            if S[i-1] == 'A' and S[i] == 'B':
                answer += 1
        if S[0] == 'B' and S[-1] == 'A':
            BxA += 1
        elif S[0] == 'B':
            Bx += 1
        elif S[-1] == 'A':
            xA += 1
    k = min(Bx, xA)
    Bx -= k
    xA -= k
    answer += k
    if BxA:
        answer += BxA-1
        if Bx or xA:
            answer += 1
    return answer


if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

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

a = 0
b = 0
ab = 0
ba = 0
for x in s:
    if x[-1] == 'A':
        a += 1
    if x[0] == 'B':
        b += 1
    if 'AB' in x:
        ab += x.count('AB')
    if x[-1] == 'A' and x[0] == 'B':
        ba = 1
ans = ab + min(max(a, b) - ba, min(a, b))
print(ans)

Submission status: Wrong Answer

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

a = 0
b = 0
ab = 0
ba = 0
for x in s:
    if x[-1] == 'A':
        a += 1
    if x[0] == 'B':
        b += 1
    if 'AB' in x:
        ab += 1
    if x[-1] == 'A' and x[0] == 'B':
        ba = 1
ans = ab + min(max(a, b) - ba, min(a, b))
print(ans)

Submission status: Wrong Answer

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

a = 0
b = 0
ab = 0
ba = 0
for x in s:
    if x[-1] == 'A':
        a += 1
    if x[0] == 'B':
        b += 1
    if 'AB' in x:
        ab += 1
    if x[-1] == 'A' and x[0] == 'B':
        ba += 1
ans = ab + min(max(a, b) - ba, min(a, b))
print(ans)

Submission status: Wrong Answer

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

a = 0
b = 0
ab = 0
ba = 0
for x in s:
    if x[-1] == 'A':
        a += 1
    if x[0] == 'B':
        b += 1
    if 'AB' in x:
        ab += 1
    if x[-1] == 'A' and x[0] == 'B':
        ba += 1
ans = ab + min(max(a, b) - 1, min(a, b))
print(ans)

Submission status: Wrong Answer

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

a = 0
b = 0
ab = 0
for x in s:
    if x[-1] == 'A':
        a += 1
    if x[0] == 'B':
        b += 1
    if 'AB' in x:
        ab += 1
ans = ab + min(a, b)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

res = 0
xa = 0
bx = 0
ba = 0

for _ in range(N):
    s = input()
    if s[-1] == 'A' and s[0] == 'B':
        ba += 1
    else:
        xa += int(s[-1] == 'A')
        bx += int(s[0] == 'B')
    res += s.count('AB')

if xa > 0 and bx > 0:
    res += ba + 1
    xa -= 1
    bx -= 1
elif xa > 0:
    res += ba
    xa -= 1
elif bx > 0:
    res += ba
    bx -= 1
else:
    res += ba - 1

print(res + min(xa,bx))

Submission status: Wrong Answer

Solution code:
N = int(input())

res = 0
xa = 0
bx = 0
ba = 0

for _ in range(N):
    s = input()
    if s[-1] == 'A' and s[0] == 'B':
        ba += 1
    else:
        xa += int(s[-1] == 'A')
        bx += int(s[0] == 'B')
    res += s.count('AB')

if xa > 0 and bx > 0:
    res += ba + 1
    xa -= 1
    bx -= 1
elif xa > 0:
    res += ba
    xa -= 1
elif bx > 0:
    res += bx
    bx -= 1
else:
    res += bx - 1

print(res + min(xa,bx))

Submission status: Wrong Answer

Solution code:
N = int(input())
cnt, A, B, C = 0, 0, 0, 0
for _ in range(N):
    S = input().rstrip()
    flg = False
    for s in S:
        if s=="A":
            flg=True
        elif flg==True and s=="B":
            cnt += 1
            flg = False
        else: flg = False
    if S[0]=="B": B+=1
    if S[-1]=="A": A+=1
    if S[0]=="B" and S[-1]=="A": C+=1
if A==B==C: print(cnt+A-1)
else: print(cnt+min(A,B))

Submission status: Wrong Answer

Solution code:
N = int(input())

res = 0
xa = 0
bx = 0
ba = 0

for _ in range(N):
    s = input()
    if s[-1] == 'A' and s[0] == 'B':
        ba += 1
    else:
        xa += int(s[-1] == 'A')
        bx += int(s[0] == 'B')
    res += s.count('AB')
print(res + min(xa,bx) + max(0, ba-(xa==bx)))

Submission status: Wrong Answer

Solution code:
N = int(input())
cnt, A, B = 0, 0, 0
for _ in range(N):
    S = input().rstrip()
    flg = False
    for s in S:
        if s=="A":
            flg=True
        elif flg==True and s=="B":
            cnt += 1
            flg = False
        else: flg = False
    if S[0]=="B": B+=1
    if S[-1]=="A": A+=1
if A==B: print(cnt+A-1)
else: print(cnt+min(A,B))

Submission status: Wrong Answer

Solution code:
N = int(input())
cnt, A, B = 0, 0, 0
for _ in range(N):
    S = input().rstrip()
    flg = False
    for s in S:
        if s=="A":
            flg=True
        elif flg==True and s=="B":
            cnt += 1
            flg = False
        else: flg = False
    if S[0]=="B": B+=1
    if S[-1]=="A": A+=1
if A==B==N: print(cnt+A-1)
else: print(cnt+min(A,B))

Submission status: Wrong Answer

Solution code:
N = int(input())
cnt, A, B = 0, 0, 0
for _ in range(N):
    S = input().rstrip()
    flg = False
    for s in S:
        if s=="A":
            flg=True
        elif flg==True and s=="B":
            cnt += 1
        else: flg = False
    if S[0]=="B": B+=1
    if S[-1]=="A": A+=1
if A==B==N: print(cnt+A-1)
else: print(cnt+min(A,B))

Submission status: Wrong Answer

Solution code:
n, *S = open(0)
n = int(n)
a = b = ba = c = 0
for i in range(n):
  S[i] = S[i].strip()
  if S[i][-1]=='A' and S[i][0]=='B':
    ba += 1
  else:
    a += S[i][-1]=='A'
    b += S[i][0]=='B'
  c += S[i].count('AB')
ba -= 1
if a > 0:
  ba += 1
  a -= 1
if b > 0:
  ba += 1
  b -= 1
print(c + ba + min(a, b))

Submission status: Wrong Answer

Solution code:
n, *S = open(0)
n = int(n)
a = b = ba = c = 0
for i in range(n):
  S[i] = S[i].strip()
  a += S[i][-1]=='A'
  b += S[i][0]=='B'
  ba += S[i][-1]=='A' and S[i][0]=='B'
  c += S[i].count('AB')
a -= ba
b -= ba
if a > 0 and b > 0:
  ba += 1
  a -= 1
  b -= 1
elif a > 0:
  a -= 1
elif b > 0:
  b -= 1
else:
  ba -= 1
print(c + ba + min(a, b))

Submission status: Wrong Answer

Solution code:
n, *S = open(0)
n = int(n)
a = b = c = 0
for i in range(n):
  S[i] = S[i].strip()
  a += S[i][-1]=='A'
  b += S[i][0]=='B'
  c += S[i].count('AB')
print(c + min(a, b))

Submission status: Wrong Answer

Solution code:
n = int(input())
S = []
# c1:Bで始まってAで終わるもの
# c2:Bで始まるが、A以外で終わるもの
# c3:Bで始まらないが、Aで終わるもの
# c4:Bでも始まらないし、Aでも終わらないもの

ans = 0
c1 = 0
c2 = 0
c3 = 0
for _ in range(n):
  s = input()
  ans += s.count("AB")
  if s.startswith("B") and s.endswith("A"):
    c1 += 1
  elif s.startswith("B"):
    c2 += 1
  elif s.endswith("A"):
    c3 += 1
if c2 + c3 != 0:
  ans += min(c1+c2, c1+c3)
else:
  ans += c1 - 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
S = []
# c1:Bで始まってAで終わるもの
# c2:Bで始まるが、A以外で終わるもの
# c3:Bで始まらないが、Aで終わるもの
# c4:Bでも始まらないし、Aでも終わらないもの

ans = 0
c1 = 0
c2 = 0
c3 = 0
for _ in range(n):
  s = input()
  ans += s.count("AB")
  if s.startswith("B") and s.endswith("A"):
    c1 += 1
  elif s.startswith("B"):
    c2 += 1
  elif s.endswith("A"):
    c3 += 1
if c2 != 0 or c3 !=0:
  ans += min(c1+c2, c1+c3)
else:
  ans += c1 - 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
S = []
# c1:Bで始まってAで終わるもの
# c2:Bで始まるが、A以外で終わるもの
# c3:Bで始まらないが、Aで終わるもの
# c4:Bでも始まらないし、Aでも終わらないもの

ans = 0
c1 = 0
c2 = 0
c3 = 0
for _ in range(n):
  s = input()
  if "AB" in s:
    ans += 1
  if s.startswith("B") and s.endswith("A"):
    c1 += 1
  elif s.startswith("B"):
    c2 += 1
  elif s.endswith("A"):
    c3 += 1
if c2 != 0 and c3 !=0:
  ans += min(c1+c2, c1+c3)
elif c2 == 0 or c3 == 0:
  ans += min(c1+c2, c1+c3) + 1
else:
  ans += c1 - 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
S = []
# c1:Bで始まってAで終わるもの
# c2:Bで始まるが、A以外で終わるもの
# c3:Bで始まらないが、Aで終わるもの
# c4:Bでも始まらないし、Aでも終わらないもの

"""
c4は関係ない。
min(c2,c3)で作れる。
min(c1, |c2-3|)でさらに作れる。

"""
ans = 0
c1 = 0
c2 = 0
c3 = 0
for _ in range(n):
  s = input()
  if "AB" in s:
    ans += 1
  if s.startswith("B") and s.endswith("A"):
    c1 += 1
  elif s.startswith("B"):
    c2 += 1
  elif s.endswith("A"):
    c3 += 1
if c2 != 0 or c3 !=0:
  ans += min(c1+c2, c1+c3)
else:
  ans += c1 - 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
S = []
# c1:Bで始まってAで終わるもの
# c2:Bで始まるが、A以外で終わるもの
# c3:Bで始まらないが、Aで終わるもの
# c4:Bでも始まらないし、Aでも終わらないもの

"""
c4は関係ない。
min(c2,c3)で作れる。
min(c1, |c2-3|)でさらに作れる。

"""
ans = 0
c1 = 0
c2 = 0
c3 = 0
for _ in range(n):
  s = input()
  if "AB" in s:
    ans += 1
  if s.startswith("B") and s.endswith("A"):
    c1 += 1
  elif s.startswith("B"):
    c2 += 1
  elif s.endswith("A"):
    c3 += 1
if c2 != 0 and c3 !=0:
  ans += min(c1+c2, c1+c3)
else:
  ans += c1 - 1
print(ans)

Submission status: Wrong Answer

Solution code:
x=a=b=0
for _ in "_"*int(input()):s=input();x+=s.count("AB");a+=s[-1]=="A";b+=s[0]=="B"
print(x+min(a,b))

Submission status: Wrong Answer

Solution code:
x=a=b=0
for _ in "_"*int(input()):s=input();x+=s.count("AB");s=s.replace("AB","");a+=(s[-1]=="A");b+=(s[0]=="B")
print(x+min(a,b))

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","x")
    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
mn=min(ac,bc)
ac-=mn
bc-=mn
cnt += mn
if ac or bc:
    cnt += abc
else:
    cnt+=max(abc-1,0)
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
lasta = 0
firstb = 0
btoa = 0
cnt = 0
for _ in range(n):
    S = input()
    cnt += S.count('AB')
    if S[0] == 'B':
        firstb += 1
    if S[-1] == 'A':
        lasta += 1

    if S[0] == 'B' and S[-1] == 'A':
        btoa += 1

if lasta == btoa and firstb == btoa:
    lasta -= 1
    firstb -= 1
print(cnt + min(lasta, firstb))

Submission status: Wrong Answer

Solution code:
n = int(input())
lasta = 0
firstb = 0
cnt = 0
for _ in range(n):
    S = input()
    cnt += S.count('AB')
    if S[0] == 'B':
        firstb += 1
    if S[-1] == 'A':
        lasta += 1

if lasta == n:
    lasta -= 1
if firstb == n:
    firstb -= 1
print(cnt + min(lasta, firstb))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def perm(n,mod=None):
    ans = 1
    for i in range(1,n+1):
        ans *= i
        if mod!=None:
            ans %= mod
    return ans
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
    def list(self):
        l = []
        for k in self:
            l.extend([k]*self[k])
        return l
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]
def pf(x):
    C = counter()
    p = 2
    while x>1:
        k = 0
        while x%p==0:
            x //= p
            k += 1
        if k>0:
            C.add(p,k)
        p = p+2-(p==2) if p*p<x else x
    return C

N=intput()
S=[input() for _ in range(N)]
A,B,BA=0,0,0
ans=0
for s in S:
    ans+=s.count('AB')
    if s[0]=='B' and s[-1]=='A':
        BA+=1
    elif s[0]=='B':
        B+=1
    elif s[-1]=='A':
        A+=1
ans+=min(A,B)+BA-int(A==B==0)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def perm(n,mod=None):
    ans = 1
    for i in range(1,n+1):
        ans *= i
        if mod!=None:
            ans %= mod
    return ans
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
    def list(self):
        l = []
        for k in self:
            l.extend([k]*self[k])
        return l
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]
def pf(x):
    C = counter()
    p = 2
    while x>1:
        k = 0
        while x%p==0:
            x //= p
            k += 1
        if k>0:
            C.add(p,k)
        p = p+2-(p==2) if p*p<x else x
    return C

N=intput()
S=[input() for _ in range(N)]
A,B,BA=0,0,0
ans=0
for s in S:
    ans+=s.count('AB')
    if s[0]=='B' and s[-1]=='A':
        BA+=1
    elif s[0]=='B':
        B+=1
    elif s[-1]=='A':
        A+=1
ans+=min(A,B)+BA-int(A==B)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def perm(n,mod=None):
    ans = 1
    for i in range(1,n+1):
        ans *= i
        if mod!=None:
            ans %= mod
    return ans
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
    def list(self):
        l = []
        for k in self:
            l.extend([k]*self[k])
        return l
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]
def pf(x):
    C = counter()
    p = 2
    while x>1:
        k = 0
        while x%p==0:
            x //= p
            k += 1
        if k>0:
            C.add(p,k)
        p = p+2-(p==2) if p*p<x else x
    return C

N=intput()
S=[input() for _ in range(N)]
A,B=0,0
ans=0
for s in S:
    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:
f=1
a=0
b=0
c=0

for i in range(int(input())):
  s=input()
  if s[0]=="B":
    b+=1
  if s[-1]=="A":
    a+=1
  if (not(s[0]=="B" and s[-1]=="A"))and(s[0]=="B" or s[-1]=="A"):
    f=0
  for j in range(len(s)-1):
    if s[j:j+2]=="AB":
      c+=1
print(min(a,b)+c-f)

Submission status: Wrong Answer

Solution code:
f=1
a=0
b=0
c=0

for i in range(int(input())):
  s=input()
  if s[0]=="B":
    b+=1
  if s[-1]=="A":
    a+=1
  if not(s[0]=="B" and s[-1]=="A"):
    f=0
  for j in range(len(s)-1):
    if s[j:j+2]=="AB":
      c+=1
print(min(a,b)+c-f)

Submission status: Wrong Answer

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