qyliang/CodeNanoFix huggingface dataset:
Buggy code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()

n = ni()
_anum = 0
b_num = 0
banum = 0
ct = 0
for i in range(n):
    st = input()
    ct += st.count('AB')
    if st.startswith('B'):
        if st.endswith('A'):
            banum += 1
        else:
            b_num += 1
    else:
        if st.endswith("A"):
            _anum += 1
if banum > 1:
    print(ct + min(_anum, b_num) + (banum - 1) + abs(_anum - b_num))
else:
    print(ct + min(_anum, b_num))

Fixed code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()

n = ni()
_anum = 0
b_num = 0
banum = 0
ct = 0
for i in range(n):
    st = input()
    ct += st.count('AB')
    if st.startswith('B'):
        if st.endswith('A'):
            banum += 1
        else:
            b_num += 1
    else:
        if st.endswith("A"):
            _anum += 1
if banum > 0:
    if _anum > 0 and b_num > 0:
        print(ct + min(_anum - 1, b_num - 1) + (banum + 1))
    elif _anum == 0 and b_num == 0:
        print(ct + banum - 1)
    else:
        print(ct + banum)
else:
    print(ct + min(_anum, b_num))
Buggy code:
N = int(input())
s = []
temp_sum = 0
A_or_B_count = [0,0]
AB_count = 0
for i in range(N):
	aaa = input()
	temp_sum += aaa.count("AB")
	if aaa[0] == "B" and aaa[-1] == "A":
		AB_count += 1
	elif aaa[-1] == "A":
		A_or_B_count[0] += 1
	elif aaa[0] == "B":
		A_or_B_count[1] += 1
A_or_B_count.sort()
print(temp_sum + min(N - 1,A_or_B_count[0] + AB_count,A_or_B_count[1]))

	

	

Fixed code:
N = int(input())
s = []
temp_sum = 0
A_or_B_count = [0,0]
AB_count = 0
for i in range(N):
	aaa = input()
	temp_sum += aaa.count("AB")
	if aaa[0] == "B" and aaa[-1] == "A":
		AB_count += 1
	elif aaa[-1] == "A":
		A_or_B_count[0] += 1
	elif aaa[0] == "B":
		A_or_B_count[1] += 1
A_or_B_count.sort()
if A_or_B_count[0] + AB_count > A_or_B_count[1]:
	if A_or_B_count[1] == 0:
		print(temp_sum + min(N-1,AB_count - 1))
	else:
		AB_count -= (A_or_B_count[1] - A_or_B_count[0])
		print(temp_sum + min(N-1,AB_count + A_or_B_count[1]))
else:
	print(temp_sum + min(N - 1,A_or_B_count[0] + AB_count))
Buggy code:
n = int(input())
s = list(input() for _ in range(n))
cou = 0
for i in s:
    cou += i.count('AB')
a = 0
b = 0
both = 0
for i in s:
    if i[0]=="B":
        b += 1
    if i[-1]=="A":
        a += 1
    if i[0]=="B" and i[-1]=="A":
        both += 1
if max(a,b)-min(a,b)>=min(a,b)-both:
    print(cou+min(a,b))
else:
    print(cou+min(a,b)-1)

Fixed code:
n = int(input())
s = list(input() for _ in range(n))
cou = 0
for i in s:
    cou += i.count('AB')
a = 0
b = 0
both = 0
for i in s:
    if i[0]=="B":
        b += 1
    if i[-1]=="A":
        a += 1
    if i[0]=="B" and i[-1]=="A":
        both += 1
if min(a,b)==both and a==b:
    print(cou+max(min(a,b)-1,0))
else:
    print(cou+min(a,b))
Buggy code:
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)

Fixed code:
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)
Buggy code:
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

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



Fixed code:
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

#print(S)
#print("Acount  = " + str(Acount))
#print("Bcount  = " + str(Bcount))
#print("ABcount = " + str(ABcount))
if BXAcount >= 1 and Acount == BXAcount and Bcount == BXAcount :
		print(min(Acount, Bcount) + ABcount -1)
else:
	print(min(Acount, Bcount) + ABcount)
Buggy code:
# -*- coding: utf-8 -*-

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

ans = sum([s.count('AB') for s in S])
count_A = 0
count_B = 0
count_AB = 0
for s in S:
    if s[0] == 'B':
        count_A += 1
        if s[-1] == 'A':
            count_B += 1
    if s[-1] == 'A':
        count_B += 1

if max(count_A, count_B) == count_AB:
    ans += count_AB-1
else:
    ans += min(count_A, count_B)

print(ans)



Fixed code:
# -*- coding: utf-8 -*-

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

ans = sum([s.count('AB') for s in S])
count_A = 0
count_B = 0
count_AB = 0
for s in S:
    if s[0] == 'B':
        count_B += 1
        if s[-1] == 'A':
            count_AB += 1
    if s[-1] == 'A':
        count_A += 1

if max(count_A, count_B) != 0 and max(count_A, count_B) == count_AB:
    ans += count_AB-1
else:
    ans += min(count_A, count_B)

print(ans)
Buggy code:
N = int(input())
ans = 0
c1 = 0
c2 = 0
c3 = 0
ans = 0
for _ in range(N):
    s = input()
    ans += s.count("AB")
    if (s[0]=="B") & (s[1]=="A"):
        c1 += 1
    elif s[0] == "B":
        c2 += 1
    elif s[-1]=="A":
        c3 += 1

if c1== 0:
    print(ans + min(c2, c3))
elif (c1 > 0) & (max(c2, c3) == 0):
    print(ans + c1 - 1)
elif (c1 > 0) & (max(c2, c3) > 0):
    print(ans + c1 + min(c2, c3))

Fixed code:
N = int(input())
ans = 0
c1 = 0
c2 = 0
c3 = 0
ans = 0
for _ in range(N):
    s = input()
    ans += s.count("AB")
    if (s[0]=="B") & (s[-1]=="A"):
        c1 += 1
    elif s[0] == "B":
        c2 += 1
    elif s[-1]=="A":
        c3 += 1

if c1== 0:
    print(ans + min(c2, c3))
elif (c1 > 0) & (max(c2, c3) == 0):
    print(ans + c1 - 1)
elif (c1 > 0) & (max(c2, c3) > 0):
    print(ans + c1 + min(c2, c3))
Buggy code:
n = int(input())
S = []
result = 0
A_count = 0
B_count = 0
BA_count = 0

for i in range(n):
    Si = str(input())
    S.append(Si)

for i in range(n):
    Si = S[i]
    result += Si.count("AB")
    if Si[0] == "B":
        B_count += 1
    if Si[-1] == "A":
        A_count += 1
    if Si[0] == "B" and Si[-1] == "A"
        BA_count += 1 

if BA_count >= 2:
    result += (BA_count - 1)

if BA_count >= 1
    if B_count >= 1:
        result += 1
        B_count -= 1
    if A_count >= 1:
        result += 1
        A_count -= 1

if B_count >= 1 and A_count >= 1:
    result += min(B_count, A_count)

print(result)  

Fixed code:
n = int(input())
S = []
result = 0
A_count = 0
B_count = 0
BA_count = 0

for i in range(n):
    Si = str(input())
    S.append(Si)

for i in range(n):
    Si = S[i]
    result += Si.count("AB")
    if Si[0] == "B" and Si[-1] != "A":
        B_count += 1
    if Si[0] != "B" and Si[-1] == "A":
        A_count += 1
    if Si[0] == "B" and Si[-1] == "A":
        BA_count += 1 

if BA_count >= 2:
    result += (BA_count - 1)

if BA_count >= 1:
    if B_count >= 1:
        result += 1
        B_count -= 1
    if A_count >= 1:
        result += 1
        A_count -= 1

if B_count >= 1 and A_count >= 1:
    result += min(B_count, A_count)

print(result)
Buggy code:
import re
n = int(input())
s = []
compiled = re.compile(r"AB")

pre = 0
xa=0
bx=0
ba=0
for i in range(n):
    one = input()
    pre+=len(compiled.findall(one))
    f=one[0]
    l=one[-1]


    if f=="B" and l=="A":
        ba+=1
    elif f=="B" and l!="A":
        bx+=1
    elif f!="B" and l=="A":
        xa+=1
    
            

print(xa,bx,ba,pre)
cnt=ba-1
if xa>=1:
    xa-=1
    cnt+1
    
if bx>=1:
    bx-=1
    cnt+1

cnt += xa if xa>bx else bx
print(cnt+pre)

Fixed code:
import re
n = int(input())
s = []
compiled = re.compile(r"AB")

pre = 0
xa=0
bx=0
ba=0
for i in range(n):
    one = input()
    pre+=len(compiled.findall(one))
    f=one[0]
    l=one[-1]


    if f=="B" and l=="A":
        ba+=1
    elif f=="B" and l!="A":
        bx+=1
    elif f!="B" and l=="A":
        xa+=1

#print(ba,bx,xa,pre)
cnt=0
if ba>=1:
    cnt+=ba-1
    if xa>=1:
        xa-=1
        cnt+=1
    
    if bx>=1:
        bx-=1
        cnt+=1

cnt += xa if xa<bx else bx
print(cnt+pre)
Buggy code:
n = int(input())
s = [input() for _ in range(n)]

base = sum(x.count('AB') for x in s)

same_pattern = a_n = b_n = 0
for x in s:
    b = x[0]
    a = x[len(x)-1]
    if b == 'B':
        b_n += 1
    if a == 'A':
        a_n += 1
    if a == 'A' and b == 'B':
        same_pattern += 1

if same_pattern > 0 and same_pattern == min(a_n, b_n):
    print(base + min(a_n, b_n) - 1)
else:
    print(base + min(a_n, b_n))

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

base = sum(x.count('AB') for x in s)

same_pattern = a_n = b_n = 0
for x in s:
    b = x[0]
    a = x[len(x)-1]
    if b == 'B':
        b_n += 1
    if a == 'A':
        a_n += 1
    if a == 'A' and b == 'B':
        same_pattern += 1

if same_pattern > 0 and same_pattern == max(a_n, b_n):
    print(base + max(a_n, b_n) - 1)
else:
    print(base + min(a_n, b_n))
Buggy code:

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

finA = 0
topB = 0
B2A = 0
cnt = 0

for l in lst:
    cnt += l.count('AB')
    if l[0]=='B' and l[-1]=='A':
        B2A += 1
    else:
        if l[0]=='B':
            topB += 1
        elif l[-1]=='A':
            finA += 1

if BtoA==0:
    cnt += min(finA, topB)
else:
    if finA==0 and topB==0:
        cnt += BtoA - 1
    else:
        cnt += BtoA + min(finA, topB)
print(cnt)

Fixed code:
n = int(input())
lst = [input() for _ in range(n)]

finA = 0
topB = 0
BtoA = 0
cnt = 0
for l in lst:
    cnt += l.count('AB')
    if l[0]=='B' and l[-1]=='A':
        BtoA += 1
    else:
        if l[0]=='B':
            topB += 1
        elif l[-1]=='A':
            finA += 1

if BtoA==0:
    cnt += min(finA, topB)
else:
    if finA==0 and topB==0:
        cnt += BtoA - 1
    else:
        cnt += BtoA + min(finA, topB)
print(cnt)
Buggy code:
n = input()
N = int(n)

count = 0
Acount = 0
AScount = 0
Bcount = 0
BScount = 0
BAcount = 0

for i in range(N):
    s = input()
    if 1 < len(s):
        if 'AB' in s:
            count += 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
    else:
        if s == 'B':
            BScount += 1
        if s == 'A':
            AScount += 1

BAcount += min(BScount, AScount)

if BScount <= AScount:
    Acount += AScount - BScount
else:
    Bcount += BScount - AScount

if 0 < BAcount:
    count += BAcount - 1
    BAcount = 1

if 0 < Acount:
    count += 1
    Acount -= 1
if 0 < Bcount:
    count += 1
    Bcount -= 1

count += min(Bcount, Acount)
print(count)


Fixed code:
n = input()
N = int(n)

count = 0
Acount = 0
AScount = 0
Bcount = 0
BScount = 0
BAcount = 0

for i in range(N):
    s = input()
    if 1 < len(s):
        if 'AB' in s:
            count += 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
    else:
        if s == 'B':
            BScount += 1
        if s == 'A':
            AScount += 1

BAcount += min(BScount, AScount)

if BScount <= AScount:
    Acount += AScount - BScount
else:
    Bcount += BScount - AScount

if 0 < BAcount:
    count += BAcount - 1
    BAcount = 1

if 0 < Acount and 0 < BAcount:
    count += 1
    Acount -= 1

if 0 < Bcount and 0 < BAcount:
    count += 1
    Bcount -= 1

count += min(Bcount, Acount)
print(count)
Buggy code:
N = int(input())

Bxxx = 0
xxxA = 0
BxxA = 0
xABx = 0
for i in range(N):
    s = input()
    xABx += s.count("AB")
    if (s[0] == "B") and (s[-1] == "A"):
        BxxA += 1
    else:
        if s[0] == "B":
            Bxxx += 1
        if s[-1] == "A":
            xxxA += 1
#print("Bxxx={},xxxA={},BxxA={},xABx={}".format(Bxxx,xxxA,BxxA,xABx))

min_Bx_xA = min(Bxxx, xxxA)
if BxxA == 0:
    print(xABx + min_Bx_xA)
elif min_Bx_xA == 0:
    print(xABx + BxxA - 1)
else:
    set3 = min(BxxA, Bxxx, xxxA)
    #print(xABx + set3 * 2 + BxxA - set3 + min(Bxxx, xxxA) - set3)
    print(xABx + BxxA + min(Bxxx, xxxA))


Fixed code:
N = int(input())

Bxxx = 0
xxxA = 0
BxxA = 0
xABx = 0
for i in range(N):
    s = input()
    xABx += s.count("AB")
    if (s[0] == "B") and (s[-1] == "A"):
        BxxA += 1
    else:
        if s[0] == "B":
            Bxxx += 1
        if s[-1] == "A":
            xxxA += 1
#print("Bxxx={},xxxA={},BxxA={},xABx={}".format(Bxxx,xxxA,BxxA,xABx))

if BxxA == 0:
    print(xABx + min(Bxxx, xxxA))
elif Bxxx == xxxA == 0:
    print(xABx + BxxA - 1)
else:
    #set3 = min(BxxA, Bxxx, xxxA)
    #print(xABx + set3 * 2 + BxxA - set3 + min(Bxxx, xxxA) - set3)
    print(xABx + BxxA + min(Bxxx, xxxA))
Buggy code:
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:
    print(spec-1+cnt)
else:
    print(spec-abs(headb-lasta)+max(headb,lasta)+cnt)

Fixed code:
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)
Buggy code:
a = int(input())
s = 0
A=B=AB=0

d = {"A":0,"B":0}
for i in range(a):
    t = input()
    s+=t.count("AB")
    if t[0]=="B" and t[-1]=="A":AB+=1
    elif t[0]=="B":B+=1
    elif t[-1]=="A":A+=1
res = 0
res += min(A,B)
if AB>0 and abs(A-B):
    res+=AB
else:
    res+=AB-1
print(res+s)

Fixed code:
a = int(input())
s = 0
A=B=AB=0

d = {"A":0,"B":0}
for i in range(a):
    t = input()
    s+=t.count("AB")
    if t[0]=="B" and t[-1]=="A":AB+=1
    elif t[0]=="B":B+=1
    elif t[-1]=="A":A+=1
res = 0
if A==0 and B == 0 and AB >0:
    res = AB-1
elif A==0 and B == 0:
    res = 0 
elif A==1 and B == 0:
    res = AB
elif A==0 and B ==1:
    res = AB
else:
    res = AB+1 + min(A-1,B-1)
print(res+s)
Buggy code:
n = int(input())
ans = 0
a =[0 for i in range(n)]
b = [0 for i in range(n)]
c = [0 for i in range(n)]
for i in range(n):
    s = input()
    q = len(s)
    for j in range(q):
        if j == 0 and s[j] == 'B':
            b[i] = 1
        elif j == q-1 and s[j] == 'A':
            a[i] = 1
        if j > 0:
            if s[j] == 'B' and s[j-1] == 'A':
                ans += 1
    if b[i] == 1 and a[i] == 1:
        c[i] = 1
        a[i] = 0
        b[i] = 0



A = sum(a)
B = sum(b)
C = sum(c)
if C >= 1:
    ans += C-1
    if A >= 1 and B >= 1:
        ans += 2 + min(A-1,B-1)
    elif A == 0 and B == 0:
        pass
    else:
        ans += 1
else:
    ans += min(A,B)

print(ans)
print(A,B,C)

Fixed code:
n = int(input())
ans = 0
a =[0 for i in range(n)]
b = [0 for i in range(n)]
c = [0 for i in range(n)]
for i in range(n):
    s = input()
    q = len(s)
    for j in range(q):
        if j == 0 and s[j] == 'B':
            b[i] = 1
        elif j == q-1 and s[j] == 'A':
            a[i] = 1
        if j > 0:
            if s[j] == 'B' and s[j-1] == 'A':
                ans += 1
    if b[i] == 1 and a[i] == 1:
        c[i] = 1
        a[i] = 0
        b[i] = 0



A = sum(a)
B = sum(b)
C = sum(c)
if C >= 1:
    ans += C-1
    if A >= 1 and B >= 1:
        ans += 2 + min(A-1,B-1)
    elif A == 0 and B == 0:
        pass
    else:
        ans += 1
else:
    ans += min(A,B)

print(ans)
Buggy code:
N = int(input())
S =[input() for i in range(N)]
#print(S)

cnt = 0
BA = 0 # 'B~A'
lA = 0 # last 'A'
fB = 0 # first 'B'

for i in S:
  c = i.count('AB')
  cnt += c

  # count 'B~*', 'B~A' '*~A'
  if i.find('B') == 0 and i.rfind('A') == len(i)-1:
    BA += 1
  elif i.find('B') == 0:
    fB += 1
  elif i.rfind('A') == len(i)-1:
    lA += 1

lA_fB = min(lA, fB)  
r = min(abs(lA - fB), BA)
n = max(0, (BA -r) // 2)
#print(cnt)

cnt += lA_fB + r + n

#print("fB, lA, BA", fB, lA, BA)
#print("lA_fB , r , n",lA_fB , r , n)
print(cnt)

Fixed code:
N = int(input())
S =[input() for i in range(N)]
#print(S)

cnt = 0
BA = 0 # 'B~A'
lA = 0 # last 'A'
fB = 0 # first 'B'

for i in S:
  c = i.count('AB')
  # count 'B~*', 'B~A' '*~A'
  if i.find('B') == 0 and i.rfind('A') == len(i)-1:
    BA += 1
  elif i.find('B') == 0:
    fB += 1
  elif i.rfind('A') == len(i)-1:
    lA += 1
  cnt += c

# lA_fB = min(lA, fB)  
# r = min(abs(lA - fB), BA)
# n = max(0, (BA -r) // 2)

if BA > 0:
  cnt += BA -1
  if lA > 0:
    cnt += 1
    lA -= 1
  if fB > 0:
    cnt += 1
    fB -= 1

cnt += min(fB, lA)
print(cnt)
Buggy code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

N = int(input())
ans = 0

end_a = 0
start_b = 0
both = 0
for i in range(N):
    S = input()
    ans += S.count("AB")
    if S[-1] == 'A' and S[0] == 'B':
        both += 1
    elif S[-1] == 'A':
        end_a += 1
    elif S[0] == 'B':
        start_b += 1

if both == 0:
    ans = min(start_b, end_a)
else:
    if start_b + end_a > 0:
        ans = both + min(start_b, end_a) + (1 if abs(start_b - end_a) > 0 else 0)
    else:
        ans = both - 1
# if end_a > start_b:
#     ans += start_b
#     end_a -= start_b
#     start_b = 0
# else:
#     ans += end_a
#     start_b -= end_a
#     end_a = 0

# if both >= 1:
#     ans += both - 1
#     if start_b > 0:
#         ans += 1
#     if end_a > 0:
#         ans += 1
print(ans)



Fixed code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

N = int(input())
ans = 0

end_a = 0
start_b = 0
both = 0
for i in range(N):
    S = input()
    ans += S.count("AB")
    if S[-1] == 'A' and S[0] == 'B':
        both += 1
    elif S[-1] == 'A':
        end_a += 1
    elif S[0] == 'B':
        start_b += 1

if both == 0:
    ans += min(start_b, end_a)
else:
    if start_b + end_a > 0:
        ans += both + min(start_b, end_a)
    else:
        ans += both - 1
# if end_a > start_b:
#     ans += start_b
#     end_a -= start_b
#     start_b = 0
# else:
#     ans += end_a
#     start_b -= end_a
#     end_a = 0

# if both >= 1:
#     ans += both - 1
#     if start_b > 0:
#         ans += 1
#     if end_a > 0:
#         ans += 1
print(ans)
Buggy code:
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'):
    tail_a = tail_a + 1
  if (str[0] == 'B'):
    head_b = head_b + 1
  if(str[-1] == 'A' and str[0] == 'B'):
    doubleAB = doubleAB + 1

total_cand = inside_str + min(tail_a, head_b)

if(tail_a > head_b):
  total_cand = total_cand + min(doubleAB, tail_a - head_b)
else:
  total_cand = total_cand + min(doubleAB, head_b - tail_a)

print(total_cand)

Fixed code:
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)
Buggy code:
N = int(input())
eni_B = 0
end_A = 0
both = 0
count = 0

for i in range(N):
    s = list(input())
    if s[0] == 'B':
        eni_B += 1
    if s[-1] == 'A':
        end_A += 1
    if s[0] == 'B' and s[-1] == 'A':
        eni_B -= 1
        end_A -= 1
        both += 1
    for j in range(len(s)-1):
        if s[j] == 'A':
            if s[j+1] == 'B':
                count += 1


m = min(end_A,eni_B)
M = max(end_A,eni_B)

count = count + m

if M - m < both:
    count = count - m + M
    if both + m - M > 1:
        count =  count + both - 1
else:
    count = count + both

print(count) 

Fixed code:
N = int(input())
eni_B = 0
end_A = 0
both = 0
count = 0

for i in range(N):
    s = list(input())
    if s[0] == 'B':
        eni_B += 1
    if s[-1] == 'A':
        end_A += 1
    if s[0] == 'B' and s[-1] == 'A':
        eni_B -= 1
        end_A -= 1
        both += 1
    for j in range(len(s)-1):
        if s[j] == 'A':
            if s[j+1] == 'B':
                count += 1

m = min(end_A,eni_B)
M = max(end_A,eni_B)

if both == 0 :
    count = count + m
else:
    if m == 0 and M == 0 :
        count = count + both - 1
    else:
        count = count + both + m 

print(count)
Buggy code:
n = int(input())
ans = 0
a = 0
b = 0
ba = 0
for i in range(n):
    aaa = input()
    ans += aaa.count('AB')
    if aaa[0]=='B' and aaa[-1]=='A':
        ba += 1
    elif aaa[0]=='B':
        b += 1
    elif aaa[-1]=='A':
        a += 1
if ba == 0:
    ans += min(a,b)
else:
    if a>=1 and b>=1:
        ans += (ba+1+min(a-1,b-1))
    else:
        if ba == 1:
            ans += (min(a,b))
print(ans)        

Fixed code:
n = int(input())
ans = 0
a = 0
b = 0
ba = 0
for i in range(n):
    aaa = input()
    ans += aaa.count('AB')
    if aaa[0]=='B' and aaa[-1]=='A':
        ba += 1
    elif aaa[0]=='B':
        b += 1
    elif aaa[-1]=='A':
        a += 1
if ba != 0:
    if a>=1 and b>=1:
        ans += (ba+1+min(a-1,b-1))
    elif a==0 and b==0:
        ans += (ba-1)
    else:
        ans += ba
if ba == 0:
    ans += min(a,b)
    
print(ans)
Buggy code:
N = int(input())
cnt = 0
st_cnt = 0
end_cnt =0
same_cnt =0

for k in range(N):
    s = input()
    i = len(s)-1 
    cnt +=s.count("AB")

    if (s[0] =='B') & (s[i]=='A'):
        same_cnt += 1
    elif (s[0] =='B'):
        st_cnt +=1
    elif (s[i] == 'A'):
        end_cnt += 1

if (st_cnt==0) and (end_cnt==0):
    print(cnt+same_cnt-1)
else:print(cnt+min(st_cnt,end_cnt))

Fixed code:
N = int(input())
cnt = 0
st_cnt = 0
end_cnt =0
same_cnt =0

for k in range(N):
    s = input()
    cnt +=s.count("AB")

    if (s[0] =='B') & (s[-1]=='A'):
        same_cnt += 1
    elif (s[0] =='B'):
        st_cnt +=1
    elif (s[-1] == 'A'):
        end_cnt += 1
if same_cnt==0:
    print(cnt + same_cnt + min(st_cnt, end_cnt))
elif (st_cnt==0) and (end_cnt==0):
    print(cnt+same_cnt-1)
else:print(cnt+same_cnt+min(st_cnt,end_cnt))