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())
C = {0:0,1:0,2:0}
cnt = 0
for _ in range(N):
    s = input().strip()
    if s[0]=="B" and s[-1]!="A":
        C[0] += 1
    elif s[0]!="B" and s[-1]=="A":
        C[1] += 1
    elif s[0]=="B" and s[-1]=="A":
        C[2] += 1
    for i in range(len(s)-1):
        if s[i:i+2]=="AB":
            cnt += 1
if C[2]>0:
    cnt += C[2]-1
    if C[0]>0:
        cnt += 1
        C[0] -= 1
    if C[1]>0:
        cnt += 1
        C[1] -= 1
cnt += min(C[0],C[1])
print(cnt)

Solution (Python):
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 ab_cnt > 1:
        ans += max(ab_cnt - 1, 0)
        ab_cnt = 1
    if a_cnt <= b_cnt:
        ans += a_cnt
        a_cnt = 0
        b_cnt -= a_cnt
    elif b_cnt < a_cnt:
        ans += b_cnt
        b_cnt = 0
        a_cnt -= b_cnt
    if ab_cnt > 0 and (a_cnt > 0 or b_cnt > 0):
        ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):








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

if both > 0:
    ans += both - 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())
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
if not hb and not ta and not hbta:
  print(ab)
elif not hb and not ta:
  print(ab + hbta - 1)
elif not hb or not ta:
  print(ab + hbta)
else:
  print(ab + hbta + min(hb, ta))

Solution (Python):
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 and begin_b != 0:
    ans -= 1
print(ans)

Solution (Python):
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 BA > 0:
    BA -= 1
    A += 1
    ans += 1
  elif B > 0:
    B -= 1
    ans += 1
  else:
    break
print(ans)

Solution (Python):
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 and countB_A:
    count-=1
print(count+min(countA,countB))

Solution (Python):
import sys



def count_ab(a_s):
	ret=0
	is_a=False
	for c in a_s:
		if c == 'A':
			is_a=True
		elif is_a and c == 'B':
			ret+=1
			is_a=False
		else:
			is_a=False
	return ret
		
def main():
	n=int(input())
	sl=[]
	ret=0
	n_top_b=0
	n_buttom_a=0
	n_tb_ab=0
	
	for i in range(n):
		aa = input()
		ret+=count_ab(aa)
		if aa[0]=='B':
			n_top_b+=1
		if aa[-1]=='A':
			n_buttom_a+=1
		if aa[0]=='B' and aa[-1]=='A':
			n_tb_ab+=1
	
	min_n_ab=min(n_buttom_a, n_top_b)
	if min_n_ab == 0:
		return ret
	
	ret += min_n_ab
	if n_buttom_a==n_top_b and n_top_b==n_tb_ab:
		ret -= 1
	
	return ret
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
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==0:
	print(ans+max(ab-1,0))
else:
	print(ans+max(0,ab+min(a,b)))

Solution (Python):
import numpy as np

N = int(input())

cnt_AB = 0
xA = 0
Bx = 0
BA = 0
xx = 0
for i in range(N):
  s = input()
  cnt_AB += s.count('AB')
  if s[0] == 'B':
    if s[-1] == 'A':
      BA += 1
    else:
      Bx += 1
  else:
    if s[-1] == 'A':
      xA += 1
    else:
      xx += 1

A = xA + BA
B = Bx + BA
ans = cnt_AB

if A > B:
  ans += B
elif A < B:
  ans += A
elif A > 0:
  ans += A
  if xA == 0:
    ans -= 1

print(ans)

Solution (Python):
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 + max(0, min(N-1, min(A, B)))
print(answer)

Solution (Python):
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+cnt_ab+min(cnt_a,cnt_b)

print(ans)

Solution (Python):
import math

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    
    return divisors







n = int(input())
ss=[]
ans=0
counta=0
countb=0
countab=0

for i in range(n):
    ss.append(input())
    ca = 'A' == ss[-1][-1]
    cb = 'B' == ss[-1][0]
    for i in range(len(ss[-1])-1):
        if(ss[-1][i]=='A' and ss[-1][i+1]=='B'):
            ans+=1
            
    if(ca and cb):
        countab+=1
    elif(ca):
        counta+=1
    elif(cb):
        countb+=1
if(counta+countb==0):
    print(ans+max(countab-1,0))
elif(counta>=1 and countb>=1):
    print(ans+min(counta,countb)+countab)
else:
    print(ans+countab)

Solution (Python):
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)

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

Solution (Python):
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) or count == 0:
    print(minAB + min(AB))
else:
    print(minAB + min(AB)-1)

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

N=int(readline())
s=[readline().rstrip().decode("utf-8") for _ in range(N)]


A,B,AB,BA=0,0,0,0
for ss in s:
  for i in range(len(ss)-1):
    if ss[i]=="A" and ss[i+1]=="B":
      AB+=1
  if ss[-1]=="A": A+=1
  if ss[0]=="B": B+=1
  if ss[-1]=="A" and ss[0]=="B": BA+=1
A-=BA
B-=BA


if BA>1:
  AB+=BA-1
  BA=1


if BA==1:
  if A>0 or B>0:
    if A>0:
      AB+=1
      A-=1
    if B>0:
      AB+=1
      B-=1
    BA-=1


AB+=min(A,B)
print(AB)

Solution (Python):
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    n = int(input())
    d = defaultdict(int)
    ans = 0
    for i in range(n):
        s = input()
        ans+=s.count("AB")
        if s[0] == "B" and s[-1] == "A":
            d[0] += 1
        elif s[0] == "B":
            d[1]+=1
        elif s[-1] == "A":
            d[2] += 1

    if d[0] > 0:
        ans += d[0] - 1
        if d[1] > 0 and d[2] > 0:
            ans += 2
            ans += min(d[1] - 1 , d[2] - 1)
        elif d[1] > 0 and d[2] == 0:
            ans += 1
        elif d[1] == 0 and d[2] > 0:
            ans += 1
    else:
        ans+=min(d[1] , d[2])

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
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
if countab==0:
  pass
else:
  ans+=countab-1
if counta==0 and countb==0:
  print(ans)
else:
  if countab==0:
    pass
  else:
    ans+=1
  ans+=min(counta,countb)
  print(ans)

Solution (Python):
from sys import stdin
N = int(stdin.readline().rstrip())
ans = 0
c1 = 0
c2 = 0
c3 = 0
for _ in range(N):
    s = (stdin.readline().rstrip())
    ans += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        c1 += 1
    elif s[0] == "B":
        c2 += 1
    elif s[-1] == "A":
        c3 += 1
if c1 == 0:
    ans += min(c2,c3)
else:
    if c2 + c3 == 0:
        ans += (c1-1)
    else:
        ans += (c1 + min(c2,c3))
print(ans)

Solution (Python):
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


if ab > 0:
    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)

else:
    mid += min(a, b)
print(mid)

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


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

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


if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
s=''
point=0
q1,q2,q3=0,0,0 
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==0 and q3==0:
    print(q1-1+point)
  elif q2==0 or q3==0:
    print(q1-1+1+point)
  else:
    print(q1-1+point+2+min(q2-1,q3-1))
else:
  print(min(q2,q3)+point)

Solution (Python):
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

if BA == 0:
    ans = cnt + min(A, B)
else:
    ans = cnt + BA - 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())
c, fb, la, both = 0, 0, 0, 0
for _ in range(n):
  s = input()
  c += s.count("AB")
  if s[0] == "B" and s[-1] == "A":
    both += 1
  else:
    if s[0] == "B":
      fb += 1
    if s[-1] == "A":
      la += 1
if both == 0:
  c += min(fb, la)
else:
  if fb+la > 0:
    c += both + min(fb, la)
  else:
    c += both-1
print(c)

Solution (Python):
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 == b == ba == 0:
    print(cnt)
elif a == b == 0:
    print(cnt + ba-1)
else:
    print(cnt + min(a,b) + ba)

Solution (Python):
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  (countA>0 or countB>0) and countBA>0:
    count+=countBA-1+min(countB+1,countA+1)

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

Solution (Python):

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

for i in range(N):

    s = input()
    fbf = False
    laf = False

    for j in range(len(s)):

        

        if j == 0 and s[j] == "B":
            fbf = True

        if j == len(s) - 1:
            if s[j] == "A":
                laf = True

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


    if fbf and laf:
        LAFB += 1
    elif laf:
        LA += 1
    elif fbf:
        FB += 1

if LAFB > 0:
    AB += LAFB - 1

    if LA > 0:
        LA -= 1
        AB += 1

    if FB > 0:
        FB -= 1
        AB += 1

print (AB + min(FB,LA))

Solution (Python):





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():
    
    
    
    
    
    
    
    
    
    
    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
    if ba==0:print(cnt+min(a,b))
    else:
        print(cnt+ (ba-1 if a+b==0 else ba+min(a,b)) )
main()

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

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

def main():
    N = int(input())
    s = [input().strip() for i in range(N)]
    
    ans = 0
    ba = 0
    b_ = 0
    _a = 0
    for c in s:
        ans += c.count("AB")
        if c[0] == "B":
            if c[-1] == "A":
                ba += 1
            else:
                b_ += 1
        elif c[-1] == "A":
            _a += 1
    if ba == 0:
        ans += min(b_, _a)
    elif b_ + _a > 0:
        ans += ba + min(b_, _a)
    elif b_ + _a == 0:
        ans += ba - 1
    print(ans)

if __name__ == "__main__":
    main()

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

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>0 else min(x,y))
print(ans+p)

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

b = 0
a = 0
ab = 0
ex = 0

for i in range(n) :
    tmp = input()
    ab += tmp.count('AB')
    if tmp[0] == 'B' :
        b += 1
    if tmp[-1] == 'A' :
        a += 1
    if tmp[0] == 'B' and tmp[-1] == 'A' :
        ex += 1

if max(a, b) > ex or ex == 0 :
    print(ab + min(a, b))
else :
    print(ab + min(a, b) - 1)

Solution (Python):
N = int(input())
a =0
b = 0
ans = 0
cnt = 0
for i in range(N):
  s = input()
  if s[0]=="B":
    b+=1
  if s[-1]=="A":
    a+=1
  if s[0]=="B" and s[-1]=="A":
    cnt+=1
  ans+=s.count("AB")
if max(a,b)>cnt or cnt==0:
	print(ans+min(a,b))
else:
    print(ans+min(a,b)-1)

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

B_ = 0
_A = 0
BA = 0
inner = 0

for i in range(N):
    s = list(input())
    for j in range(1,len(s)):
        if s[j-1] == "A" and s[j] == "B":
            inner += 1
    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:
    print(BA + 1 + min(B_-1,_A-1,B_+_A-2) + inner)
else:
    print(min(B_,_A)+inner)

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

na=0
a=0
b=0
ry=0
for s in S:
  na+=s.count("AB")
  if s[0]=="B" and s[-1]=="A":
    ry+=1
  elif s[0]=="B":
    b+=1
  elif s[-1]=="A":
    a+=1

if a==0 and b==0 and ry!=0:
  print(na+ry-1)
else:
  print(na+ry+1+min(a-1,b-1))

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

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N = int(input())
S = [input()[:-1] for _ in range(N)]
ans = 0

end_A = 0
start_B = 0
A_B = 0
for s in S:
    if s[0] == "B"and s[-1] == "A":
        A_B += 1
    elif s[0] == "B":
        start_B += 1
    elif s[-1] == "A":
        end_A += 1
    for a, b in zip(s, s[1:]):
        if a == "A" and b == "B":
            ans += 1

add = 0
if A_B == 0:
    add = min(end_A, start_B)
else:
    if max(end_A, start_B) > 0:
        add = A_B+min(end_A, start_B)
    else:
        add = A_B-1

print(ans+add)

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]
N=int(input())
a,A,B,C=0,0,0,0
for i in range(N):
    s=input()
    for j in range(len(s)-1):
        if s[j]=="A" and s[j+1]=="B":
            a+=1
    if s[0]=="B":
        A+=1
    if s[-1]=="A":
        B+=1
        if s[0]=="B":
            C+=1
if A==B==C:
    a+=max(0,C-1)
else:
    a+=min(A,B)
print(a)

Solution (Python):
n=int(input())
ba,a,b,ab=0,0,0,0
for i 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(len(s)-1):
        if s[i]=='A' and s[i+1]=='B':
            ab+=1
if a==b and a==0 and ba!=0:
	ba-=1
print(min(a,b)+ba+ab)

Solution (Python):


N=int(input())

l=[0]*3
ans=0
for i in range(N):
    S=(input())
    ans+=S.count("AB")
    if S[0]=="B" and S[-1]=="A":
        l[0]+=1
    elif S[0]=="B":
        l[1]+=1
    elif S[-1]=="A":
        l[2]+=1
if l[0]>0:
    ans+=(l[0]-1)
    if l[1]>0:
        l[1]-=1
        ans+=1
    if l[2]>0:
        l[2]-=1
        ans+=1
ans+=min(l[1],l[2])
    

print(ans)

Solution (Python):
def main():
    n = int(input())
    ls = [input() for _ in range(n)]
    acount = 0
    bcount = 0
    abcount = 0
    bacount = 0
    for s in ls:
        abcount += s.count("AB")
        if s[0] =="B" and s[-1] =="A":bacount += 1
        elif s[0] =="B":bcount += 1
        elif s[-1] =="A":acount += 1

    if acount == 0 and bcount == 0:
        if bacount > 0:
            bacount -= 1
        print(abcount+bacount)
    elif acount == 0 or bcount == 0:
        print(abcount+bacount)
    else:
        print(abcount+bacount+min(acount,bcount))

main()

Solution (Python):

import sys
input=sys.stdin.readline

n=int(input())
cnt=0
b_a=0
a=0
b=0
for i in range(n):
    s=input().rstrip('\n')
    cnt+=s.count('AB')
    is_b=s[0]=='B'
    is_a=s[-1]=='A'
    if is_b or is_a:
        if is_b and is_a:b_a+=1
        elif is_b:b+=1
        else:a+=1
    


if b_a:
    cnt+=b_a-1
    if a==b and a>0:
        a-=1;b-=1
        cnt+=2
    else:
        if abs(a-b)>0:
            cnt+=1
cnt+=min(a,b)

print(cnt)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  ans=0

  _ba=0
  _b=0
  _a=0
  for _ in range(n):
    s=S()
    ans+=s.count('AB')

    if s[0]=='B':
      if s[-1]=='A':
        _ba+=1
      else:
        _b+=1
    else:
      if s[-1]=='A':
        _a+=1

  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)

  return ans


print(main())

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  ans=0

  _ba=0
  _b=0
  _a=0
  for _ in range(n):
    s=S()
    ans+=s.count('AB')

    if s[0]=='B':
      if s[-1]=='A':
        _ba+=1
      else:
        _b+=1
    else:
      if s[-1]=='A':
        _a+=1

  ans+=max(0,_ba-1)

  if _ba>0:
    if _b>0:
      ans+=1
      _b-=1
    if _a>0:
      ans+=1
      _a-=1

  if _b>0 and _a>0:
    ans+=min(_b,_a)

  return ans


print(main())

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

if N==1:
    print(input().count("AB"))
    exit()

BA=0
B_=0
_A=0
cnt=0

for i in range(N):
    s=input()

    if s[0] == "B" and s[-1] == "A":
        BA += 1
    elif s[0] == "B" and s[-1] != "A":
        B_ += 1
    elif s[-1] != "B" and s[-1] == "A":
        _A += 1

    cnt += s.count("AB")


if _A==0 and B_==0:
    ans=max(0,BA-1)+cnt
elif (_A!=0 and B_==0) or (_A==0 and B_!=0):
    ans=BA+cnt

else:
    ans=BA+1+min(_A-1,B_-1)+cnt

print(ans)

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

tailA = 0
headB = 0
same = 0
inside = 0
for s in S:
    inside += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        same += 1
    else:
        if s[0] == 'B':
            headB += 1
        if s[-1] == 'A':
            tailA += 1

ans = 0

if same >= 1:
    if tailA >= 1:
        ans += same
        tailA -= 1
    else:
        ans += same - 1

    if headB >= 1:
        ans += 1
        headB -= 1

ans += max(min(tailA, headB), 0)
print(ans + inside)

Solution (Python):
n=int(input())
a=0
b=0
malti=0
origin=0
for i in range(n):
    s=input()
    if s[0]=="B" and s[-1]=="A":
        malti+=1
    elif s[0]=="B" and s[-1]!="A":
        b+=1
    elif s[0]!="B" and s[-1]=="A":
        a+=1
    for i in range(len(s)-1):
        if s[i]=="A" and s[i+1]=="B":
            origin+=1

if a>=1 and b>=1:
    print(origin+malti+1 +min(a-1,b-1))
elif a==0 and b>=1:
    print(origin+malti)
elif a>=1 and b==0:
    print(origin+malti)
elif a==0 and b==0:
    print(origin+max(malti-1,0))

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

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

x = max(0, BA - 1)
ans += x
BA -= x
x = min(BA, BX)
ans += x
BA -= x
x = min(BA, XA)
ans += x
BA -= x
ans += min(XA, BX)
print(ans)

Solution (Python):
n=int(input())
S=[input() for i in range(n)]
a,b,c=0,0,0
cnt=0
for i in S:
    if i[0]=="B" and i[-1]=="A":c +=1
    elif i[0]=="B":a +=1
    elif i[-1]=="A":b +=1
    for j in range(len(i)-1):
        if i[j]+i[j+1]=="AB":cnt +=1

if c==0:cnt +=min(a,b)
elif a+b==0:cnt +=c-1
elif c!=0 and a+b>0:cnt +=min(a,b)+c
print(cnt)

Solution (Python):
n = int(input())
AB = [ 0 for _ in range(4)]
for i in range(n):
    s = str(input())
    m = len(s)
    for j in range(m-1):
        if s[j]=="A" and s[j+1]=="B":
            AB[3] +=1
    if s[0]=="B" and s[-1]=="A":
        AB[0] +=1
    elif s[0]=="B" and s[-1]!= "A":
        AB[1] += 1
    elif s[0]!="B" and s[-1]=="A":
        AB[2] +=1
    else:
        continue
ans = 0
if AB[0]==0:
    ans = min(AB[1], AB[2]) + AB[-1]
elif  AB[1]+AB[2]>0:
    ans = AB[0] + min(AB[1], AB[2]) + AB[-1]
elif AB[1]==0 and AB[2]==0:
    ans = AB[0]-1 +AB[-1]
print(ans)

Solution (Python):
n=int(input())
ba,a,b,ab=0,0,0,0
for i 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(len(s)-1):
        if s[i]=='A' and s[i+1]=='B':
            ab+=1
if a==b and a==0 and ba!=0:
	ba-=1
print(min(a,b)+ba+ab)

Solution (Python):

n = int(input())
s = []
a = 0
b = 0
c = 0
for i in range(n):
    s.append(input())
    if s[i][0] == 'B' and s[i][-1] != 'A':
        b += 1
    if s[i][-1] == 'A' and s[i][0] != 'B':
        a += 1
    if s[i][0] == 'B' and s[i][-1] =='A':
        c += 1

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

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

Solution (Python):

def main():
    N, *S = open(0).read().split()
    ans, ab, start_b, end_a = 0, 0, 0, 0
    for s in S:
        if s[0] == "B" and s[-1] == "A":
            ab += 1
        elif s[0] == "B":
            start_b += 1
        elif s[-1] == "A":
            end_a += 1
        ans += s.count("AB")
    if start_b + end_a == 0:
        ans += max(0, ab - 1)
    else:
        ans += ab + min(start_b, end_a)
    print(ans)


if __name__ == "__main__":
    main()

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

ans = 0
ba = 0
xa = 0
bx = 0
for s in S:
  ans += s.count('AB')
  if s[0] == 'B' and s[-1] == 'A':
    ba += 1
  elif s[0] == 'B' and s[-1] != 'A':
    bx += 1
  elif s[0] != 'B' and s[-1] == 'A':
    xa += 1
if xa == 0 and bx == 0:
  ans += max(ba-1,0)
elif (xa == 0 and bx != 0) or (xa != 0 and bx == 0):
  ans += ba
else:
  ans += ba+1 + min(xa-1,bx-1)
print(ans)

Solution (Python):
N = int(input())
ans = 0
_A = 0
B_ = 0
B_A = 0

for _ in range(N):
    s = input()
    ans += s.count('AB')

    if s[-1] == 'A':
        _A += 1

    if s[0] == 'B':
        B_ += 1

    if s[0] == 'B' and s[-1] == 'A':
        B_A += 1

ans += min(_A, B_)
if _A == B_ == B_A != 0:
    ans -= 1
print(ans)

Solution (Python):
N = int(input())
r = 0
nBA = 0
nA = 0
nB = 0
for i in range(N):
  s = input()
  r += s.count("AB")
  if s[0] == "B" and s[-1] == "A":
    nBA += 1
  elif s[0] == "B":
    nB += 1
  elif s[-1] == "A":
    nA += 1
r += min(nA, nB)
if nA > 0 or nB > 0:
  r += nBA
else:
  r += max(0, nBA-1)
print(r)

Solution (Python):

n = int(input())
s = [input() for _ in range(n)]
ans = 0
xa = 0
bx = 0
bxa = 0
for c in s:
    ans += sum(c[i:i + 2] == 'AB' for i in range(len(c) - 1))
    if c[0] == 'B':
        bx += 1
    if c[-1] == 'A':
        xa += 1
    if c[0] == 'B' and c[-1] == 'A':
        bx -= 1
        xa -= 1
        bxa += 1
ans += max(0, bxa - 1)
bxa = 0 + (bxa > 0)
ans += min(xa, bxa)
ans += min(bxa, bx)
rxa = max(0, xa - bxa)
rbx = max(0, bx - bxa)
ans += min(rxa, rbx)
print(ans)

Solution (Python):
import sys
def main():
    def input():
        return sys.stdin.readline()[:-1]
    N = int(input())
    ans = 0
    a, b, c = 0, 0, 0
    for k in range(N):
        s = input()
        if s[0] == "B" and s[-1] == "A":
            c += 1
        if s[0] == "B":
            b += 1
        if s[-1] == "A":
            a += 1
        for k in range(len(s)-1):
            if s[k:k+2] == "AB":
                ans += 1
    if c > 0 and a == b == c:
        print(ans + c-1)
    else:
        print(ans + min(a,b))

if __name__ == '__main__':
    main()