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

Solution (Python):
N = int(input())
s_list = [input() for _ in range(N)]

ans = 0
start_B = 0
end_A = 0
B_A = 0
for s in s_list:
  ans += s.count("AB")
  if s[0] == "B" and s[-1] == "A":
    B_A += 1
  elif s[0] == "B":
    start_B += 1
  elif s[-1] == "A":
    end_A += 1

if B_A > 0 and start_B + end_A == 0:
  ans += B_A - 1
else:
  ans += min(start_B, end_A) + B_A

print(ans)

Solution (Python):
n = int(input())
s = []
for i in range(0, n):
    s.append(input())
 
c = 0
a = 0
b = 0
ab = 0
 
for i in range(0, n):
    for j in range(1, len(s[i])):
        if s[i][j-1:j+1] == "AB":
            c += 1
    if s[i][-1] == "A" and s[i][0] == "B":
        ab += 1
    else: 
        if s[i][0] == "B":
            b += 1
        if s[i][-1] == "A":
            a += 1
            
s = c
 
if ab > 0:
    s += ab - 1
    if a > 0:
        s += 1
        a -= 1
    if b > 0:
        s += 1
        b -= 1
 
s += min(a, b)
 
print(s)

Solution (Python):
import numpy as np
    
n = int(input())
S = np.asarray([])
St = np.zeros(4) 
count = 0

for i in range(n):
    S=np.append(S,input())
    tmp = 0
    count += S[i].count("AB")
    if S[i][0] == "B":
        tmp += 1
    if S[i][-1] == "A":
        tmp += 2
    St[tmp]+=1

if (St[1] == 0)&(St[3]>0):
    St[1]+=1
    St[3]-=1

if (St[2] == 0)&(St[3]>0):
    St[2]+=1
    St[3]-=1

print(int(count+min(St[1],St[2])+St[3]))

Solution (Python):
N = int(raw_input())

inner_ab_count = 0
f_b_count = 0
l_a_count = 0
b_a_count = 0

for n in xrange(N):
    s = raw_input()
    inner_ab_count += s.count('AB')
    if s[0] == 'B':
        if s[-1] == 'A': b_a_count += 1
        else: f_b_count += 1
    elif s[-1] == 'A': l_a_count += 1

if b_a_count <= 0: result = inner_ab_count + min([l_a_count, f_b_count])
elif l_a_count + f_b_count == 0: result = inner_ab_count + b_a_count-1
else: result = inner_ab_count + b_a_count + min([l_a_count,f_b_count])
print result

Solution (Python):
n = int(input())
s = [input() for _ in range(n)]
a = 0
b = 0
ab = 0
ans = 0
for i in range(n):
    ans += s[i].count('AB')
    if s[i][-1]=='A' and s[i][0]=='B':
        ab += 1
        continue
    if s[i][-1]=='A':
        a += 1
    if s[i][0]=='B':
        b += 1
if ab==0:
    ans += min(a,b)
elif a+b>0:
    ans += ab+min(a,b)
else:
    ans += ab-1
print(ans)

Solution (Python):
def countAB(s):
    c = 0
    for i in range(0, len(s), 2):
        if i+1 < len(s) and s[i] == 'A' and s[i+1] == 'B':
            c += 1
        elif i-1 >= 0 and s[i-1] == 'A' and s[i] == 'B':
            c += 1
    return c


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

head = []
tail = []
con = []
els = []

for i in range(n):
    h = s[i][0]
    t = s[i][-1]
    if h == 'B' and t == 'A':
        con.append(s[i])
    elif h == 'B':
        tail.append(s[i])
    elif t == 'A':
        head.append(s[i])
    else:
        els.append(s[i])

string = ''.join(els)
state = False
while True:
    if not state:
        if len(head) > 0:
            string += head.pop(0)
        elif len(con) > 0:
            string += con.pop(0)
        else: break
        state = True
    elif state:
        if len(con) > 0:
            string += con.pop(0)
        elif len(tail) > 0:
            string += tail.pop(0)
            state = False
        else: break

string = string + ''.join(head+tail+con)

print(countAB(string))

Solution (Python):
n = int(input())
s_list = [input() for _ in range(n)]
res_ab = 0
res_ba = 0
res_a = 0
res_b = 0
for s in s_list:
    res_ab += s.count('AB')
    if s[0]=='B' and s[-1]=='A':
        res_ba += 1
    elif s[0]=='B':
        res_a += 1
    elif s[-1] == 'A':
        res_b += 1

if res_ba == 0:
    print(res_ab+min(res_a,res_b))
else:
    if res_a+res_b > 0:
        print(res_ab+res_ba+min(res_a,res_b))
    else:
        print(res_ab+res_ba-1)

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

ct_a=0
ct_b=0
ct_ab=0
ct_ba=0

for i in range(N):
    if s[i][0]=='B' and s[i][-1]=='A':
        ct_ba+=1
    elif s[i][0]=='B':
        ct_b+=1
    elif s[i][-1]=='A':
        ct_a+=1
    for j in range(len(s[i])-1):
        if s[i][j]=='A' and s[i][j+1]=='B':
            ct_ab+=1

if ct_ba==0:
    print(ct_ab+min(ct_a, ct_b))
elif ct_a+ct_b>0:
    print(ct_ab+ct_ba+min(ct_a,ct_b))
else:
    print(ct_ab+ct_ba-1)

Solution (Python):
N = int(input())
ba = b = a = 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

if ba == 0:
    ans += min(a, b)
else:
    if a + b > 0:
        ans += ba + min(a, b)
    else:
        ans += ba - 1

print(ans)

Solution (Python):
N = int(input())
ba = b = a = 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

if ba == 0:
    ans += min(a, b)
else:
    if a + b > 0:
        ans += ba + min(a, b)
    else:
        ans += ba - 1

print(ans)

Solution (Python):
N=int(input())
start=0
end=0
r=0
cnt=0
for i in range(N):
    s=input()
    cnt+=s.count('AB')
    if s[0] == 'B':
        start+=1
    if s[-1] == 'A':
        end+=1
    if s[0] == 'B' and s[-1] == 'A':
        r+=1
if start == end and start == r and end > 0:
    cnt -=1
print(cnt+min(start,end))

Solution (Python):

N = int(input())
S = [input() for i in range(N)]
c1, c2, c3, c = 0, 0, 0, 0
for s in S:
    if s[-1] == 'A' and s[0] == 'B':
        c1 += 1
    elif s[-1] != 'A' and s[0] == 'B':
        c2 += 1
    elif s[-1] == 'A' and s[0] != 'B':
        c3 += 1
    c += s.count('AB')

if c1 == 0:
    print(c + min(c2, c3))
elif c2 + c3 > 0:
    print(c + c1 + min(c2, c3))
else:
    print(c + c1 -1)

Solution (Python):
N = input()
s = []
flaga = 0
flagbs = 0
flagba = 0
Aend = 0
Bstart = 0
AB = 0
flaginit = 0
for i in range(N):
	s.append(raw_input())
for s2 in s:
	flaginit = 1
	flaga = 0
	flagbs = 0
	for c in s2:
		if flaginit == 1:
			if c =='A':
				flaga = 1
			elif c =='B':
				Bstart += 1
				flagbs = 1
			flaginit = 0
		else:
			if c == 'A':
				flaga = 1
			elif c =='B':
				if flaga == 1:
					AB += 1
				flaga = 0
			else:
				flaga = 0
	if flaga == 1:
		Aend += 1
		if flagbs == 0:
			flagba = 1
	else:
		if flagbs == 1:
			flagba = 1
if(flagba == 0 and Aend > 0):
	Aend -= 1
AB += min(Aend,Bstart,N-1)
print(AB)

Solution (Python):
N=int(input())
start=0
end=0
r=0
cnt=0
for i in range(N):
    s=input()
    cnt+=s.count('AB')
    if s[0] == 'B':
        start+=1
    if s[-1] == 'A':
        end+=1
    if s[0] == 'B' and s[-1] == 'A':
        r+=1
if start == end and start == r and end > 0:
    cnt -=1
print(cnt+min(start,end))

Solution (Python):
N = int(input())
ba = b = a = 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

if ba == 0:
    ans += min(a, b)
else:
    if a + b > 0:
        ans += ba + min(a, b)
    else:
        ans += ba - 1

print(ans)

Solution (Python):
n = int(input())
s = [input() for _ in range(n)]
a = 0
b = 0
ab = 0
ans = 0
for i in range(n):
    ans += s[i].count('AB')
    if s[i][-1]=='A' and s[i][0]=='B':
        ab += 1
        continue
    if s[i][-1]=='A':
        a += 1
    if s[i][0]=='B':
        b += 1
if ab==0:
    ans += min(a,b)
elif a+b>0:
    ans += ab+min(a,b)
else:
    ans += ab-1
print(ans)

Solution (Python):
def countAB(s):
    c = 0
    for i in range(0, len(s), 2):
        if i+1 < len(s) and s[i] == 'A' and s[i+1] == 'B':
            c += 1
        elif i-1 >= 0 and s[i-1] == 'A' and s[i] == 'B':
            c += 1
    return c


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

head = []
tail = []
con = []
els = []

for i in range(n):
    h = s[i][0]
    t = s[i][-1]
    if h == 'B' and t == 'A':
        con.append(s[i])
    elif h == 'B':
        tail.append(s[i])
    elif t == 'A':
        head.append(s[i])
    else:
        els.append(s[i])

string = ''.join(els)
state = False
while True:
    if not state:
        if len(head) > 0:
            string += head.pop(0)
        elif len(con) > 0:
            string += con.pop(0)
        else: break
        state = True
    elif state:
        if len(con) > 0:
            string += con.pop(0)
        elif len(tail) > 0:
            string += tail.pop(0)
            state = False
        else: break

string = string + ''.join(head+tail+con)

print(countAB(string))

Solution (Python):





import sys

N = int(input())
S = [input() for i in range(N)]




b_cnt = 0
a_cnt = 0
ab_cnt = 0
for i in range(N):
    if S[i][0]=='B'and S[i][-1]=='A':
        ab_cnt += 1
    elif S[i][0]=='B':
        b_cnt += 1
    elif S[i][-1]=='A':
        a_cnt += 1




ans = 0
if ab_cnt > 0:
    ans += ab_cnt - 1
    if b_cnt > 0:
        b_cnt -= 1
        ans += 1
    if a_cnt > 0:
        a_cnt -= 1
        ans += 1
ans += min(a_cnt, b_cnt)

"""
if max(a_cnt, b_cnt) == 0 and ab_cnt > 0:
    ab_cnt -= 1
elif min(a_cnt, b_cnt) > 0 and ab_cnt > 0:
    ab_cnt += 1
elif abs(a_cnt-b_cnt) == 0 and ab_cnt > 0:
    ab_cnt += 1
elif abs(a_cnt-b_cnt) > 0 and ab_cnt > 0:
    ab_cnt += 0
"""
    



print('/'.join(S).count('AB') + ans)

Solution (Python):
n=int(input())
s=[input() for _ in range(n)]
isFinishA=[s[i][-1]=='A' for i in range(n)]
isStartB=[s[i][0]=='B' for i in range(n)]
ans=0
for si in s:
    wasA=False
    for sij in si:
        if sij=='B':
            if wasA:
                ans+=1
        wasA=(sij=='A')

fromBtoA=0
fromB=0
toA=0
for i in range(n):
    if isFinishA[i]:
        if isStartB[i]:
            fromBtoA+=1
        else:
            toA+=1
    else:
        if isStartB[i]:
            fromB+=1

if fromBtoA==0:
    ans+=min(fromB,toA)
else:
    if fromB+toA>0:
        ans+=fromBtoA+min(fromB,toA)
    if fromB+toA==0:
        ans+=fromBtoA-1

print(ans)

Solution (Python):

N = int(input())
S = [input() for i in range(N)]
c1, c2, c3, c = 0, 0, 0, 0
for s in S:
    if s[-1] == 'A' and s[0] == 'B':
        c1 += 1
    elif s[-1] != 'A' and s[0] == 'B':
        c2 += 1
    elif s[-1] == 'A' and s[0] != 'B':
        c3 += 1
    c += s.count('AB')

if c1 == 0:
    print(c + min(c2, c3))
elif c2 + c3 > 0:
    print(c + c1 + min(c2, c3))
else:
    print(c + c1 -1)

Solution (Python):

import numpy as np


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


count = 0
for string in s:
    count += string.count("AB")


s_ba, s_oa, s_bo = [], [], []

for string in s:
    if string[0] == "B":
        if string[-1:] == "A": 
            s_ba.append(string)
        else:
            s_bo.append(string)
    elif string[-1:] == "A":
        s_oa.append(string)






element = 0
if len(s_oa) != 0: 
    s_oa.pop(0)
    element += 1

element += len(s_ba)

if len(s_bo) != 0: 
    s_bo.pop(0)
    element += 1

element += min(len(s_bo), len(s_oa))

if element != 0:
    count += element - 1

print(count)

Solution (Python):

N = int(input())
S = [input() for i in range(N)]
c1, c2, c3, c = 0, 0, 0, 0
for s in S:
    if s[-1] == 'A' and s[0] == 'B':
        c1 += 1
    elif s[-1] != 'A' and s[0] == 'B':
        c2 += 1
    elif s[-1] == 'A' and s[0] != 'B':
        c3 += 1
    c += s.count('AB')

if c1 == 0:
    print(c + min(c2, c3))
elif c2 + c3 > 0:
    print(c + c1 + min(c2, c3))
else:
    print(c + c1 -1)

Solution (Python):





import sys

N = int(input())
S = [input() for i in range(N)]




b_cnt = 0
a_cnt = 0
ab_cnt = 0
for i in range(N):
    if S[i][0]=='B'and S[i][-1]=='A':
        ab_cnt += 1
    elif S[i][0]=='B':
        b_cnt += 1
    elif S[i][-1]=='A':
        a_cnt += 1




ans = 0
if ab_cnt > 0:
    ans += ab_cnt - 1
    if b_cnt > 0:
        b_cnt -= 1
        ans += 1
    if a_cnt > 0:
        a_cnt -= 1
        ans += 1
ans += min(a_cnt, b_cnt)

"""
if max(a_cnt, b_cnt) == 0 and ab_cnt > 0:
    ab_cnt -= 1
elif min(a_cnt, b_cnt) > 0 and ab_cnt > 0:
    ab_cnt += 1
elif abs(a_cnt-b_cnt) == 0 and ab_cnt > 0:
    ab_cnt += 1
elif abs(a_cnt-b_cnt) > 0 and ab_cnt > 0:
    ab_cnt += 0
"""
    



print('/'.join(S).count('AB') + ans)

Solution (Python):
n = int(input())
S = [input() for i in range(n)]
cnt = 0
a = 0
b = 0
ba = 0
for i in range(n):
    cnt += S[i].count("AB")
    if S[i].startswith("B") and S[i].endswith("A"):
        ba += 1
    elif S[i].startswith("B"):
        b += 1
    elif S[i].endswith("A"):
        a += 1
if ba >= 1:
    cnt += ba - 1
    if a >= 1:
        cnt += 1
        a -= 1
    if b >= 1:
        cnt += 1
        b -= 1
    cnt += min(a, b)
elif a >= 1 and b >= 1:
    cnt += min(a, b)
print(cnt)

Solution (Python):

N = int(input())
S = [input() for i in range(N)]
c1, c2, c3, c = 0, 0, 0, 0
for s in S:
    if s[-1] == 'A' and s[0] == 'B':
        c1 += 1
    elif s[-1] != 'A' and s[0] == 'B':
        c2 += 1
    elif s[-1] == 'A' and s[0] != 'B':
        c3 += 1
    c += s.count('AB')

if c1 == 0:
    print(c + min(c2, c3))
elif c2 + c3 > 0:
    print(c + c1 + min(c2, c3))
else:
    print(c + c1 -1)

Solution (Python):
N = int(input())
S = []
ABcnt = 0
topABcnt = 0
topBbtmA = 0
topBbtmNO = 0
topNObtmA = 0
for _ in range(N):
  s = input()
  nowABcnt = s.count('AB')
  ABcnt += nowABcnt
  if s[0:2]=='AB':
    topABcnt += 1
  if s[0]=='B':
    if s[-1]=='A': topBbtmA += 1
    else: topBbtmNO += 1
  elif s[-1]=='A':
    topNObtmA += 1
  if nowABcnt!=0 or s[0]=='B' or s[-1]=='A': S.append(s)

ans = 0


ans += min(topBbtmNO, topNObtmA)
firstSurvivorCnt = max(topBbtmNO, topNObtmA) - ans

nikaisenGet = min(firstSurvivorCnt, topBbtmA)
ans += nikaisenGet

topBbtmA -= nikaisenGet
if ans == 0: ans += max(0, topBbtmA-1)
else: ans += topBbtmA 


print(ans+ABcnt)

Solution (Python):
N = int(input())
s_list = [input() for _ in range(N)]
end_a_num = 0
start_b_num = 0
same_num = 0
ans = 0
for s in s_list:
    ab_num = s.count('AB')
    ans += ab_num
    if s[-1] == 'A':
        end_a_num+=1
    if s[0]=='B':
        start_b_num+=1
    if s[-1]=='A' and s[0]=='B':
        same_num+=1


ans+= min(end_a_num,start_b_num)
if end_a_num==start_b_num and end_a_num==same_num and end_a_num>0:
    ans-=1


print(ans)

Solution (Python):
N = int(input())
S = []
ABcnt = 0
topABcnt = 0
topBbtmA = 0
topBbtmNO = 0
topNObtmA = 0
for _ in range(N):
  s = input()
  nowABcnt = s.count('AB')
  ABcnt += nowABcnt
  if s[0:2]=='AB':
    topABcnt += 1
  if s[0]=='B':
    if s[-1]=='A': topBbtmA += 1
    else: topBbtmNO += 1
  elif s[-1]=='A':
    topNObtmA += 1
  if nowABcnt!=0 or s[0]=='B' or s[-1]=='A': S.append(s)

ans = 0


ans += min(topBbtmNO, topNObtmA)
firstSurvivorCnt = max(topBbtmNO, topNObtmA) - ans

nikaisenGet = min(firstSurvivorCnt, topBbtmA)
ans += nikaisenGet

topBbtmA -= nikaisenGet
if ans == 0: ans += max(0, topBbtmA-1)
else: ans += topBbtmA 


print(ans+ABcnt)

Solution (Python):
N = int(input())
S = []
ABcnt = 0
topABcnt = 0
topBbtmA = 0
topBbtmNO = 0
topNObtmA = 0
for _ in range(N):
  s = input()
  nowABcnt = s.count('AB')
  ABcnt += nowABcnt
  if s[0:2]=='AB':
    topABcnt += 1
  if s[0]=='B':
    if s[-1]=='A': topBbtmA += 1
    else: topBbtmNO += 1
  elif s[-1]=='A':
    topNObtmA += 1
  if nowABcnt!=0 or s[0]=='B' or s[-1]=='A': S.append(s)

ans = 0


ans += min(topBbtmNO, topNObtmA)
firstSurvivorCnt = max(topBbtmNO, topNObtmA) - ans

nikaisenGet = min(firstSurvivorCnt, topBbtmA)
ans += nikaisenGet

topBbtmA -= nikaisenGet
if ans == 0: ans += max(0, topBbtmA-1)
else: ans += topBbtmA 


print(ans+ABcnt)

Solution (Python):
n = int(input())
S = [input() for i in range(n)]
cnt = 0
a = 0
b = 0
ba = 0
for i in range(n):
    cnt += S[i].count("AB")
    if S[i].startswith("B") and S[i].endswith("A"):
        ba += 1
    elif S[i].startswith("B"):
        b += 1
    elif S[i].endswith("A"):
        a += 1
if ba >= 1:
    cnt += ba - 1
    if a >= 1:
        cnt += 1
        a -= 1
    if b >= 1:
        cnt += 1
        b -= 1
    cnt += min(a, b)
elif a >= 1 and b >= 1:
    cnt += min(a, b)
print(cnt)

Solution (Python):
N = int(input())
S = []
ABcnt = 0
topABcnt = 0
topBbtmA = 0
topBbtmNO = 0
topNObtmA = 0
for _ in range(N):
  s = input()
  nowABcnt = s.count('AB')
  ABcnt += nowABcnt
  if s[0:2]=='AB':
    topABcnt += 1
  if s[0]=='B':
    if s[-1]=='A': topBbtmA += 1
    else: topBbtmNO += 1
  elif s[-1]=='A':
    topNObtmA += 1
  if nowABcnt!=0 or s[0]=='B' or s[-1]=='A': S.append(s)

ans = 0


ans += min(topBbtmNO, topNObtmA)
firstSurvivorCnt = max(topBbtmNO, topNObtmA) - ans

nikaisenGet = min(firstSurvivorCnt, topBbtmA)
ans += nikaisenGet

topBbtmA -= nikaisenGet
if ans == 0: ans += max(0, topBbtmA-1)
else: ans += topBbtmA 


print(ans+ABcnt)

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

Solution (Python):
n=int(input())

ans=0
ba=0
bstart=0
aend=0
for i in range(n):
  s=input()
  for j in range(len(s)):
    if s[j]=="A":
      if not j+1==len(s):
        if s[j+1]=="B":
          ans+=1
  
  if s[0]=="B" and s[-1]=="A":
    ba+=1
  else:
    if s[0]=="B":
      bstart+=1
    if s[-1]=="A":
      aend+=1
            
if ba==0:
  print(ans+min(bstart,aend))
else:
  if bstart>=1 and aend>=1:
    print(ans+ba-1+2+min(bstart-1,aend-1))
  else:
    print(ans+ba-1+max(min(1,bstart),min(1,aend)))

Solution (Python):
n = int(input())
S = [input() for i in range(n)]
cnt = 0
a = 0
b = 0
ba = 0
for i in range(n):
    cnt += S[i].count("AB")
    if S[i].startswith("B") and S[i].endswith("A"):
        ba += 1
    elif S[i].startswith("B"):
        b += 1
    elif S[i].endswith("A"):
        a += 1
if ba >= 1:
    cnt += ba - 1
    if a >= 1:
        cnt += 1
        a -= 1
    if b >= 1:
        cnt += 1
        b -= 1
    cnt += min(a, b)
elif a >= 1 and b >= 1:
    cnt += min(a, b)
print(cnt)

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

Solution (Python):
n=int(input())

ans=0
ba=0
bstart=0
aend=0
for i in range(n):
  s=input()
  for j in range(len(s)):
    if s[j]=="A":
      if not j+1==len(s):
        if s[j+1]=="B":
          ans+=1
  
  if s[0]=="B" and s[-1]=="A":
    ba+=1
  else:
    if s[0]=="B":
      bstart+=1
    if s[-1]=="A":
      aend+=1
            
if ba==0:
  print(ans+min(bstart,aend))
else:
  if bstart>=1 and aend>=1:
    print(ans+ba-1+2+min(bstart-1,aend-1))
  else:
    print(ans+ba-1+max(min(1,bstart),min(1,aend)))

Solution (Python):
N=int(input())
lasta=0
headb=0
spec=0
cnt=0
for _ in range(N):
    s=str(input())
    cnt+=s.count("AB")
    if s[0]=="B" and s[-1]=="A":
        spec+=1
    elif s[-1]=="A":
        lasta+=1
    elif s[0]=="B":
        headb+=1
        
if abs(headb-lasta)>=spec:
    print(min(headb,lasta)+spec+cnt)   
elif lasta==0 and headb==0 and spec>0:
    print(spec-1+cnt)
elif lasta==0 and headb==0 and spec==0:
    print(cnt)
else:
    print(spec-abs(headb-lasta)+max(headb,lasta)+cnt)

Solution (Python):
N = int(input())
S = []
ABcnt = 0
topABcnt = 0
topBbtmA = 0
topBbtmNO = 0
topNObtmA = 0
for _ in range(N):
  s = input()
  nowABcnt = s.count('AB')
  ABcnt += nowABcnt
  if s[0:2]=='AB':
    topABcnt += 1
  if s[0]=='B':
    if s[-1]=='A': topBbtmA += 1
    else: topBbtmNO += 1
  elif s[-1]=='A':
    topNObtmA += 1
  if nowABcnt!=0 or s[0]=='B' or s[-1]=='A': S.append(s)

ans = 0


ans += min(topBbtmNO, topNObtmA)
firstSurvivorCnt = max(topBbtmNO, topNObtmA) - ans

nikaisenGet = min(firstSurvivorCnt, topBbtmA)
ans += nikaisenGet

topBbtmA -= nikaisenGet
if ans == 0: ans += max(0, topBbtmA-1)
else: ans += topBbtmA 


print(ans+ABcnt)

Solution (Python):
n = int(input())
S = [input() for i in range(n)]
cnt = 0
a = 0
b = 0
ba = 0
for i in range(n):
    cnt += S[i].count("AB")
    if S[i].startswith("B") and S[i].endswith("A"):
        ba += 1
    elif S[i].startswith("B"):
        b += 1
    elif S[i].endswith("A"):
        a += 1
if ba >= 1:
    cnt += ba - 1
    if a >= 1:
        cnt += 1
        a -= 1
    if b >= 1:
        cnt += 1
        b -= 1
    cnt += min(a, b)
elif a >= 1 and b >= 1:
    cnt += min(a, b)
print(cnt)

Solution (Python):
n=int(input())

ans=0
ba=0
bstart=0
aend=0
for i in range(n):
  s=input()
  for j in range(len(s)):
    if s[j]=="A":
      if not j+1==len(s):
        if s[j+1]=="B":
          ans+=1
  
  if s[0]=="B" and s[-1]=="A":
    ba+=1
  else:
    if s[0]=="B":
      bstart+=1
    if s[-1]=="A":
      aend+=1
            
if ba==0:
  print(ans+min(bstart,aend))
else:
  if bstart>=1 and aend>=1:
    print(ans+ba-1+2+min(bstart-1,aend-1))
  else:
    print(ans+ba-1+max(min(1,bstart),min(1,aend)))

Solution (Python):
N = int(input())
S = []
ABcnt = 0
topABcnt = 0
topBbtmA = 0
topBbtmNO = 0
topNObtmA = 0
for _ in range(N):
  s = input()
  nowABcnt = s.count('AB')
  ABcnt += nowABcnt
  if s[0:2]=='AB':
    topABcnt += 1
  if s[0]=='B':
    if s[-1]=='A': topBbtmA += 1
    else: topBbtmNO += 1
  elif s[-1]=='A':
    topNObtmA += 1
  if nowABcnt!=0 or s[0]=='B' or s[-1]=='A': S.append(s)

ans = 0


ans += min(topBbtmNO, topNObtmA)
firstSurvivorCnt = max(topBbtmNO, topNObtmA) - ans

nikaisenGet = min(firstSurvivorCnt, topBbtmA)
ans += nikaisenGet

topBbtmA -= nikaisenGet
if ans == 0: ans += max(0, topBbtmA-1)
else: ans += topBbtmA 


print(ans+ABcnt)

Solution (Python):
N = int(input())
a = 0
b = 0
c = 0
v = 0

for i in range(N):
    S = input()
    for j in range(len(S)-1):
        if S[j:j+2] == 'AB':
            c += 1
    if S[0] == 'B' and S[-1] == 'A':
        v += 1
        continue
    if S[0] == 'B':
        b += 1
    if S[-1] == 'A':
        a += 1
'''
if v >= 1:
    if a < b:
        a += 1
    else:
        b += 1
'''
if a >= 1 or b >= 1:
    print(c + v + min(a, b))
else:
    print(c + max(0, v-1))

Solution (Python):
def solve():
    N = int(input())
    S = [input() for _ in range(N)]
    AB_count = 0
    
    head_B_count = 0
    tail_A_count = 0
    B_and_A_count = 0
    for si in S:
        
        AB_count += si.count('AB')
        
        list_si = list(si)
        if list_si[0] == 'B' and list_si[-1] == 'A':
            B_and_A_count += 1
            pass
        elif list_si[0] == 'B':
            head_B_count += 1
        elif list_si[-1] == 'A':
            tail_A_count += 1
    if B_and_A_count > 0:
        AB_count += B_and_A_count - 1
        if head_B_count > 0:
            AB_count += 1
            head_B_count -= 1
        if tail_A_count > 0:
            AB_count += 1
            tail_A_count -= 1
    AB_count += min(head_B_count, tail_A_count)
    print(AB_count)


if __name__ == '__main__':
    solve()

Solution (Python):
N= int(input())

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

if(b==0 or a==0): print(ans) 
elif(k>0 and k==a and b==k): print(ans+k-1)
else: print(ans + min(a,b,N-1))

Solution (Python):
N = int(input())
AB_number = 0
ab_cnt =0
a_cnt = 0
b_cnt = 0
for _ in range(N):
    s_i = input()
    AB_number += s_i.count("AB")
    if s_i[0] == "B" and s_i[-1] == "A":
        ab_cnt += 1
    elif s_i[-1] == "A":
        a_cnt += 1
    elif s_i[0] == "B":
        b_cnt += 1

if ab_cnt== 0:
    print(AB_number+min(a_cnt,b_cnt))
elif a_cnt != b_cnt:
    print(AB_number+(ab_cnt-1)+1+min(a_cnt,b_cnt))
elif a_cnt==0:
    print(AB_number+(ab_cnt-1)+min(a_cnt,b_cnt))
else:
    print(AB_number+(ab_cnt-1)+1+min(a_cnt,b_cnt))

Solution (Python):
n = int(input())
l = [input() for i in range(n)]
a = 0
b = 0
ba = 0
ab = 0
for i in range(n):
    if 'AB' in l[i]:
        for j in range(len(l[i])-1):
            if l[i][j] == 'A' and l[i][j+1] == 'B':
                ab += 1
    if l[i][0] =='B' and l[i][-1] == 'A':
        ba += 1
    elif l[i][-1] == 'A':
        a += 1
    elif l[i][0] == 'B':
        b += 1
if max(a,b) == 0 and ba <= 1:
    print(ab)
elif max(a,b) == 0:
    print(ab+ba-1)
else:
    print(min(a,b)+ab+ba)

Solution (Python):
n = int(input())
s = []
for i in range(0, n):
    s.append(input())

c = 0
a = 0
b = 0
ab = 0

for i in range(0, n):
    for j in range(1, len(s[i])):
        if s[i][j-1:j+1] == "AB":
            c += 1
    if s[i][-1] == "A" and s[i][0] == "B":
        ab += 1
    else: 
        if s[i][0] == "B":
            b += 1
        if s[i][-1] == "A":
            a += 1
            
s = c

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

s += min(a, b)

print(s)

Solution (Python):
N = int(input())
end_A_only = 0
start_B_only = 0
both = 0
count = 0
for i in range(N):
    s = input()
    count += s.count("AB")
    if s[-1] == "A" and s[0] == "B":
        both += 1
    elif s[-1] == "A":
        end_A_only += 1
    elif s[0] == "B":
        start_B_only += 1
    else:
        pass

if both == 0:
    count += min(end_A_only, start_B_only)

else:
    if end_A_only == 0 and start_B_only == 0:
        count += both - 1
    elif end_A_only == 0 or start_B_only == 0:
        count += both
    else:
        count += both + 1
        count += min(end_A_only - 1, start_B_only - 1)


print(count)

Solution (Python):
N=int(input())
lasta=0
headb=0
spec=0
cnt=0
for _ in range(N):
    s=str(input())
    for i in range(len(s)-1):
        if s[i:i+2]=="AB":
            cnt+=1
    if s[0]=="B" and s[-1]=="A":
        spec+=1
    elif s[-1]=="A":
        lasta+=1
    elif s[0]=="B":
        headb+=1
        
if abs(headb-lasta)>=spec:
    print(min(headb,lasta)+spec+cnt)   
elif lasta==0 and headb==0 and spec>0:
    print(spec-1+cnt)
elif lasta==0 and headb==0 and spec==0:
    print(cnt)
else:
    print(spec-abs(headb-lasta)+max(headb,lasta)+cnt)

Solution (Python):
n = int(input())
ab = 0
a = 0
b = 0
ba = 0
for i in range(n):
    s = input()
    ab = ab + s.count('AB')
    start_b = s[:1] == 'B'
    end_a = s[-1:] == 'A'
    if start_b:
        b = b + 1
    if end_a:
        a = a + 1
    if start_b and end_a:
        ba = ba + 1

b = b - ba
a = a - ba

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

Solution (Python):
N = int(input())

a = 0
b = 0
c = 0
ans = 0

for _ in range(N):
    s = input()
    flag = False
    for i in range(len(s)):
        if (flag == True) and (s[i] == "B"):
            ans += 1
            flag = False
        elif (s[i] == "A"):
            flag = True
        else:
            flag = False
    if (s[0] == "B" and s[-1] == "A"):
        c += 1
    elif (s[0] == "B"):
        b += 1
    elif (s[-1] == "A"):
        a += 1
if c > 0:
    ans += c - 1
    c = 1
    if (a > 0 or b > 0):
        ans += 1
print(ans + min(a, b))

Solution (Python):
from sys import stdin
    
def calc(s):
    lastA = 0
    firstB = 0
    both = 0
    interAB = 0
    for _s in s:
        if _s[0] == 'B' and _s[-1] == 'A':
            both += 1
        else:
            if _s[0] == 'B':
                firstB += 1
            if _s[-1] == 'A':
                lastA += 1
        
        interAB += _s.count('AB')

    if lastA == 0 and firstB == 0:
        both = max(both-1,0)
    return min(lastA + both, firstB + both) + interAB

def main():
    n = stdin.readline().rstrip()
    s = [x.rstrip() for x in stdin.readlines()]
    ans = calc(s)
    print(ans)
    
if __name__=='__main__':
    main()

Solution (Python):


import sys
def p(*a):
  s=" ".join(map(str,a))
  
  sys.stderr.write(s+"\n")


N = int(input())

ans=a=b=ba=ab=0

for _ in range(N):
	s = input()
	ab+=s.count('AB')
	if (s[0]=='B')and(s[-1]=='A'): ba+=1
	if (s[0]=='B')and(s[-1]!='A'): b +=1
	if (s[0]!='B')and(s[-1]=='A'): a +=1

ans+=ab
ans+=max(0,ba-1)


if ba>0:
	if b>0:
		ans+=1
		b-=1
	if a>0:
		ans+=1
		a-=1

ans+=min(b,a)


print(ans)


p(a,b,ba,ab)

Solution (Python):
N = int(input())
s = [input() for i in range(N)]
abc = sum([e.count("AB") for e in s]) 
bstart, aend = 0, 0
B_A = 0
for e in s:
    if e[0] == "B":
        bstart += 1
    if e[-1] == "A":
        aend += 1
    if e[0] == "B" and e[-1] == "A":
        B_A += 1
makedB_A = min(bstart-B_A, aend-B_A)
ans = 0

if bstart == 0 and aend == 0:
    ans = abc
elif bstart == aend:
    if makedB_A == 0:
        ans = abc + bstart - 1 
    else:
        ans = abc + bstart
else:
    ans = abc + min(bstart, aend)

    
print(ans)

Solution (Python):
N = int(input())
AB_number = 0
ab_cnt =0
a_cnt = 0
b_cnt = 0
for _ in range(N):
    s_i = input()
    AB_number += s_i.count("AB")
    if s_i[0] == "B" and s_i[-1] == "A":
        ab_cnt += 1
    elif s_i[-1] == "A":
        a_cnt += 1
    elif s_i[0] == "B":
        b_cnt += 1

if ab_cnt== 0:
    print(AB_number+min(a_cnt,b_cnt))
elif a_cnt != b_cnt:
    print(AB_number+(ab_cnt-1)+1+min(a_cnt,b_cnt))
elif a_cnt==0:
    print(AB_number+(ab_cnt-1)+min(a_cnt,b_cnt))
else:
    print(AB_number+(ab_cnt-1)+1+min(a_cnt,b_cnt))

Solution (Python):
N = int(input())
s = []
for i in range(N):
    s.append(input())
A = 0
B = 0
BA = 0
inAB = 0
for i in range(N):
    if s[i][-1] == "A" and s[i][0] == "B":
        BA += 1
    elif s[i][-1] == "A":
        A += 1
    elif s[i][0] == "B":
        B += 1
    inAB += s[i].count("AB")

if A == B and A == 0 and BA != 0: BA -= 1
print(min(A+BA,B+BA)+inAB)

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