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())

ans, tailA, headB, overlap = 0, 0, 0, 0
for _ in range(n):
    s = input()
    ans += s.count('AB')
    tailA += 1 * (s[-1] == 'A')
    headB += 1 * (s[0] == 'B')
    overlap += 1 * (s[-1] == 'A' and s[0] == 'B')

if overlap > 0:
    tailA -= overlap
    headB -= overlap
    ans += overlap - 1
    if tailA > 0:
        ans += 1
        tailA -= 1
    if headB > 0:
        ans += 1
        headB -= 1

ans += min(tailA, headB)
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

n = int(input())
a = 0
b = 0
ba = 0
ans = 0

for i in range(n):
  s = input().strip()
  leng = len(s)
  if s[0] == 'B' and s[-1] == 'A':
    ba += 1
    a += 1
    b += 1
  elif s[-1] == 'A':
    a += 1
  elif s[0] == 'B':
    b += 1
  for j in range(leng - 1):
    if s[j] == 'A' and s[j+1] == 'B':
      ans += 1

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

Solution (Python):
import sys
input = sys.stdin.readline

n = int(input())
a = 0
b = 0
ba = 0
ans = 0

for i in range(n):
  s = input().strip()
  leng = len(s)
  if s[0] == 'B' and s[-1] == 'A':
    ba += 1
    
    
  elif s[-1] == 'A':
    a += 1
  elif s[0] == 'B':
    b += 1
  for j in range(leng - 1):
    if s[j] == 'A' and s[j+1] == 'B':
      ans += 1

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

Solution (Python):
from sys import exit

N = int(input())

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

count = 0
a_count = 0
b_count = 0
a_b_count = 0

for s in S:
	if s[0] == 'B' and not s[-1] == 'A':
		b_count += 1
	if not s[0] == 'B' and s[-1] == 'A':
		a_count += 1
	if s[0] == 'B' and s[-1] == 'A':
		a_b_count += 1
	for i in range(len(s) - 1):
		if s[i] == 'A':
			if s[i + 1] == 'B':
				count += 1

if a_b_count == 0:
	print(count + min(a_count, b_count))
else:
	if not (a_count == 0 and b_count == 0):
		a_count += 1
		b_count += 1
	print(count + min(a_count, b_count) + a_b_count - 1)

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

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

Solution (Python):
import sys,heapq
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False
def ddprint(x):
  if DBG:
    print(x)

ab = 0
leftb = 0
righta = 0
lbra = 0
n = inn()
for i in range(n):
    s = input()
    if s[0] == 'B' and s[-1] == 'A':
        lbra += 1
    if s[0] == 'B':
        leftb += 1
    if s[-1] == 'A':
        righta += 1
    for j in range(len(s)-1):
        if s[j:j+2] == 'AB':
            ab += 1

if DBG:
    print("ab {} l {} r {} lbra {} ".format(ab, leftb, righta, lbra))
print(ab + (max([0,lbra-1]) if lbra==leftb and leftb==righta else min([leftb, righta])))

Solution (Python):
N = int(input())
inside_str = 0
tail_a = 0
head_b = 0
 
doubleAB = 0
 
for i in range(N):
  str = input()
  inside_str = inside_str + str.count('AB')
  
  if(str[-1] == 'A' and str[0] == 'B'):
  	doubleAB = doubleAB + 1
  elif (str[-1] == 'A'):
    tail_a = tail_a + 1
  elif (str[0] == 'B'):
    head_b = head_b + 1
 
if(tail_a == 0 and head_b == 0):
  if(doubleAB == 0):
    total_cand = inside_str
  else:
    total_cand = inside_str + doubleAB - 1
else:
  total_cand = inside_str + doubleAB + min(tail_a, head_b)

print(total_cand)

Solution (Python):
n = int(input())
l = []
cnt = 0
te = [0,0,0]
for i in range(n):
    l.append(input())
for i in l:
    if i[0] == "B" and i[-1] == "A":
        te[0] += 1
    elif i[0] == "B":
        te[1] += 1
    elif i[-1] == "A":
        te[2] += 1
    for j in range(len(i)-1):
        if i[j] == "A" and i[j+1] == "B":
            cnt += 1
if te[0] != 0:
    te[0] -= 1
    if te[1]>0:
        te[0] += 1
        te[1] -= 1
    if te[2]>0:
        te[0] += 1
        te[2] -= 1
print(cnt+te[0]+min(te[1],te[2]))

Solution (Python):
n = input()

ans = 0

x = y = z = 0
for _ in xrange(n):
    s = raw_input().strip()
    for i in xrange(len(s) - 1):
        if s[i] == 'A' and s[i + 1] == 'B': ans += 1

    if s[0] == 'B' and s[-1] == 'A': z += 1
    elif s[0] == 'B': y += 1
    elif s[-1] == 'A': x += 1

m = min(x, y)
ans += m + z

if x == 0 and y == 0:
    if z > 0: ans -= 1

ans = max(0, ans)
print ans

Solution (Python):
N = int(input())
s = 0
front_b = 0
tail_a = 0
has_a_b = 0
for i in range(N):
    t = input()
    s += t.count('AB')
    if t[0] == 'B' and t[len(t) -1] == 'A':
        has_a_b += 1
    elif t[0] == 'B':
        front_b += 1
    elif t[len(t) -1] == 'A':
        tail_a += 1

res = s
if has_a_b > 0:
    if front_b > 0 and tail_a > 0:
        res += has_a_b + 1 + min(front_b-1, tail_a-1)
    elif front_b > 0 or tail_a > 0:
        res += has_a_b
    else:
        res += has_a_b - 1
else:
    res += min(front_b, tail_a)

print(res)

Solution (Python):
import sys
N=int(input())
l=[]
q=0
cnt=0
BA=0
BN=0
NA=0

if 1<=N<=10**4:
	for i in range(N):
		st=input()
		if 2<=len(st)<=10:
			l.append(st)
		else:
			sys.exit()
	for i in l:
		cnt+=i.count('AB')
		if i[0]=='B' and i[-1]=='A':
			BA+=1
		elif i[0]=='B':
			BN+=1
		elif i[-1]=='A':
			NA+=1
	if BA>0:
		cnt+=BA-1
		if BN>0:
			cnt+=1
			BN-=1
		if NA>0:
			cnt+=1
			NA-=1
	if BN>0 and NA>0:
		cnt+=min(BN,NA)
	print(cnt)

Solution (Python):
n = int(input())
ans, a, b, ba = 0, 0, 0, 0

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

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

Solution (Python):
n=int(input())
s=[input() for i in range(n)]
ans=0
c=[0,0,0]
for i in range(n):
    ans+=s[i].count('AB')
    if s[i][0]=='B'and s[i][-1]=='A':
        c[0]+=1
    elif s[i][0]=='B':
        c[1]+=1
    elif s[i][-1]=='A':
        c[2]+=1

if c[0]==0:
    ans+=min(c[1:3])
elif sum(c[1:3])==0:
    ans+=c[0]-1
elif sum(c[1:3])>0:
    ans+=c[0]+min(c[1:3])


print(ans)

Solution (Python):
import math
N = int(input())
S = []
counterBX = 0
counterXA = 0
counterBXA = 0
counterAB = 0
for i in range(N):
    S.append(input())
    if S[i][0] == 'B' and S[i][-1] != 'A':
        counterBX +=1
    if S[i][0] != 'B' and S[i][-1] == 'A':
        counterXA +=1
    if S[i][0] == 'B' and S[i][-1] == 'A':
        counterBXA +=1
    if len(S[i]) > 1:
        for j in range(len(S[i]) - 1):
            if S[i][j] == 'A' and S[i][j+1] == 'B':
                counterAB +=1
minval = min(counterBX,counterXA)
sa = abs(counterBX - counterXA)

if counterBXA == 0:
    ans = counterAB + min(counterXA,counterBX)
elif counterBXA >=1 and counterXA == 0 and counterBX == 0:
    ans = counterAB + counterBXA - 1
elif counterBXA >=1 and counterXA >=1 and counterBX == 0:
    ans = counterAB + (counterBXA - 1) + 1 + min(counterXA - 1,counterBX)
elif counterBXA >=1 and counterXA == 0 and counterBX >=1:
    ans = counterAB + (counterBXA - 1) + 1 +min(counterXA,counterBX - 1)
else:
    ans = counterAB + (counterBXA - 1) + 2 + min(counterXA - 1, counterBX - 1)
print(ans)

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())
B_=0
_A=0
AB = 0
B_A=0
for i in range(n):
    s = input()
    AB += s.count('AB')
    if s[0] == 'B' and s[-1] == "A":
        B_A += 1
    elif s[0] == 'B':
        B_ += 1
    elif s[-1] == 'A':
        _A += 1
    
if B_A == 0:
    ans = AB + min(_A,B_)
else:
    if _A == 0 and B_ == 0:
        ans = AB + (B_A-1)
    else:
        ans = AB + B_A + min(_A,B_)
        
print(ans)

Solution (Python):
N = int(input())
Ss=[input() for s in range(N)]
inside_count = [s.count("AB") for s in Ss]
start_count = len([s for s in Ss if s[0]=="B" and s[-1]!="A"])
end_count = len([s for s in Ss if s[-1]=="A" and s[0]!="B"])
multi_count = len([s for s in Ss if s[0]=="B" and s[-1]=="A"])
if end_count==0 and start_count==0:
    if multi_count<=0:
        print(sum(inside_count))
    else:
        print(sum(inside_count)+multi_count-1)
else:
    print(sum(inside_count)+min(start_count,end_count)+multi_count)

Solution (Python):
N = int(input())
Ss=[input() for s in range(N)]
count = [s.count("AB") for s in Ss]
headcount = len([s for s in Ss if s[0]=="B" and s[-1]!="A"])
outcount = len([s for s in Ss if s[-1]=="A" and s[0]!="B"])
multicount = len([s for s in Ss if s[0]=="B" and s[-1]=="A"])
if outcount==0 and headcount==0:
    if multicount<=0:
        print(sum(count))
    else:
        print(sum(count)+multicount-1)
else:
    print(sum(count)+min(headcount,outcount)+multicount)

Solution (Python):
N = int(input())
ans = 0
b_x = 0
b_a = 0
x_a = 0
y = 0
for _ in range(N):
  s = input()
  ans += s.count('AB')
  if s[0] == 'B' and s[-1] == 'A':
    b_a += 1
  elif s[0] == 'B':
    b_x += 1
  elif s[-1] == 'A':
    x_a += 1
  else:
    y += 1
if x_a >= 1 and b_x >= 1:
  ans += b_a + 1	
elif x_a >= 1 or b_x >= 1:
  ans += b_a
else:
  if b_a > 0:
    ans += b_a - 1
if x_a - 1 <= 0 or b_x -1 <= 0:
  ans += 0
else:
  ans += min(x_a - 1, b_x - 1)
print(ans)

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

A_count = B_count = BA_count = 0

ans = 0
for i in range(N):
    line = raw_input().strip()

    if line[0] == 'B':
        B_count += 1
        if line[-1] == 'A':
            BA_count += 1
            A_count += 1
    else:
        if line[-1] == 'A':
            A_count += 1

    for j in range(len(line)-1):
        if line[j:j+2] == 'AB':
            ans += 1


if A_count != 0 and B_count != 0:
    if A_count == B_count:
        ans += A_count
        if B_count == BA_count:
            ans -= 1
    elif A_count > B_count:
        ans += B_count
    else:
        ans += A_count

print(ans)

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

ans = 0
last_a_num = 0
first_b_num = 0
multi_ba_num = 0
for _ in range(N):
    s = input()
    ans += s.count('AB')
    if s[-1] == 'A':
        last_a_num += 1
    if s[0] == 'B':
        first_b_num += 1
    if s[-1] == 'A' and s[0] == 'B':
        multi_ba_num += 1

connect_num = min(last_a_num, first_b_num)
if last_a_num == first_b_num and last_a_num == multi_ba_num:
    if connect_num > 0:
        connect_num -= 1
ans += connect_num

print(ans)

Solution (Python):
n = int(input())
c,x,y,z = 0,0,0,0
for _ in range(n):
    s = input()
    c+=s.count("AB")
    if s[0]=="B" and s[-1]=="A": x+=1
    elif s[-1]=="A": y+=1
    elif s[0]=="B": z+=1
y,z = min(y,z),max(y,z)
if x and y+z: print(c+x+y)
elif x: print(c+x-1)
else: print(c+y)

Solution (Python):
count = 0
n = int(raw_input())
endA = 0
begB = 0
both = 0
for i in xrange(n):
    word = raw_input()
    for j in xrange(len(word)-1):
        if word[j] == "A" and word[j+1] == "B":
            count += 1
    if word[0] == "B" and word[len(word)-1] == "A":
        both += 1
    elif word[0] == "B":
        begB += 1
    elif word[len(word) - 1] == "A":
        endA += 1

if endA > 0 and both > 0:
    count += 1
    endA -= 1
if begB > 0 and both > 0:
    count += 1
    begB -= 1
if both > 0:
    count += both - 1
count += min(endA, begB)
print count

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

end_A = 0
start_B = 0
AB_count = 0
both = 0

for i in range(N):
    if 'AB' in s[i]:
        AB_count += s[i].count('AB')
    if s[i][0] == 'B':
        start_B += 1
    if s[i][len(s[i])-1] == 'A':
        end_A += 1
    if s[i][0] == 'B' and s[i][len(s[i])-1] == 'A':
        both += 1
start_B -= both
end_A -= both

if both == 0:
    print(AB_count + min(start_B, end_A))
elif end_A + start_B > 0:
    print(AB_count + both + min(start_B, end_A))
else:
    print(AB_count + both - 1)

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

end_A = 0
start_B = 0
AB_count = 0
both = 0

for i in range(n):
    if 'AB' in s[i]:
        AB_count += s[i].count('AB')
    if s[i][0] == 'B':
        start_B += 1
    if s[i][len(s[i])-1] == 'A':
        end_A += 1
    if s[i][0] == 'B' and s[i][len(s[i])-1] == 'A':
        both += 1
start_B -= both
end_A -= both

if both == 0:
    print(AB_count + min(start_B, end_A))
elif end_A + start_B > 0:
    print(AB_count + both + min(start_B, end_A))
else:
    print(AB_count + both - 1)

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

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

Solution (Python):
N = int(input())
Bst = 0
Alas = 0
BA = 0
AB = 0
for i in range(N):
    s = input()
    if s[0] == 'B' and s[-1] == 'A':
        BA += 1
    elif s[0] == 'B':
        Bst += 1
    elif s[-1] == 'A':
        Alas += 1
    AB += s.count('AB')
    
if Bst >= 1 and Alas >= 1:
    AB += (BA+1+min(Bst-1, Alas-1))
    
elif Bst == 0 and Alas == 0:
    if BA >= 1:
        AB += (BA-1)
else:
    AB += BA
    
print(AB)

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

acount = 0
bcount = 0
abcount = 0
ans = 0

for _ in range(n):
    s = input()
    if s[0] == 'B':
        bcount += 1
    if s[-1] == 'A':
        acount += 1
    if s[0] == 'B' and s[-1] == 'A':
        abcount += 1
    for i in range(len(s)-1):
        if s[i] == 'A' and s[i+1] == 'B':
            ans += 1

if abcount == 0 or max(abcount,acount,bcount) != abcount:
    print(ans + min(acount,bcount))
else:
    print(ans + min(acount,bcount) - 1)

Solution (Python):
x = y = z = 0
ans = 0

n = int(input())
for _ in range(n):
    s = input()
    ans += s.count("AB")

    if s[0] == 'B' and s[-1] == 'A':
        x += 1
    elif s[-1] == 'A':
        y += 1
    elif s[0] == 'B':
        z += 1

if y == 0 and z == 0:
    ans += max(x - 1, 0)
else:
    ans += x + min(y, z)

print(ans)

Solution (Python):
n=int(input())
count1=0
count2=0
count3=0
ans=0
for i in range(n):
	flag=0
	s=str(input())
	for j in range(len(s)-1):
		if(s[j]+s[j+1]=='AB'):
			ans+=1

	if(s[-1]=='A' and s[0]=='B'):
		count1+=1
	elif(s[0]=='B'):
		count2+=1
	elif(s[-1]=='A'):
		count3+=1

if(count3>0):
	count3-=1
	ans+=count1
	if(count2>0):
		ans+=1
		count2-=1
else:
	if(count1>0):
		ans+=count1-1
		if(count2>0):
			ans+=1
			count2-=1

while(count3>0 and count2>0):
	ans+=1
	count2-=1
	count3-=1

print(ans)

Solution (Python):
import re

n = int(raw_input())

ab = 0
a = 0
b = 0

ans = 0
for i in range(0, n):
    s = raw_input()
    find_list = re.findall("AB", s)
    ans += len(find_list)

    if re.search("^B.*A$", s) is not None:
        ab += 1
        continue

    if re.search(".*A$", s) is not None:
        a += 1

    if re.search("^B.*", s) is not None:
        b += 1

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

else:
    ans += min(a, b)

print(ans)

Solution (Python):
N = int(input())
S =[0]*N
for i in range(N):
    S[i] = str(input())

ans = int(0)
a=int(0)
b=int(0)
c=int(0)
R=int(0)

for i in range(N):
    AB = S[i].split("AB")
    R = len(AB)-1

    ans += R
    R = 0

for i in range(N):
    if S[i].endswith('A') : a +=1
    if S[i].startswith('B') : b +=1
    if S[i].endswith('A') and S[i].startswith('B'): c +=1

if c==0 and c==a and c==b: R=0
elif c==a and c==b:
    R = min(a, b)-1
else : R=min(a, b)

ans += R

print(ans)

Solution (Python):
N = int(input())
S = (list(input() for i in range(N)))
cot = 0
cot_a = 0
cot_b = 0
cot_ab = 0

for s in S:
    for j in range(len(s)-1):
        if s[j] == "A" and s[j+1] == "B":
            cot += 1

for s in S:
    if s[-1] == 'A' and s[0] == 'B':
        cot_ab += 1
    elif s[-1] == 'A':
        cot_a += 1
    elif s[0] == 'B':
        cot_b += 1

if cot_ab == 0:
    cot += min(cot_a, cot_b)
elif cot_a + cot_b >0:
    cot += cot_ab + min(cot_a, cot_b)
else:
    cot += cot_ab - 1
print(cot)

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

    a = 0
    b = 0
    ab = 0

    count = 0

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


    for i in range(N):
        if (s[i][0] == 'B' or s[i][-1] == 'A'):
            if (s[i][0] == 'B' and s[i][-1] == 'A'):
                ab += 1
            elif (s[i][0] == 'B'):
                b += 1
            else:
                a += 1

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


if __name__=='__main__':
    main()

Solution (Python):
N=int(input())
A=0
B=0
AB=0
ans=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[-1]=="A":
    AB+=1
  elif s[0]=="B":
    B+=1
  elif s[-1]=="A":
    A+=1
  else:
    continue

if AB==0:
  ans+=min(A,B)
else:
  if A==0 and B==0:
    ans+=(AB-1)
  else:
    ans+=(AB+min(A,B))
    
print(ans)

Solution (Python):
n = int(input())
s = [input() for i in range(n)]
count,a,b,ab = 0,0,0,0
for word in s:
  count += word.count("AB")
  if word[0] == "B" and word[-1] =="A":
    ab += 1
  elif word[0] == "B":
    b += 1
  elif word[-1] == "A":
    a += 1
  else:
    pass
if ab == 0:
  print(min([a,b])+count)
else:
  if a == 0 and b == 0:
    print(ab-1+count)
  elif a == 0 or b == 0:
    print(ab+count)
  else:
    print(ab+1+min([a-1,b-1])+count)

Solution (Python):
N = int(input())
str_list = [input() for i in range(N)]

_a_cnt = 0
b__cnt = 0
b_a_cnt = 0
other_cnt = 0

ans = 0
for s in str_list:
    if s[0] == "B" and s[-1] == "A":
        b_a_cnt += 1
    elif s[-1] == "A":
        _a_cnt += 1
    elif s[0] == "B":
        b__cnt += 1
    else:
        other_cnt += 1
    ans += s.count("AB")

if _a_cnt > 0 and b__cnt > 0:
    ans += b_a_cnt + 1 + min(_a_cnt - 1, b__cnt - 1)
elif _a_cnt > 0 or b__cnt > 0:
    ans += b_a_cnt
elif b_a_cnt >= 2:
    ans += b_a_cnt - 1

print(ans)

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

Solution (Python):
n = int(input())
cnt = 0
ca = 0
cb = 0
cab = 0
for i in range(n):
    tmp = input()
    cnt += tmp.count("AB")
    if tmp[0] == "B" and tmp[-1] == "A":
        cab += 1
    elif tmp[0] == "B":
        cb += 1
    elif tmp[-1] == "A":
        ca += 1
if ca == cb == 0 and cab > 0:
    cnt -= 1
print(cnt + cab + min(ca, cb))

Solution (Python):
n = int(input())
cnt = 0
ca = 0
cb = 0
cab = 0
for i in range(n):
    tmp = input()
    cnt += tmp.count("AB")
    if tmp[0] == "B" and tmp[-1] == "A":
        cab += 1
    elif tmp[0] == "B" and tmp[-1] != "A":
        cb += 1
    elif tmp[0] != "B" and tmp[-1] == "A" :
        ca += 1
if ca == cb == 0 and cab > 0:
    cnt -= 1
print(cnt + cab + min(ca, cb))

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

Solution (Python):
n = int(input())
xa, bx, bxa = 0, 0, 0
ans = 0
for _ in range(n):
	s = input()
	for i in range(len(s)-1):
		if s[i] == "A" and s[i+1] == "B":
			ans += 1
	if s[0] == "B":
		if s[-1] == "A":
			bxa += 1
		else:
			bx += 1
	elif s[-1] == "A":
		xa += 1

if xa == bx:
	if xa == 0:
		print(ans+max(0, bxa-1))
	else:
		print(ans+xa+bxa)
else:
	print(ans+bxa+min(xa, bx))

Solution (Python):
N=int(input())
L=[]
ans=0
A=0
B=0
AB=0
for i in range (N):
        L.append(input())
        ans+=L[i].count("AB")
        A+=L[i][-1].count("A")
        B+=L[i][0].count("B")
        if L[i][-1]=="A" and L[i][0]=="B":
                AB += 1
if A>=B:
        ans+=B
        if AB==A and AB>=1:
                ans-=1
else:
        ans+=A
        if AB==B and AB>=1:
                ans-=1            
print(ans)

Solution (Python):
n = int(input())
ans, ba, xa, bx = 0, 0, 0, 0

for i in range(n):
    s = input()
    ans += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        ba += 1
    if s[0] == "B" and s[-1] != "A":
        bx += 1
    if s[0] != "B" and s[-1] == "A":
        xa += 1

if xa == 0 and bx == 0:
    ans += max(0, ba-1)
else:
    ans += ba
    ans += min(xa, bx)

print(ans)

Solution (Python):
def solve():
  n = int(input())
  s = [input() for _ in range(n)]
  ans = 0
  righta = 0
  leftb = 0
  double = 0
  
  for i in range(n):
    tmp = s[i]
    targetlength = len(tmp)
    if tmp[0] =='B':leftb += 1
    if tmp[-1] == 'A': righta += 1
    if tmp[0] =='B' and tmp[-1] =='A':
      leftb -= 1
      righta -= 1
      double += 1
    for j in range(targetlength-1):
      if tmp[j] =='A' and tmp[j+1] == 'B': ans += 1
  ans += min(righta,leftb)
  righta -= min(righta,leftb)
  leftb -= min(righta,leftb)
  for i in range(double):
    if righta > 0:
      ans += 1
      continue
    if leftb > 0:
      ans += 1
      continue
    if righta == 0 and leftb == 0:
      righta += 1
      leftb += 1

  
  print(ans)
solve()

Solution (Python):

N = int(input())
S = [str(input()) for _ in range(N)]
Aend = 0
Bstart = 0
BstartAend = 0
ABcon = 0
for s in S:
    if s[0] == "B" and s[-1] == "A":
        BstartAend += 1
    elif(s[0] == "B"):
        Bstart += 1
    elif(s[-1] == "A"):
        Aend += 1
    ABcon += s.count("AB")

tmp = min(Aend, Bstart)
if BstartAend == 0:
    print(ABcon + tmp)
else:
    if max(Aend, Bstart) == 0:
        print(ABcon + BstartAend - 1)
    else:
        print(ABcon + BstartAend + tmp)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

i  = lambda: int(input())
s  = lambda: input()

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

if b_a == 0:
    print(count + min([b, a]))
else:
    if a + b == 0:
        print(count + b_a -1)
    else:
        print(count + b_a + min([b, a]))

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

Solution (Python):
n = int(input())
AB,B_A, _A,B_ = 0,0,0,0
for _ in range(n):
    s = input()
    AB +=s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        B_A +=1
    elif s[0] == 'B':
        B_ +=1
    elif s[-1] == 'A':
        _A +=1
if B_A == 0:
    print(AB + min(B_,_A))
else:
    if _A + B_==0:
        print(AB + B_A -1)
    else:
        print(AB + B_A + min(B_,_A))

Solution (Python):
N = int(input())
inward = 0
top = 0
fin = 0
dbl = 0

for _ in range(N):
    s = input()
    n = len(s)
    if s[0] == 'B' and s[n-1] == 'A':
        dbl += 1
    elif s[0] == 'B' and s[n-1] != 'A':
        top += 1
    elif s[0] != 'B' and s[n-1] == 'A':
        fin += 1
    for i in range(n-1):
        if s[i] == 'A' and s[i+1] == 'B':
            inward += 1

ans = inward
MAX = N-1
if dbl>0:
    ans += dbl-1
    MAX -= (dbl-1)
    if top >0:
        ans +=1
        top -=1
        MAX -=1
    if fin >0:
        ans +=1
        fin -=1
        MAX -=1
ans += min(top,fin,MAX)
print(ans)

Solution (Python):
n=int(input())
s=[input() for _ in range(n)]
ans=0
for i in s:
    ans+=i.count("AB")
endA=[]
startB=[]
AND=[]
for i in s:
    if i[0]=="B" and i[-1]=="A":
        AND+=[i]
    elif i[0]=="B":
        startB+=[i]
    elif i[-1]=="A":
        endA+=[i]
MIN=min(len(endA),len(startB))
if not AND:
    print(ans+MIN)
elif endA or startB:
    print(ans+MIN+len(AND))
else:
    print(ans+len(AND)-1)

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

a = 0
b = 0
c = 0
d = 0

ans = 0

for x in s:
    if x[0] == 'B':
        if x[-1] == 'A':
            c += 1
        else:
            b += 1
    else:
        if x[-1] == 'A':
            a += 1
        else:
            d += 1
    for i in range(len(x) - 1):
        if x[i] == 'A' and x[i+1] == 'B':
            ans += 1

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

print(ans)

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

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



A = 0
B = 0
AB = 0

for i in s:
	if i[0] == 'B' and i[-1] == 'A':
		AB += 1
	elif i[0] == 'B':
		B += 1
	elif i[-1] == 'A':
		A += 1

if AB == 0:
	ans += min(A,B)
else:
	ans += AB + min(A,B) + (-1 if (A+B) == 0 else 0)

print(ans)

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

count_A = 0
count_B = 0
count_AB = 0
count = 0

for i in range(N):
    flag = False
    for j in range(len(s[i])):
        if j == 0 and s[i][j] == "B":
            count_B += 1
            flag = True
        if s[i][j] == "A":
            if j == len(s[i]) - 1:
                count_A += 1
                if flag:
                    count_A -= 1
                    count_B -= 1
                    count_AB += 1
            elif s[i][j+1] == "B":
                count += 1

if count_A == 0 and count_B == 0:
    if count_AB > 0:
        count += count_AB - 1

else:
    if count_A <= count_B:
        count += count_A
    else:
        count += count_B

    count += count_AB

print(count)

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

count_A = 0
count_B = 0
count_AB = 0
count = 0

for i in range(N):
    flag = False
    for j in range(len(s[i])):
        if j == 0 and s[i][j] == "B":
            count_B += 1
            flag = True
        if s[i][j] == "A":
            if j == len(s[i]) - 1:
                count_A += 1
                if flag:
                    count_A -= 1
                    count_B -= 1
                    count_AB += 1
            elif s[i][j+1] == "B":
                count += 1

if count_A == 0 and count_B == 0:
    if count_AB > 0:
        count += count_AB - 1

else:
    if count_A <= count_B:
        count += count_A
    else:
        count += count_B

    count += count_AB

print(count)

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

ab, start_b, end_a, b_a = 0, 0, 0, 0

if n == 1:
  print(input().count('AB'))
else:
  for _ in range(n):
    chars = input()
    if 'AB' in chars:
      ab += chars.count('AB')
    if chars[0] == 'B':
      if chars[-1] == 'A':
        b_a += 1
      else:
        start_b += 1
    elif chars[-1] == 'A':
      end_a += 1
  if start_b + end_a == 0 and b_a:
    print(ab + b_a - 1)
  else:
    print(ab + b_a + min(start_b, end_a))

Solution (Python):
n = int(input())
 
ans = 0
A = 0
B = 0
AB = 0
for _ in range(n):
    s = input()
    ans += s.count('AB')
    
    if s[-1] == 'A' and s[0] != 'B':
        A += 1 
    if s[0] == 'B' and s[-1] != 'A':
        B += 1
    if s[-1] == 'A' and s[0] == 'B':
        AB += 1
    
ans += min(A+AB, B+AB)


if AB>0 and A+B==0:
    ans -= 1

print(ans)

Solution (Python):
from re import findall


def main():
    N = int(input())
    num_full = 0
    begin_b = 0
    end_a = 0
    both = 0
    for i in range(N):
        s_i = input()
        num_full += len(findall('AB', s_i))
        if s_i[0] == 'B' and s_i[-1] == 'A':
            both += 1
        if s_i[0] == 'B':
            begin_b += 1
        if s_i[-1] == 'A':
            end_a += 1
    ans = num_full
    if begin_b == end_a:
        if begin_b == both:
            ans += max(begin_b - 1, 0)
        else:
            ans += begin_b
    else:
        ans += min(begin_b, end_a)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
































n = int(input())

a,b=0,0
flg = 1
ans = 0

for _ in range(n):

	s = input()

	if(s[0] == 'B' and s[-1] == 'A'):
		a += 1
		b += 1

	elif(s[0] == 'B'):
		b += 1
		flg = 0


	elif(s[-1] == 'A'):
		a += 1
		flg = 0 


	ans += s.count('AB')


if(min(a,b) > 0):
	ans += min(a,b) - flg

print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
n=int(input())
ans=0
cntA,cntB,cntAB=0,0,0
for _ in range(n):
    s=input().strip()
    ans+=s.count('AB')
    if s[0]=='B':
        cntB+=1
        if s[-1]=='A':
            cntAB+=1
    if s[-1]=='A':
        cntA+=1
ans+=min(cntA,cntB)
if cntA==cntB==cntAB and cntAB:
    ans-=1
print(ans)

Solution (Python):
N = int(input())
BA = 0
B_ = 0
_A = 0
ans = 0
s = []
for i in range(N):
    s.append(input())
    ans += s[i].count('AB')
    if s[i][0] is 'B' and s[i][-1] is 'A':
        BA += 1
    elif s[i][0] is 'B' and s[i][-1] is not 'A':
        B_ += 1
    elif s[i][0] is not 'B' and s[i][-1] is 'A':
        _A += 1

if BA == 0:
    ans += min(B_, _A)
    print(ans)
elif B_ == 0 and _A == 0:
    ans += BA - 1
    print(ans)
else:
    ans += BA + min(B_, _A)
    print(ans)

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

S = []
for n in range(N):
	S.append(input())

Acount  = 0
Bcount  = 0
ABcount = 0
BXAcount = 0

for s in S:
	if s[0]  == "B" :
		Bcount += 1
	if s[-1] == "A" :
		Acount += 1
	for i in range(len(s)-1) : 
		if s[i] == "A" and s[i+1] == "B" :
			ABcount += 1
	if s[0] == "B" and s[-1] == "A" :
		BXAcount += 1





if BXAcount >= 1 and Acount == BXAcount and Bcount == BXAcount :
		print(min(Acount, Bcount) + ABcount -1)
else:
	print(min(Acount, Bcount) + ABcount)