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=0
BA=0
XA=0
BX=0
for i in range(N):
    st=input()
    s+=st.count("AB")
    f1=0
    f2=0
    if(st[0]=="B"):f1=1
    if(st[-1]=="A"):f2=1

    if(f1 and f2):
        BA+=1
    elif(f1):
        BX+=1
    elif(f2):
        XA+=1
if(XA==0 and BX==0):
    print(s)
else:
    print(s+min(XA+BA,BX+BA))

Submission status: Wrong Answer

Solution code:
N=int(input())
s=0
BA=0
XA=0
BX=0
for i in range(N):
    st=input()
    s+=st.count("AB")
    f1=0
    f2=0
    if(st[0]=="B"):f1=1
    if(st[-1]=="A"):f2=1

    if(f1 and f2):
        BA+=1
    elif(f1):
        BX+=1
    elif(f2):
        XA+=1

print(s+min(BX+BA,BX+BA))

Submission status: Wrong Answer

Solution code:
N=int(input())
s=0
BA=0
XA=0
BX=0
for i in range(N):
    st=input()
    s+=st.count("AB")
    f1=0
    f2=0
    if(st[0]=="B"):BX+=1
    if(st[-1]=="A"):XA+=1

print(s+min(BX,XA))

Submission status: Wrong Answer

Solution code:
N=int(input())
ans=0
ba=xa=bx=0
for i in range(N):
    s=input()
    ans+=s.count('AB')
    if s.startswith('B') and s.endswith('A'):
        ba+=1
    elif s.endswith('A'):
        xa+=1
    elif s.startswith('B'):
        bx+=1

if ba==0:
    ans+=min(xa,bx)
elif ba>0:
    ans+=ba-1
    ans+=min(xa,bx)
    if xa!=bx:
        ans+=1

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
ans=0
lwa=swb=0 # Aで始まるもの，Bで始まるものの数
for i in range(N):
    s=input()
    ans+=s.count('AB')
    lwa+=int(s.endswith('A'))
    swb+=int(s.startswith('B'))
ans+=min(lwa,swb,N-1)
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    ab_cnt = 0
    b_cnt = 0
    a_cnt = 0
    ans = 0
    for _ in range(N):
        S = input()
        ans += S.count("AB")
        if S[0] == "B" and S[-1] == "A":
            ab_cnt += 1
        elif S[0] == "B":
            b_cnt += 1
        elif S[-1] == "A":
            a_cnt += 1
    if a_cnt < b_cnt:
        ans += a_cnt
        b_cnt -= a_cnt
        ans += min(b_cnt, ab_cnt)
    else:
        ans += b_cnt
        a_cnt -= b_cnt
        ans += min(a_cnt, ab_cnt)
    if ab_cnt > 1:
        ans += ab_cnt - 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# 1. 両端にaもbも含まれないもしくは「先頭がaで末尾がb」は分けて考える 最後にテキトーに加えてやる
# 2. 「先頭がb」「末尾がa」「先頭がbで末尾がa」
# cca
# caa
# bac
# bbc
# bccca < -これは連結したほうがよさそう連結したあとに左にooa右にbooを加える
# あまったooaとbooで適当にくっつける
n = int(input())
ans = 0
both = 0
a, b = 0, 0
for _ in range(n):
    s = input()
    if s[0] != "B" and s[-1] != "A":
        ans += s.count("AB")
    else:
        if s[0] == "B" and s[-1] == "A":
            both += 1
            ans += s.count("AB")
        elif s[0] == "B":
            b += 1
            ans += s.count("AB")
        elif s[-1] == "A":
            a += 1
            ans += s.count("AB")
# 両脇にあるやつを連結
ans += both - 1
# 連結したやつの先頭に加えられたら加える
if a > 0:
    ans += 1
    a -= 1
# 同じく末尾
if b > 0:
    ans += 1
    b -= 1
# 余ったやつで作成
ans += min(a, b)
print(ans)

Submission status: Wrong Answer

Solution code:
# 1. 両端にaもbも含まれないもしくは「先頭がaで末尾がb」は分けて考える 最後にテキトーに加えてやる
# 2. 「先頭がb」「末尾がa」「先頭がbで末尾がa」
# cca
# caa
# bac
# bbc
# bccca < -これは連結したほうがよさそう連結したあとに左にooa右にbooを加える
# あまったooaとbooで適当にくっつける
n = int(input())
ans = 0
both = 0
a, b = 0, 0
for _ in range(n):
    s = input()
    if s[0] != "B" and s[-1] != "A":
        ans += s.count("AB")
    else:
        if s[0] == "B" and s[-1] == "A":
            both += 1
            ans += s.count("AB")
        elif s[0] == "B":
            b += 1
            ans += s.count("AB")
        else:
            a += 1
            ans += s.count("AB")
# 両脇にあるやつを連結
ans += both - 1
# 連結したやつの先頭に加えられたら加える
if a > 0:
    ans += 1
    a -= 1
# 同じく末尾
if b > 0:
    ans += 1
    b -= 1
# 余ったやつで作成
ans += min(a, b)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
hb = 0
ta = 0
hbta = 0
ab = 0
for i in range(n):
  s = input()
  ab += s.count('AB')
  f = (s[len(s) -1] == 'A')
  g = (s[0] == 'B')
  if f and g:
    hbta += 1
  elif f:
    ta += 1
  elif g:
    hb += 1
ab += min(hbta, ta)
ab += min(hbta, hb)
ab += max(0, min(hb-hbta, ta-hbta))
ab += max(0, min(hbta-hb, hbta-ta))
print(ab)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
last_a = 0
begin_b = 0

ans = 0
check = 0
for s in S:
    if s[0] == "B":
        begin_b += 1
    if s[-1] == "A":
        last_a += 1
    if s[0] == "B" and s[-1] == "A":
        check += 1
    for i in range(len(s)-1):
        if s[i] == "A" and s[i+1] == "B":
            ans += 1
ans += min(last_a, begin_b)
if last_a == begin_b and check == begin_b:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
last_a = 0
begin_b = 0

ans = 0
check = 0
for s in S:
    if s[0] == "B":
        begin_b += 1
    if s[-1] == "A":
        last_a += 1
    if s[0] == "B" and s[-1] == "A":
        check = 1
    for i in range(len(s)-1):
        if s[i] == "A" and s[i+1] == "B":
            ans += 1
ans += min(last_a, begin_b)
if last_a == begin_b and check == 1:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
last_a = 0
begin_b = 0

ans = 0
check = 0
for s in S:
    if s[0] == "B":
        begin_b += 1
    if s[-1] == "A":
        last_a += 1
    if s[0] == "B" and s[-1] == "A":
        check = 1
    for i in range(len(s)-1):
        if s[i] == "A" and s[i+1] == "B":
            ans += 1
ans += min(last_a, begin_b)
if last_a == 1 and begin_b == 1 and check == 1:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
last_a = 0
begin_b = 0

ans = 0
for s in S:
    if s[0] == "B":
        begin_b += 1
    if s[-1] == "A":
        last_a += 1
    for i in range(len(s)-1):
        if s[i] == "A" and s[i+1] == "B":
            ans += 1
ans += min(last_a, begin_b)
if N == 1 and S[0][0] == "B" and S[0][-1] == "A":
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
last_a = 0
begin_b = 0

ans = 0
for s in S:
    if s[0] == "B":
        begin_b += 1
    if s[-1] == "A":
        last_a += 1
    for i in range(len(s)-1):
        if s[i] == "A" and s[i+1] == "B":
            ans += 1
ans += min(last_a, begin_b)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = 0
B = 0
BA = 0
ans = 0
for i in range(N):
  s = input()
  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

while A > 0 or BA > 0:
  if A > 0:
    A -= 1
  elif BA > 0:
    BA -= 1
  else:
    break
  if B > 0:
    B -= 1
    ans += 1
  elif BA > 0:
    BA -= 1
    A += 1
    ans += 1
  else:
    break
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = 0
B = 0
BA = 0
ans = 0
for i in range(N):
  s = input()
  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

while A > 0 or BA > 0:
  if A > 0:
    A -= 1
  elif BA > 0:
    BA -= 1
  else:
    break
  if B > 0:
    B -= 1
    ans += 1
  elif BA > 0:
    ans += 1
  else:
    break
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
S=[input() for i in range(N)]
count=0
countA=0
countB=0
countB_A=0
for s in S:
    countA+=s[0]=="B"
    countB+=s[-1]=="A"
    countB_A+=s[0]=="B" and s[-1]=="A"
    for i in range(len(s)-1):
        count+=s[i]+s[i+1]=="AB"
if countA==countB==countB_A:
    count-=1
print(count+min(countA,countB))

Submission status: Wrong Answer

Solution code:
N=int(input())
S=[input() for i in range(N)]
count=0
countA=0
countB=0
for s in S:
    countA+=s[0]=="B"
    countB+=s[-1]=="A"
    for i in range(len(s)-1):
        count+=s[i]+s[i+1]=="AB"
print(count+min(countA,countB))

Submission status: Wrong Answer

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

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

# tail = a
a = [False] * N
for i in range(N):
    if s[i][-1] == 'A':
        a[i] = True

# head = b
b = [False] * N
for i in range(N):
    if s[i][0] == 'B':
        b[i] = True

print(ans + min(sum(a), sum(b)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = 0
B = 0
AB = 0
BtoA = 0
for i in range(N):
    s = input()
    AB += s.count('AB')
    if s[-1] == 'A':
        A += 1
    if s[0] == 'B':
        B += 1
    if s[0] == 'B' and s[-1] == 'A':
        BtoA += 1
if A == B == BtoA:
    A -= 1
answer = AB + min(N-1, min(A, B))
print(answer)

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

cnt=0
cnt_a=0
cnt_b=0
cnt_ab=0
for i in range(n):
    if s[i][0]=='B' and s[i][-1]=='A':
        cnt_ab+=1
    elif s[i][0]=='B':
        cnt_b+=1
    elif s[i][-1]=='A':
        cnt_a+=1
    if 'AB' in s[i]:
        cnt+=s[i].count('AB')
ans=cnt+(cnt_a+cnt_b+cnt_ab*2)//2
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in [0]*n]
#print(s)

# (元々あるABの数)+(連結で出来るABの数)
cnt = 0
a_cnt = 0
b_cnt = 0
ba_cnt = 0
for st in s:
  cnt += st.count("AB")
  if st[-1]=="A":
    a_cnt += 1
  if st[0]=="B":
    b_cnt += 1
  if st[0]=="B" and st[-1]=="A":
    ba_cnt += 1    
    
#それと、
if a_cnt == ba_cnt:
  print(cnt + (ba_cnt-1) + min( a_cnt-ba_cnt, b_cnt-ba_cnt))
else:
  print(cnt+min(a_cnt,b_cnt))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in [0]*n]
#print(s)

# (元々あるABの数)+(連結で出来るABの数)
cnt = 0
a_cnt = 0
b_cnt = 0
ba_cnt = 0
for st in s:
  cnt += st.count("AB")
  if st[-1]=="A":
    a_cnt += 1
  if st[0]=="B":
    b_cnt += 1
  if st[0]=="B" and st[-1]=="A":
    ba_cnt += 1    
    
#あくまでも末尾のAと他の文字のBがくっつかなきゃダメ 
print(cnt + (ba_cnt-1) + min( a_cnt-ba_cnt, b_cnt-ba_cnt))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in [0]*n]
#print(s)

# (元々あるABの数)+(連結で出来るABの数)
cnt = 0
a_cnt = 0
b_cnt = 0
for st in s:
  cnt += st.count("AB")
  if st[-1]=="A":
    a_cnt += 1
  if st[0]=="B":
    b_cnt += 1
    
#先頭のB,末尾のAが全部についていたら、必ずB-----Aとなってしまう
if min(a_cnt,b_cnt) == n:
  print(cnt+min(a_cnt,b_cnt)-1)
else:
  print(cnt+min(a_cnt,b_cnt))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in [0]*n]
#print(s)

# (元々あるABの数)+(連結で出来るABの数)
cnt = 0
a_cnt = 0
b_cnt = 0
for st in s:
  cnt += st.count("AB")
  if st[-1]=="A":
    a_cnt += 1
  if st[0]=="B":
    b_cnt += 1
print(cnt+min(a_cnt,b_cnt))

Submission status: Wrong Answer

Solution code:
N = int(input())

A = []
B = []

ans = 0
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":
        B.append(i)
    if s[-1] == "A":
        A.append(i)

A = set(A)
B = set(B)
#print(A, B)
if len(A) == len(B):
    ans += len(A)
    if len(A&B) == len(A):
        ans -= 1
elif len(A)> len(B):
    ans += len(B)
else:
    ans += len(A)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

A = []
B = []

ans = 0
for i 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.append(i)
    if s[-1] == "A":
        A.append(i)

A = set(A)
B = set(B)

if len(A) == N and len(B)==N:
    ans -= 1
elif len(A)> len(B):
    ans += len(B)
else:
    ans += len(A)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

A = []
B = []
C = []

for i in range(N):
    s = input()
    if s[-1] == "A":
        A.append(i)
        if s[0] == "B":
            B.append(i)
            C.append(i)
    elif s[0] == "B":
        B.append(i)
print(len(A)*len(B)-len(C))

Submission status: Wrong Answer

Solution code:
N = int(input())
s = [input() for i in range(N)]
minAB = 0
AB = [0, 0]
count = 0
for i, x in enumerate(s):
    minAB += x.count("AB")
    if x[-1] == "A":
        AB[0] += 1
    if x[0] == "B":
        AB[1] += 1
        if x[-1] == "A":
            count += 1
if count != max(AB):
    print(minAB + min(AB))
else:
    print(minAB + min(AB)-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = [input() for i in range(N)]
minAB = 0
AB = [0, 0]
for i, x in enumerate(s):
    minAB += x.count("AB")
    if x[-1] == "A":
        AB[0] += 1
    if x[0] == "B":
        AB[1] += 1

print(minAB + min(AB))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 18
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10**9+7


n = I()
s = SR(n)
last_a = 0
first_b = 0
last_a_first_b = 0
ab = 0
for i in s:
    ab += i.count('AB')
    if i[0] == 'B' and i[-1] == 'A':
        last_a_first_b += 1
    elif i[0] == 'B':
        first_b += 1
    elif i[-1] == 'A':
        last_a += 1


if last_a_first_b:
    ab += last_a_first_b - 1
    if last_a != first_b:
        ab += 1

ab += min(last_a, first_b)
print(ab)

Submission status: Wrong Answer

Solution code:
n=int(input())
countab=0
counta=0
countb=0
count=0
for i in range(n):
  w=input()
  num=len(w)
  for j in range(num-1):
    if w[j]=="A" and w[j+1]=="B":
      count+=1
  if w[0]=="B" and w[-1]=="A":
    countab+=1
  elif w[0]=="B":
    countb+=1
  elif w[-1]=="A":
    counta+=1
ans=count+countab
ans+=min(counta,countb)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
countab=0
counta=0
countb=0
count=0
for i in range(n):
  w=input()
  num=len(w)
  for j in range(num-1):
    if w[j]=="A" and w[j+1]=="B":
      count+=1
  if w[0]=="B" and w[-1]=="A":
    countab+=1
  elif w[0]=="B":
    countb+=1
  elif w[-1]=="A":
    counta+=1
ans=count
ans+=min(counta,countb)
num2=abs(counta-countb)
ans+=min(countab,num2)
if countab>num2:
  ans+=countab-num2
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
countab=0
counta=0
countb=0
count=0
for i in range(n):
  w=input()
  num=len(w)
  for j in range(num-1):
    if w[j]=="A" and w[j+1]=="B":
      count+=1
  if w[0]=="B" and w[-1]=="A":
    countab+=1
  elif w[0]=="B":
    countb+=1
  elif w[-1]=="A":
    counta+=1
ans=count
ans+=min(counta,countb)
num2=abs(counta-countb)
ans+=min(countab,num2)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
word = []
ab = 0
a = 0
b = 0
mid = 0

for i in range(n):
    s = str(input())
    if s[0] == "B" and s[-1] == "A":
        ab += 1
    elif s[0] == "B":
        a += 1
    elif s[-1] == "A":
        b += 1
    for k in range(len(s) - 1):
        if s[k] == "A" and s[k + 1] == "B":
            mid += 1

# print(a, b, ab, mid)

mid += ab - 1
if a == 0 and b == 0:
    mid += 0
elif a == 0 or b == 0:
    mid += 1
else:
    mid += 2 + min(a-1, b-1)

print(mid)

Submission status: Wrong Answer

Solution code:
import heapq
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    a = 0
    b = 0
    ab = 0
    for _ in range(N):
        s = input()[:-1]
        ab += s.count("AB")
        a += s[-1:].count("A")
        b += s[:1].count("B")

    print(ab + min(a,b))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
s=''
point=0
q1,q2,q3=0,0,0 #q1はBA q2はBの最初のみ q3はAの末尾のみ
for j in range(N):
    n=str(input())
    point+=n.count("AB")
    if n[0]=="B" and n[len(n)-1]=="A":
        q1+=1  
    elif n[0]=="B":
        q2+=1
    elif n[len(n)-1]=="A":
        q3+=1
    else:
        pass
if q1>=1:
    if q2>=1 and q3>=1:
        print(q1-1+2+min(q2-1,q3-1)+point)
    elif q2>=1:
        print(q1-1+point)
    elif q3>=1:
        print(q1-1+point)
    else:
        print(q1-1+point)
else:
    print(min(q2,q3)+point)

Submission status: Wrong Answer

Solution code:
N=int(input())
s=''
point=0
q1,q2,q3=0,0,0 #q1はBA q2はBの最初のみ q3はAの末尾のみ
for j in range(N):
    n=str(input())
    point+=n.count("AB")
    if n[0]=="B" and n[len(n)-1]=="A":
      q1+=1  
    elif n[0]=="B":
      q2+=1
    elif n[len(n)-1]=="A":
      q3+=1
    else:
      pass
if q1>=1:
  q2-=1
  q3-=1
  print(q1-1+2+min(q3,q2)+point)
else:
  print(min(q2,q3)+point)

Submission status: Wrong Answer

Solution code:
N=int(input())
s=''
point=0
q1,q2,q3=0,0,0 #q1はBA q2はAの末尾のみ q3はBの最初のみ
for j in range(N):
    n=str(input())
    point+=n.count("AB")
    if n[0]=="B" and n[len(n)-1]=="A":
      q1+=1  
    elif n[0]=="B":
      q2+=1
    elif n[len(n)-1]=="A":
      q3+=1
    else:
      pass
q2-=1
q3-=1
print(q1-1+2+min(q3,q2)+point)

Submission status: Wrong Answer

Solution code:
N = int(input())
cnt = 0
A = 0
B = 0
BA = 0
for _ in range(N):
    s = input()
    cnt += 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

# A <= Bとする
if not A <= B:
    A, B = B, A

if BA == 0:
    ans = cnt + A
else:
    ans = cnt + A + BA-1 + (B - A > 0)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
cnt = 0
A = 0
B = 0
BA = 0
for _ in range(N):
    s = input()
    cnt += 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

# A <= Bとする
if not A <= B:
    A, B = B, A

ans = cnt
ans += max(BA-1,0)
ans += A
ans += B - A > 0
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
cnt = 0
A = 0
B = 0
BA = 0
for _ in range(N):
    s = input()
    cnt += 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

# A <= Bとする
if not A <= B:
    A, B = B, A

ans = cnt
ans += max(BA-1,0)
A += 1
ans += min(A, B)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
cnt = 0
A = 0
B = 0
BA = 0
for _ in range(N):
    s = input()
    cnt += 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

# A <= Bとする
if not A <= B:
    A, B = B, A

ans = cnt
ans += BA-1
BA = 1
A += 1
ans += min(A, B)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
cnt = 0
A = 0
B = 0
BA = 0
for _ in range(N):
    s = input()
    cnt += 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

# A <= Bとする
if not A <= B:
    A, B = B, A

ans = cnt
ans += BA-1
BA = 1
A += 1
ans += max(A, B)
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
get=0
L=[]
s=''
point=0
for j in range(N):
    n=str(input())
    point+=n.count("AB")
    if n[0]=="B" and n[len(n)-1]=="A":
        L.append("1.1")
    elif n[0]=="B" and n[len(n)-1]!="A":
        L.append("1.0")
    elif n[0]!="B" and n[len(n)-1]=="A":
        L.append("0.1")
    else:
        L.append("0.0")
L.sort()
for j in range(N):
    s=s+L[j]
print(point+s.count("11"))

Submission status: Wrong Answer

Solution code:
N=int(input())
L=[]
for i in range(N):
  L.append(str(input()))
L.sort()
s=''
for j in range(N):
  s=s+L[j]
point=0
for k in range(len(s)-1):
  if s[k]=="A" and s[k+1]=="B":
    point+=1
print(point)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heapify,heappop,heappush
import sys,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
s = [input() for i in range(n)]
cnt = 0
a = 0
b = 0
ba = 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
if a == 0 and b == 0:
    print(cnt + ba-1)
else:
    print(cnt + min(a,b) + ba)

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heapify,heappop,heappush
import sys,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
s = [input() for i in range(n)]
cnt = 0
a = 0
b = 0
ba = 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
if a == 0 and b == 0:
    print(cnt + ba-1)
else:
    print(cnt + min(a+ba,b+ba))

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heapify,heappop,heappush
import sys,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
s = [input() for i in range(n)]
cnt = 0
a = 0
b = 0
for i in s:
    if i[0] == 'B':
        b += 1
    if i[-1] == 'A':
        a += 1
    for j in range(len(i)-1):
        if i[j] == 'A' and i[j+1] == 'B':
            cnt += 1
print(cnt + min(a,b))

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[list(input()) for i in range(n)]
count=0
countA=0
countB=0
countBA=0
for i in range(n):
    for j in range(len(s[i])-1):
        if s[i][j]=='A' and s[i][j+1]=='B':
            count+=1

for i in range(n):
    if s[i][0]=='B' and s[i][-1]=='A':
        countBA+=1
        
    elif s[i][0]=='B':
        countB+=1
    
    elif s[i][-1]=='A':
        countA+=1

if countBA>0:
    count+=countBA-1+min(countB+1,countA+1)

else:
    count+=min(countB,countA)
    
print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[list(input()) for i in range(n)]
count=0
countA=0
countB=0
countBA=0
for i in range(n):
    for j in range(len(s[i])-1):
        if s[i][j]=='A' and s[i][j+1]=='B':
            count+=1

for i in range(n):
    if s[i][0]=='B' and s[i][-1]=='A':
        countBA+=1
        
    elif s[i][0]=='B':
        countB+=1
    
    elif s[i][-1]=='A':
        countA+=1

count+=max(countBA-1,0)+min(countB+1,countA+1)
print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[list(input()) for i in range(n)]
count=0
countA=0
countB=0
for i in range(n):
    for j in range(len(s[i])-1):
        if s[i][j]=='A' and s[i][j+1]=='B':
            count+=1

for i in range(n):
    if s[i][0]=='B':
        countB+=1
    if s[i][-1]=='A':
        countA+=1

if countA==countB:
    count+=max(countA,0)
else:
    count+=min(countA,countB)
print(count)

Submission status: Wrong Answer

Solution code:

N = int(input())
AB = 0
LA = 0
FB = 0

for i in range(N):

    s = input()

    for j in range(len(s)):

        if j == 0 and s[j] == "B":
            FB += 1

        if j == len(s) - 1:
            if s[j] == "A":
                LA += 1

        elif s[j] == "A" and s[j+1] == "B":
            AB += 1



print (AB + min(FB,LA,N-1))

Submission status: Wrong Answer

Solution code:
#import math
#import copy
#import sys
#import bisect
#input = sys.stdin.readline
mod=10**9+7
def swap(a,b):
    c=a
    a=b
    b=c
    return a,b
def factorial(x): #階乗計算
    n=1
    for i in range(x):
        n=(n*(i+1))%mod
    return n

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)
def lcm(a, b):
    return((a*b)//gcd(a,b))
def div(x): #約数配列
    div=[1]
    check=2
    while(x!=1 and check <= int(x**0.5)+2):
        if(x%check==0):
            div.append(check)
            while(x%check==0):
                x=x//check
        check+=1
    if x != 1:
      div.append(x)
    return div
def div2(x): #素因数分解配列
    div2=[]
    check=2
    while(x!=1 and check <= int(x**0.5)+2):
        while x%check==0:
            div2.append(check)
            x/=check
        check+=1
    if x != 1:
      div2.append(x)
    return div2

def main():
    # x,y = map(int,input().split())           x=1,y=2
    # a = input().split()                      a=['1','2','3',...,'n']
    # a = list(map(int,input().split()))       a=[1,2,3,4,5,...,n]
    # li = input().split('T')	              FFFTFTTFF => li=['FFF', 'F', '', 'FF']
    # 複数行の「1 2 3 4」型の入力を一配列に
    # x = sorted([list(map(int, input().split())) for _ in range(n)])
    # ソート x.sort(key=lambda y:y[1])
    # [chr(i) for i in range(97, 97+26)] 英語小文字リスト
    # [chr(i) for i in range(65, 65+26)] 英語大文字リスト atcoder
    # ord(c) chr(2) alphabet <=> number
    n=int(input())
    cnt=a=b=ba=0
    for _ in range(n):
        s=input()
        for i in range(len(s)-1):
            if s[i:i+2]=="AB":
                cnt+=1
        if s[0]=="B" and s[len(s)-1]=="A":
            ba+=1
        elif s[0]=="B":
            b+=1
        elif s[len(s)-1]=="A":
            a+=1
    cnt+=min(ba,a)+min(ba,b)+min( max(0,a-min(ba,a)),max(0,b-min(ba,b)) )+min( max(0,ba-min(ba,a)),max(0,ba-min(ba,b)) )
    print(cnt)
    
main()

Submission status: Wrong Answer

Solution code:
#import math
#import copy
#import sys
#import bisect
#input = sys.stdin.readline
mod=10**9+7
def swap(a,b):
    c=a
    a=b
    b=c
    return a,b
def factorial(x): #階乗計算
    n=1
    for i in range(x):
        n=(n*(i+1))%mod
    return n

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)
def lcm(a, b):
    return((a*b)//gcd(a,b))
def div(x): #約数配列
    div=[1]
    check=2
    while(x!=1 and check <= int(x**0.5)+2):
        if(x%check==0):
            div.append(check)
            while(x%check==0):
                x=x//check
        check+=1
    if x != 1:
      div.append(x)
    return div
def div2(x): #素因数分解配列
    div2=[]
    check=2
    while(x!=1 and check <= int(x**0.5)+2):
        while x%check==0:
            div2.append(check)
            x/=check
        check+=1
    if x != 1:
      div2.append(x)
    return div2

def main():
    # x,y = map(int,input().split())           x=1,y=2
    # a = input().split()                      a=['1','2','3',...,'n']
    # a = list(map(int,input().split()))       a=[1,2,3,4,5,...,n]
    # li = input().split('T')	              FFFTFTTFF => li=['FFF', 'F', '', 'FF']
    # 複数行の「1 2 3 4」型の入力を一配列に
    # x = sorted([list(map(int, input().split())) for _ in range(n)])
    # ソート x.sort(key=lambda y:y[1])
    # [chr(i) for i in range(97, 97+26)] 英語小文字リスト
    # [chr(i) for i in range(65, 65+26)] 英語大文字リスト atcoder
    # ord(c) chr(2) alphabet <=> number
    n=int(input())
    cnt=a=b=ba=0
    for _ in range(n):
        s=input()
        for i in range(len(s)-1):
            if s[i:i+2]=="AB":
                cnt+=1
        if s[0]=="B" and s[len(s)-1]=="A":
            ba+=1
        elif s[0]=="B":
            b+=1
        elif s[len(s)-1]=="A":
            a+=1
    cnt+=min(ba,a)+min(ba,b)+min( max(0,a-min(ba,a)),max(0,b-min(ba,a)) )+min( max(0,ba-min(ba,a)),max(0,ba-min(ba,a)) )
    print(cnt)
    
main()

Submission status: Wrong Answer

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

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[str(input()) for i in range(n)]
#print(s)
ans=0
x=0
y=0
z=0
for i in range(n):
    ans+=s[i].count('AB')
    if s[i][0]=='B' and s[i][-1]=='A':
        x+=1
        y+=1
        z+=1
    elif s[i][-1]=='A':
        x+=1
        z+=1
    elif s[i][0]=='B':
        y+=1
        z+=1
p=(x-1 if x==y==z else min(x,y))
print(ans+p)