qyliang/CodeNanoFix huggingface dataset:
Buggy code:
# -*- coding: utf-8 -*-

def solve():
    NAB, NBA, NB_, N_A = 0, 0, 0, 0
    for _ in range(int(input())):
        S = input()
        NAB += S.replace('AB', '-').count('-')
        b, a = S.startswith('B'), S.endswith('A')
        NBA += b&a
        NB_ += b&(~a)
        N_A += (~b)&a
    res = NAB + max(NBA-1, 0) + (NB_>=1) + (N_A>=1) + max(min(NB_, N_A)-1, 0)

    return str(res)

if __name__ == '__main__':
    print(solve())




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

def solve():
    NAB, NBA, NB_, N_A = 0, 0, 0, 0
    for _ in range(int(input())):
        S = input()
        NAB += S.replace('AB', '-').count('-')
        b, a = S.startswith('B'), S.endswith('A')
        NBA += (b and a)
        NB_ += (b and (not a))
        N_A += ((not b) and a)
    res = NAB + max(NBA-1, 0) + (NB_>=1 and NBA>=1) + (N_A>=1 and NBA>=1) + max(min(NB_, N_A)-(NBA>=1), 0)

    return str(res)

if __name__ == '__main__':
    print(solve())
Buggy code:
N =int(input())
co = {0:0,1:0,2:0,3:0}
ans = 0
foo =0
for _ in range(N):
    i = input()
    foo = 0
    if i[0] =="B":
        foo += 1
    if i[-1] =="A":
        foo += 2
    co[foo] += 1
    ans += i.count("AB")
ans +=  min(co[1],co[2])
if min(co[1],co[2])==0:
    ans += co[3]
    if co[1]==0 and co[2] == 0:
        ans -= 1 
print(ans)

Fixed code:
N =int(input())
co = {0:0,1:0,2:0,3:0}
ans = 0
foo =0
for _ in range(N):
    i = input()
    foo = 0
    if i[0] =="B":
        foo += 1
    if i[-1] =="A":
        foo += 2
    co[foo] += 1
    ans += i.count("AB")
ans +=  min(co[1],co[2])
ans += co[3]
if co[1]==0 and co[2] == 0 and co[3]!= 0:
    ans -= 1
print(ans)
Buggy code:
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 abs(a-b)>=cnt:
	print(ans+min(a,b))
else:
    print(ans+min(a,b)-cnt)

Fixed code:
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)
Buggy code:
n = int(input())
s = []
for i in range(n):
  s.append(input())

ab = 0
ab_ = 0
a = 0
b = 0
for i in s:
  ab += i.count("AB")
  if i[0] == "B" and i[1] == "A":
    ab_ += 1
  if i[0] == "B":
    b += 1
  if i[-1] == "A":
    a += 1
if ab_ == a and ab_ == b:
  print(ab+a-1)
else:
  print(ab+min([a,b]))

Fixed code:
n = int(input())
s = []
for i in range(n):
  s.append(input())
     
ab = 0
ab_ = 0
a = 0
b = 0
for i in s:
  ab += i.count("AB")
  if i[0] == "B" and i[-1] == "A":
    ab_ += 1
  if i[0] == "B":
    b += 1
  if i[-1] == "A":
    a += 1
if ab_ == a and ab_ == b and not ab_ == 0:
  print(ab+ab_-1)
else:
  print(ab+min([a,b]))
Buggy code:
n = int(input())
s_list = []
for _ in range(n):
    s_list.append(input())

ans = 0
head_b = 0
tail_a = 0
headAndTail = 0

for s in s_list:
    if 'AB' in s:
        ans += 1
    if s.startswith('B') and s.endswith('A')::
       headAndTail += 1
    elif s.startswith('B'):
        head_b += 1
    elif s.endswith('A'):
        tail_a += 1

ans += min([head_b, tail_a])

if head_b == 0 and tail_a == 0:
    ans += max([0,headAndTail-1])
else:
    ans += headAndTail

print(ans)

Fixed code:
n = int(input())
s_list = []
for _ in range(n):
    s_list.append(input())

ans = 0
head_b = 0
tail_a = 0
headAndTail = 0

for s in s_list:
    ans += s.count('AB')

    if s.startswith('B') and s.endswith('A'):
       headAndTail += 1
    elif s.startswith('B'):
        head_b += 1
    elif s.endswith('A'):
        tail_a += 1

ans += min([head_b, tail_a])

if head_b == 0 and tail_a == 0:
    ans += max([0,headAndTail-1])
else:
    ans += headAndTail

print(ans)
Buggy code:
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()

    n = int(input())
    s_table = [0]*3
    ans = 0
    for i in range(n):
        s = input()
        ans += s.count('AB')
        if s[0]=='B' and s[-1] == 'A':
            s_table[0] += 1
        elif s[0] == 'B' and s[-1] != 'A':
            s_table[1] += 1
        elif s[0] != 'B' and s[-1] == 'A':
            s_table[2] += 1
    
    m = min(s_table[1:])
    s_table[0] += m
    s_table[1] -= m
    s_table[2] -= m
    ans += s_table[0]-1 + min(1, sum(s_table[1:])) 
    print(ans)
        
    
if __name__ == '__main__':
    main()

Fixed code:
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()

    n = int(input())
    s_table = [0]*3
    ans = 0
    for i in range(n):
        s = input()
        ans += s.count('AB')
        if s[0] == 'B' and s[-1] == 'A':
            s_table[0] += 1
        elif s[0] == 'B' and s[-1] != 'A':
            s_table[1] += 1
        elif s[0] != 'B' and s[-1] == 'A':
            s_table[2] += 1
    
    if s_table[0] == 0:
        ans += min(s_table[1:])
    elif sum(s_table[1:]) > 0:
        ans += s_table[0]+min(s_table[1:])
    else:
        ans += s_table[0]-1
    print(ans)
        
    
if __name__ == '__main__':
    main()
Buggy code:
n = int(input())
res = 0
numa = numb = numboth = 0
for _ in range(n):
    s = input()
    res += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        numboth += 1
    elif s[0] == "B":
        numb += 1
    elif s[-1] == "A":
        numa += 1

#print(numboth, numa, numb)
#print(res)

if numa != 0 or numb != 0:
    res += numboth
else:
    if numboth != 0:
        res += numboth - 1

res += min(numa, numb)


Fixed code:
n = int(input())
res = 0
numa = numb = numboth = 0
for _ in range(n):
    s = input()
    res += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        numboth += 1
    elif s[0] == "B":
        numb += 1
    elif s[-1] == "A":
        numa += 1

#print(numboth, numa, numb)
#print(res)

if numboth == 0:
    res += min(numa, numb)
else:
    if numa == 0 and numb == 0:
        res += numboth - 1
    else:
        res += numboth
        res += min(numa, numb)
print(res)
Buggy code:
n = int(input())
ba, xa, bx = 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)

Fixed code:
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)
Buggy code:
N = int(input())
both = 0 
cnt = 0
gA = 0
sB = 0
for _ in range(N):
    s = input()
    cnt += s.count('AB')
    if s[-1] == 'A': gA += 1
    if s[0] == 'B': sB += 1
    if s[0] == 'B' and s[-1] == 'A': both += 1
ans = min(gA, sB) + cnt
if both == min(gA, sB):
    ans -= 1
print(ans)
# print(cnt, gA, sB)


Fixed code:
N = int(input())
both = 0 
cnt = 0
gA = 0
sB = 0
for _ in range(N):
    s = input()
    cnt += s.count('AB')
    if s[-1] == 'A': gA += 1
    if s[0] == 'B': sB += 1
    if s[0] == 'B' and s[-1] == 'A': both += 1
ans = min(gA, sB) + cnt
if both == gA and gA == sB and both != 0:
    ans -= 1
print(ans)
# print(cnt, gA, sB, both)
Buggy code:
n = int(input())
slist = []
ans = 0
startB, endA = 0, 0
for _ in range(n):
    s = list(input())
    for j in range(len(s) - 1):
        if ("".join(s[j:j + 2]) == "AB"):
            ans += 1
    startB += 1 if s[0] == "B" else 0
    endA += 1 if s[len(s) - 1] == "A" else 0

if startB == endA:
    startB -= 1

print(ans+min(startB, endA))


Fixed code:
n = int(input())
slist = []
ans = 0
startB, endA = 0, 0
b_a = 0
for _ in range(n):
    s = list(input())
    for j in range(len(s) - 1):
        if ("".join(s[j:j + 2]) == "AB"):
            ans += 1
    startB += 1 if s[0] == "B" else 0
    endA += 1 if s[len(s) - 1] == "A" else 0
    b_a += 1 if s[0] == "B" and s[len(s) - 1] == "A" else 0

if startB == b_a and endA == b_a:
    startB -= 1

print(ans+min(max(0, startB), max(0, endA)))
Buggy code:
N=int(input())
S=[input() for i in range(N)]
BA=0
B=0
A=0
cnt=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
ans=max(BA-1,0)
if B>0:
  ans+=1
  B-=1
if A>0:
  ans+=1
  A-=1
ans+=min(A,B)
print(ans+cnt)

Fixed code:
N=int(input())
S=[input() for i in range(N)]
BA=0
B=0
A=0
cnt=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 BA==0:
  print(cnt+min(A,B))
else:
  ans=BA-1
  if B>0:
    ans+=1
    B-=1
  if A>0:
    ans+=1
    A-=1
  print(cnt+min(A,B)+ans)
Buggy code:
N = int(input())

innerAB_cnt = 0
endA_cnt = 0
startB_cnt = 0
B_A_cnt = 0
for i in range(N):
    S = input()
    innerAB_cnt += S.count('AB')
    is_end_A = S[-1] == 'A'
    is_start_B = S[0] == 'B'
    if is_end_A and is_start_B:
        B_A_cnt += 1
    elif is_end_A:
        endA_cnt += 1
    elif is_start_B:
        startB_cnt += 1
remainder = abs(startB_cnt - endA_cnt)
if B_A_cnt == 0:
    print(min(endA_cnt, startB_cnt) + innerAB_cnt)
elif endA_cnt > 0 and startB_cnt > 0:
    print(min(endA_cnt, startB_cnt) + B_A_cnt + innerAB_cnt)
else:
    print(B_A_cnt-1 + innerAB_cnt)




Fixed code:
N = int(input())

innerAB_cnt = 0
endA_cnt = 0
startB_cnt = 0
B_A_cnt = 0
for i in range(N):
    S = input()
    innerAB_cnt += S.count('AB')
    is_end_A = S[-1] == 'A'
    is_start_B = S[0] == 'B'
    if is_end_A and is_start_B:
        B_A_cnt += 1
    elif is_end_A:
        endA_cnt += 1
    elif is_start_B:
        startB_cnt += 1
remainder = abs(startB_cnt - endA_cnt)
if B_A_cnt == 0:
    print(min(endA_cnt, startB_cnt) + innerAB_cnt)
elif endA_cnt + startB_cnt > 0:
    print(min(endA_cnt, startB_cnt) + B_A_cnt + innerAB_cnt)
else:
    print(B_A_cnt-1 + innerAB_cnt)
Buggy code:
n = int(input())
a = 0
b = 0
ab = 0
total = 0
for i in range(n):
	t = input()
	total += t.count("AB")
	if t[-1] == "A" and t[0] == "B":
		ab += 1
	else:
		a += t[-1] == "A"
		b += t[0] == "B"
	
if ab == 0:
	print(total + min(a,b))
elif a == 0 and b == 0:
	print(total + ab - 1)
elif a > 0 and b > 0:
	print(total + ab + min(a, b) - 1)
else:
	print(total + ab)

Fixed code:
n = int(input())
a = 0
b = 0
ab = 0
total = 0
t = []

for i in range(n):
	t.append(input())

for i in t:
	total += i.count("AB")
	if i[-1] == "A" and i[0] == "B":
		ab += 1
	else:
		a += i[-1] == "A"
		b += i[0] == "B"
if ab == 0:
	print(total + min(a,b))
elif a == 0 and b == 0:
	print(total + ab - 1)
elif a > 0 and b > 0:
	print(total + ab + min(a, b))
else:
	print(total + ab)
Buggy code:
n=int(input())
s=[0]*n
ct=0
a=0
b=0
c=0
for i in range(n):
    s[i]=input()
for i in range(n):
    for j in range(len(s[i])):
        t=s[i]
        ts=list(t)
        ts.append(0)
        if ts[j]=='A' and ts[j+1]=='B':
            ct+=1
    if ts[0]=='B' and ts[len(s[i])-1]!='A':
        a+=1
    if ts[len(s[i])-1]=='A' and ts[0]!='B':
        b+=1
    if ts[0]=='B' and ts[len(s[i])-1]=='A':
        c+=1
ct += max(0,c -1)
if c !=0:
  if a > 0 and b >0:
    ct += 2 + min(a-1,b-1)
  elif head>0:
    ct += 1 + min(a-1,b)
  elif last>0:
    ct += 1 + min(a,b-1)
else:
  ct += min(a,b)
print(ct)

Fixed code:
n=int(input())
s=[0]*n
ct=0
a=0
b=0
c=0
for i in range(n):
    s[i]=input()
for i in range(n):
    for j in range(len(s[i])):
        t=s[i]
        ts=list(t)
        ts.append(0)
        if ts[j]=='A' and ts[j+1]=='B':
            ct+=1
    if ts[0]=='B' and ts[len(s[i])-1]!='A':
        a+=1
    if ts[len(s[i])-1]=='A' and ts[0]!='B':
        b+=1
    if ts[0]=='B' and ts[len(s[i])-1]=='A':
        c+=1
ct += max(0,c -1)
if c !=0:
  if a > 0 and b >0:
    ct += 2 + min(a-1,b-1)
  elif a>0:
    ct += 1 + min(a-1,b)
  elif b>0:
    ct += 1 + min(a,b-1)
else:
  ct += min(a,b)
print(ct)
Buggy code:
n = int(input())
s = []

for i in range(n):
    s.append(list(input()))

x = 0    

for i in range(n):
    for j in range(1,len(s[i])):
        if s[i][j-1]=="A" and s[i][j]=="B":
            x += 1

p = 0
q = 0
r = 0
for i in range(n):
    if s[i][0]=="B":
        p += 1
    if s[i][-1]=="A":
        q += 1
    if s[i][0]== "B" and s[i][-1]=="A":
        r +=1

if p == q :
    print(x-1+min(p,q))
else:
    print(x+min(p,q))


Fixed code:
n = int(input())
s = []

for i in range(n):
    s.append(list(input()))

x = 0    

for i in range(n):
    for j in range(1,len(s[i])):
        if s[i][j-1]=="A" and s[i][j]=="B":
            x += 1

p = 0
q = 0
r = 0
for i in range(n):
    if s[i][0]=="B" and s[i][-1]!= "A":
        p += 1
    if s[i][0]!="B" and s[i][-1]=="A":
        q += 1
    if s[i][0]== "B" and s[i][-1]=="A":
        r +=1


if p==0 and q==0 and r!=0:
    print(x+r-1)
else:
    print(x+r+min(p,q))
Buggy code:
import sys
input = sys.stdin.readline

N = int(input())
S = [input() for _ in range(N)]
l, m, n = 0, 0, 0
ans = 0
for s in S:
    if s[0] == 'B' and s[-2] == 'A':
        n += 1
    elif s[0] == 'B':
        m += 1
    elif s[-2] == 'A':
        l += 1
    for i in range(len(s)-1):
        if s[i:i+2] == 'AB':
            ans += 1
ans += min(l, m)
if n > 0:
    ans += n - 1
if l != m and l != 0 and m != 0:
    ans += 1
print(ans)

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

N = int(input())
S = [input() for _ in range(N)]
l, m, n = 0, 0, 0
ans = 0
for s in S:
    if s[0] == 'B' and s[-2] == 'A':
        n += 1
    elif s[0] == 'B':
        m += 1
    elif s[-2] == 'A':
        l += 1
    for i in range(len(s)-1):
        if s[i:i+2] == 'AB':
            ans += 1
if n > 0:
    ans += n - 1
    if l > 0:
        l -= 1
        ans += 1
    if m > 0:
        m -= 1
        ans += 1
ans += min(l, m)
print(ans)
Buggy code:
n = int(input())
internal_AB = 0
B_A = 0
B_ = 0
_A = 0
for i in range(n):
    s = input()
    for j in range(len(s)-1):
        if s[j] == 'A' and s[j+1] == 'B':
            internal_AB += 1
    if s[0] == 'B' and s[-1] == 'A':
        B_A += 1
    elif s[0] == 'B':
        B_ += 1
    elif s[-1] == 'A':
        _A += 1
if _A == 0 and B_ == 0:
    ans = max(0,B_A - 1)
elif _A == 0 or B_ == 0:
    ans = B_A
else:
    ans = B_-1 + _A-1 + max(B_A-1, 0)
print(ans + internal_AB)





Fixed code:
n = int(input())
internal_AB = 0
B_A = 0
B_ = 0
_A = 0
for i in range(n):
    s = input()
    for j in range(len(s)-1):
        if s[j] == 'A' and s[j+1] == 'B':
            internal_AB += 1
    if s[0] == 'B' and s[-1] == 'A':
        B_A += 1
    elif s[0] == 'B':
        B_ += 1
    elif s[-1] == 'A':
        _A += 1
if _A == 0 and B_ == 0:
    ans = max(0,B_A - 1)
elif _A == 0 or B_ == 0:
    ans = B_A
else:
    if B_A > 0:
        ans = B_A +1 + min(_A-1,B_-1)
    else:
        ans = min(B_,_A)
print(ans + internal_AB)
Buggy code:
n=int(input())
a=0
b=0
ab=0
x=0
for i in range(n):
  s=input()
  l=len(s)-1
  if s[0]=='B':
    if s[l]=='A':
      ab+=1
    else:
      b+=1
  elif s[l]=='A':
    a+=1
  x+=s.count('AB')
if ab>0:
  x+=(a>0)+(b>0)
x+=max(0,ab-1)
print(x)

Fixed code:
n=int(input())
a=0
b=0
ab=0
x=0
for i in range(n):
  s=input()
  l=len(s)-1
  if s[0]=='B':
    if s[l]=='A':
      ab+=1
    else:
      b+=1
  elif s[l]=='A':
    a+=1
  x+=s.count('AB')
if ab>0:
  x+=ab-1
  x+=(a>0)+(b>0)
  a-=1;b-=1
a=max(0,a);b=max(0,b)
x+=min(a,b)
print(x)
Buggy code:
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 tailA >= 1:
    ans += same
    tailA -= 1
else:
    ans += max(same - 1, 0)

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

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

Fixed code:
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)
Buggy code:
tailA = 0
both = 0
c = 0
for i in range(int(input())):
    t = input()
    if t[0] =='B' and t[-1]=='A':
        both+=1
    elif t[0] == 'B':
        headB+=1
    elif t[-1] == 'A':
        tailA +=1
    c+=t.count('AB')
if both>0:
    c+=both-1
    headB+=1
    tailA+=1
print(c+min(headB,tailA))


Fixed code:
headB = 0
tailA = 0
both = 0
c = 0
for i in range(int(input())):
    t = input()
    if t[0] =='B' and t[-1]=='A':
        both+=1
    elif t[0] == 'B':
        headB+=1
    elif t[-1] == 'A':
        tailA +=1
    c+=t.count('AB')
if both>0:
    if tailA+headB>0:
        if tailA:
            c+=1
            tailA-=1
        if headB:
            c+=1
            headB-=1
        c+=min(headB,tailA)
        c+=both-1
    else:
        c+=both-1
else:
    c+=min(tailA,headB)
print(c)
Buggy code:
n=int(input())
cnt=0
cnta=0
cntb=0
cntba=0
for i in range(n):
    s=input()
    for i in range(1,len(s)):
        if s[i]=='B' and s[i-1]=='A':
            cnt+=1
    if s[-1]=='A' and s[0]=='B':
        cntba+=1
    elif s[-1]=="A":
        cnta+=1
    elif s[0]=="B":
        cntb+=1
print(cnt,cnta,cntb,cntba)
if cntb==0 and cntba==0:
    print(cnt)
elif cntb==0 and cntba!=0:
    print(cnt+min(cntba+cntb,cntba+cnta-1))
else:
    print(cnt+min(cntb+cntba,cnta+cntba))

Fixed code:
n=int(input())
cnt=0
cnta=0
cntb=0
cntba=0
for i in range(n):
    s=input()
    for i in range(1,len(s)):
        if s[i]=='B' and s[i-1]=='A':
            cnt+=1
    if s[-1]=='A' and s[0]=='B':
        cntba+=1
    elif s[-1]=="A":
        cnta+=1
    elif s[0]=="B":
        cntb+=1
#print(cnt,cnta,cntb,cntba)
if cntb==0 and cntba==0:
    print(cnt)
elif cntb==0 and cntba!=0:
    print(cnt+min(cntba+cntb,cntba+cnta-1))
else:
    print(cnt+min(cntb+cntba,cnta+cntba))
Buggy code:
n, *ss = open(0).read().split()

ans = 0

inter_count = sum([s.count('AB') for s in ss])

ans += inter_count

_a = sum(s[-1] == 'A' for s in ss)
b_ = sum(s[0] == 'B' for s in ss)
b_a = sum(s[0] == 'B' and s[-1] == 'A' for s in ss)
_a -= b_a
b_ -= b_a

ans += b_a-1

if b_a:
    ans += bool(b_)
    b_ -= bool(b_)
    ans += bool(_a)
    _a -= bool(_a)

ans += min(b_, _a)

print(ans)

Fixed code:
n, *ss = open(0).read().split()
ans = 0
inter_count = sum([s.count('AB') for s in ss])
ans += inter_count

_a = sum(s[-1] == 'A' for s in ss)
b_ = sum(s[0] == 'B' for s in ss)
b_a = sum(s[0] == 'B' and s[-1] == 'A' for s in ss)
_a -= b_a
b_ -= b_a

ans += max(0,b_a-1)

if b_a:
    ans += bool(b_)
    b_ -= bool(b_)
    ans += bool(_a)
    _a -= bool(_a)

ans += min(b_, _a)
print(ans)
Buggy code:
N,*L = open(0).read().split()

ans = 0
a = 0
b = 0
c = 0
for s in L:
  for j in range(len(s)-1):
    if s[j]=='A' and s[j+1]=='B':
      ans += 1
  if s[-1]=='A' and s[0]=='B':
    c += 1
  elif s[-1]=='A':
    a += 1
  elif s[0]=='B':
    b += 1
ans += c-1
c = 1
if a==b:
  ans += a
elif a<b:
  ans += a
  ans += min(b-a,c)
else:
  ans += b
  ans += min(a-b,c)
print(ans)
  

Fixed code:
N,*L = open(0).read().split()

ans = 0
a = 0
b = 0
c = 0
for s in L:
  for j in range(len(s)-1):
    if s[j]=='A' and s[j+1]=='B':
      ans += 1
  if s[-1]=='A' and s[0]=='B':
    c += 1
  elif s[-1]=='A':
    a += 1
  elif s[0]=='B':
    b += 1
if c!=0:
  if a+b>0:
    ans += c+min(a,b)
  else:
    ans += c-1
else:
  ans += min(a,b)
print(ans)
Buggy code:
N=int(input())
s=[input() for _ in range(N)]
cnt=0

A_count=0
B_count=0
BA_count=0
for u in s:
    cnt+=u.count("AB")
    if u[0]=="B":
        B_count+=1
    if u[-1]=="A":
        A_count+=1
    if u[0]=="B" and u[-1]=="A":
        BA_count+=1
if A_count==B_count==BA_count:
    if A_count==0:
        print(cnt)
    else:
        print(cnt+A_count-1)
else:
    print(cnt+min(A_count,B,count))

Fixed code:
N=int(input())
s=[input() for _ in range(N)]
cnt=0

A_count=0
B_count=0
BA_count=0
for u in s:
    cnt+=u.count("AB")
    if u[0]=="B":
        B_count+=1
    if u[-1]=="A":
        A_count+=1
    if u[0]=="B" and u[-1]=="A":
        BA_count+=1
if A_count==B_count==BA_count:
    if A_count==0:
        print(cnt)
    else:
        print(cnt+A_count-1)
else:
    print(cnt+min(A_count,B_count))
Buggy code:
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"):
        a += 1
    elif S[i].endswith("A"):
        b += 1
cnt += ba - 1
if a >= 1:
    cnt += 1
if b >= 1:
    cnt += 1
print(cnt)


Fixed code:
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)
Buggy code:
n = int(input())
S = [input() for _ in range(n)]

count = 0
ba = 0
wa = 0
bw = 0
for s in S:
    count += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        ba += 1
    elif s[0] == 'B':
        bw += 1
    elif s[-1] == 'A':
        wa += 1

if ba:
    count += ba - 1
    ba = 1
if ba:
    if wa and bw:
        count += 2
        wa -= 1
        bw -= 1
        ba -= 1
    if ba:
        if wa < bw:
            wa += 1
        else:
            bw += 1
count += min(wa,bw)

print(count)´

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

count = 0
ba = 0
wa = 0
bw = 0
for s in S:
    count += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        ba += 1
    elif s[0] == 'B':
        bw += 1
    elif s[-1] == 'A':
        wa += 1

if ba:
    count += ba - 1
    ba = 1
if ba:
    if wa and bw:
        count += 2
        wa -= 1
        bw -= 1
        ba -= 1
    if ba:
        if wa < bw:
            wa += 1
        else:
            bw += 1
count += min(wa,bw)

print(count)
Buggy code:
import sys
stdin=sys.stdin

ip=lambda: int(sp())
fp=lambda: float(sp())
lp=lambda:list(map(int,stdin.readline().split()))
sp=lambda:stdin.readline().rstrip()

n=ip()
a=[]
b=[]
ans=0
for i in range(n):
  s=sp()
  if s[0]=='B':
    b.append(i)
  if s[-1]=='A':
    a.append(i)
  for j in range(len(s)-1):
    if s[j]=='A' and s[j+1]=='B':
      ans+=1
      
c=len(list(set(a)&set(b)))
a_rest=len(a)-c
b_rest=len(b)-c
if c==0:
  omake=min(a_rest,b_rest)
else:
  if a_rest==b_rest==0:
    omake=c-1
  else:
    omake=c+min(a_rest,b_rest)+min((max(a_rest,b_rest)-min(a_rest,b_rest)),1)
  
print(ans+omake)




Fixed code:
import sys
stdin=sys.stdin

ip=lambda: int(sp())
fp=lambda: float(sp())
lp=lambda:list(map(int,stdin.readline().split()))
sp=lambda:stdin.readline().rstrip()

n=ip()
a=[]
b=[]
ans=0
for i in range(n):
  s=sp()
  if s[0]=='B':
    b.append(i)
  if s[-1]=='A':
    a.append(i)
  for j in range(len(s)-1):
    if s[j]=='A' and s[j+1]=='B':
      ans+=1
      
c=len(list(set(a)&set(b)))
a_rest=len(a)-c
b_rest=len(b)-c
if c==0:
  omake=min(a_rest,b_rest)
else:
  if a_rest==b_rest==0:
    omake=c-1
  else:
    omake=c+min(a_rest,b_rest)
  
print(ans+omake)