qyliang/CodeNanoFix huggingface dataset:
Buggy code:
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'):
            print('ok')
            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()


Fixed code:
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()
Buggy code:
import re
N = int(input())
S = [input() for i in range(N)]
nAB = sum(map(lambda x: x.count('AB'), S))
lastB = sum(map(lambda x: x[0] == 'B', S))
firstA = sum(map(lambda x: x[-1] == 'A', S))
matchAB = sum(map(lambda x: x[0] == 'B' and x[-1] == 'A', S))
if min(lastB, firstA) == 0:
    print(nAB)
elif matchAB - min(lastB, firstA) == 0:
    print(nAB+min(lastB, firstA)-1)
else:
    print()
    print(nAB+min(lastB, firstA))


Fixed code:
import re
N = int(input())
S = [input() for i in range(N)]
nAB = sum(map(lambda x: x.count('AB'), S))
lastB = sum(map(lambda x: x[0] == 'B', S))
firstA = sum(map(lambda x: x[-1] == 'A', S))
matchAB = sum(map(lambda x: x[0] == 'B' and x[-1] == 'A', S))
if min(lastB, firstA) == 0:
    print(nAB)
elif lastB == firstA and matchAB - min(lastB, firstA) == 0:
    print(nAB+min(lastB, firstA)-1)
elif matchAB - min(lastB, firstA) == 0:
    print(nAB+min(lastB, firstA))
else:
    print(nAB+min(lastB, firstA))
Buggy code:
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]))

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

def main():
    input = sys.stdin.readline
    N = int(input())
    ans = 0
    ba = 0  # Num. of str that the first letter is B and the last one is A.
    xb = 0  # Num. of str that the last letter is B and the first one is not A.
    ax = 0  # Num. of str that the first letter is A and the last one is not B.
    for _ in range(N):
        s = str(input().strip())

        # Count the number of AB contained in the initial strings.
        for i in range(len(s)):
            if s[i:i+2] == 'AB':
                ans += 1

        if s[0] == 'B' and s[-1] == 'A':
            ba += 1
        elif s[0] == 'B':
            xb += 1
        elif s[-1] == 'A':
            ax += 1

    if max(xb, ax) == 0:
        # N of ba make N-1 new "AB".
        # BxxA + BxxA + ... + BxxA -> BxxABxxAB...ABxxA
        ans += ba - 1

    else:  # There is at least one bx or ax.
        # N of ba make N-1 new "AB" and a ba.
        #   ba + ba + ... + ba -> baba...ba = ba
        # The last remaining ba and bx/ax make a new "AB" and a new bx/ax.
        #   ba + bx -> bABx = bx,  xa + ba -> xABa = xa
        # Then, bx and xa make a new "AB".
        #   xa + bx -> xABx 
        ans += ba + min(xa, bx)

    return ans


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


Fixed code:
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    ans = 0
    ba = 0  # Num. of str that the first letter is B and the last one is A.
    xa = 0  # Num. of str that the last letter is A and the first one is not B.
    bx = 0  # Num. of str that the first letter is B and the last one is not A.
    for _ in range(N):
        s = str(input().strip())

        # Count the number of AB contained in the initial strings.
        for i in range(len(s)):
            if s[i:i+2] == 'AB':
                ans += 1

        if s[0] == 'B' and s[-1] == 'A':
            ba += 1
        elif s[-1] == 'A':
            xa += 1
        elif s[0] == 'B':
            bx += 1

    if ba == 0:
        # One xa ans one bx make one new "AB".
        #   xa + bx -> xABx
        ans += min(xa, bx)

    elif max(xa, bx) == 0:
        # N of ba make N-1 new "AB".
        # BxxA + BxxA + ... + BxxA -> BxxABxxAB...ABxxA
        ans += ba - 1

    else:  # There is at least one bx or ax and at least one ba.
        # N of ba make N-1 new "AB" and a ba.
        #   ba + ba + ... + ba -> baba...ba = ba
        # The last remaining ba and bx/ax make a new "AB" and a new bx/ax.
        #   ba + bx -> bABx = bx,  xa + ba -> xABa = xa
        # Then, bx and xa make a new "AB".
        #   xa + bx -> xABx 
        ans += ba + min(xa, bx)

    return ans


if __name__ == '__main__':
    print(main())
Buggy code:
n = int(input())
a = 0
b = 0
ab = 0
b_a = 0
for i in range(n):
    s = input()
    if s[0] == "B" and s[-1] == "A":
        b_a += 1
    elif s[0] == "B":
        b += 1
    elif s[-1] == "A":
        a += 1
    for j in range(len(s) - 1):
        if s[j] == "A" and s[j + 1] == "B":
            ab += 1
if a > b:
    a, b = b, a
ans = ab
if b == 0:
    if b_a != 0:
        ans += b_a - 1
elif b == 1:
    ans += b_a
else:
    ans += b_a + a
print(ans)


Fixed code:
n = int(input())
a = 0
b = 0
ab = 0
b_a = 0
for i in range(n):
    s = input()
    if s[0] == "B" and s[-1] == "A":
        b_a += 1
    elif s[0] == "B":
        b += 1
    elif s[-1] == "A":
        a += 1
    for j in range(len(s) - 1):
        if s[j] == "A" and s[j + 1] == "B":
            ab += 1
if a > b:
    a, b = b, a
ans = ab
if b == 0:
    if b_a != 0:
        ans += b_a - 1
elif a + b == 1:
    ans += b_a
else:
    ans += b_a + a
print(ans)
Buggy code:
N = int(input())
ans = 0
A,B,AB = 0,0,0
for i in range(N):
    s = input()
    ans += s.count('AB')
    if s[0] == 'B' and s[-1] ==  'A':
        AB += 1
    elif s[0] == 'B':
        B += 1
    elif s[-1] == 'A':
        A += 1
add =  0
add += (AB - 1)
if AB != 0:
    if A >= 1:
        A -= 1
        add += 1
    if B >= 1:
        B -= 1
        add += 1
add += min(A,B)

print(min(add,N-1) + ans)

Fixed code:
N = int(input())
ans = 0
A,B,AB = 0,0,0
for i in range(N):
    s = input()
    ans += s.count('AB')
    if s[0] == 'B' and s[-1] ==  'A':
        AB += 1
    elif s[0] == 'B':
        B += 1
    elif s[-1] == 'A':
        A += 1


add =  0

if AB != 0:
    add += (AB - 1)
    if A >= 1:
        A -= 1
        add += 1
    if B >= 1:
        B -= 1
        add += 1
add += min(A,B)

print(min(add,N-1) + ans)
Buggy code:
N= int(input())
S = [input() for _ in range(N)]

IB= [0] * (N)
LA = [0] * (N)
flag = True
Eflag = False
cntb= 0
cnta = 0
ans = 0
for i in range(N):
    if S[i][0] == 'B':
        IB[i] = 1
        cntb += 1
    if S[i][len(S[i])-1] == 'A' :
        LA[i] = 1
        cnta += 1
    if (IB[i] == 1 and LA[i] == 0) or (IB[i]==0 and LA[i] == 1):
        flag = False
    elif (IB[i] == 1 and LA[i] == 1):
        Eflag = True
    for j in range(len(S[i])):
        if S[i][j] == 'A' and S[i][j+1]=='B' : ans += 1
        
        
ans += min(cntb,cnta)
if Eflag and flag:    
    ans -= 1



print(ans)
    


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

IB= [0] * (N)
LA = [0] * (N)
flag = True
Eflag = False
cntb= 0
cnta = 0
ans = 0
for i in range(N):
    if S[i][0] == 'B':
        IB[i] = 1
        cntb += 1
    if S[i][len(S[i])-1] == 'A' :
        LA[i] = 1
        cnta += 1
    if (IB[i] == 1 and LA[i] == 0) or (IB[i]==0 and LA[i] == 1):
        flag = False
    elif (IB[i] == 1 and LA[i] == 1):
        Eflag = True
    for j in range(len(S[i])-1):
        if S[i][j] == 'A' and S[i][j+1]=='B' : ans += 1
        
        
ans += min(cntb,cnta)
if Eflag and flag:    
    ans -= 1



print(ans)
Buggy code:
N = int(input())
ans = 0
a = 0
b = 0
ab = 0
for i in range(N):
    s = input()
    ans += s.count("AB")
    a += s[-1]=="A"
    b += s[0]=="B"
    ab += (s[-1]=="A" and s[0]=="B")
if a==b==ab:
    if a!=0:
    ans += a - 1
else:
    ans += min(a, b)
print(ans)


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

print(s)

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

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

s = c
m = min(a, b)
s += m
a -= m
b -= m

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

print(s)

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

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

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

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

s += min(a, b)

print(s)
Buggy code:
N = int(input())

ab = 0
x = 0
y = 0
z = 0

for _ in range(N):
    s = input()
    ab += s.count("AB")
    x += (s[-1] == "A")
    y += (s[0] == "B")
    z += (s[-1] == "A" and s[0] == "B")

if x or y:
    print(ab + min(x+z, y+z))
else:
    print(ab + z-1)


Fixed code:
N = int(input())

ab = 0
x = 0
y = 0
z = 0

for _ in range(N):
    s = input()
    ab += s.count("AB")
    if (s[-1] == "A" and s[0] == "B"):
        z += 1
    elif (s[-1] == "A"):
        x += 1
    elif (s[0] == "B"):
        y += 1

if (not x and not y) and z:
    print(ab + z-1)
else:
    print(ab + min(x+z, y+z))
Buggy code:
n=int(input())
a_num=0
b_num=0
set_num=0
ans=0
for i in range(n):
    s=list(input())
    for j in range(len(s)-1):
        if s[j]+s[j+1]=="AB":
            ans+=1
    if s[0]=="B" and s[-1]=="A":
        set_num+=1
    elif s[0]=="B":
        b_num+=1
    elif s[-1]=="A":
        a_num+=1
if set_num==0:
    print(ans+min(a_num,b_num))
else:
    ans+=set_num-1
    if 0<a_num:
        a_num+=1
    if 0<b_num:
        b_num+=1
    print(ans+min(a_num,b_num))

Fixed code:
n=int(input())
a_num=0
b_num=0
set_num=0
ans=0
for i in range(n):
    s=list(input())
    for j in range(len(s)-1):
        if s[j]+s[j+1]=="AB":
            ans+=1
    if s[0]=="B" and s[-1]=="A":
        set_num+=1
    elif s[0]=="B":
        b_num+=1
    elif s[-1]=="A":
        a_num+=1
if set_num==0:
    print(ans+min(a_num,b_num))
else:
    ans+=set_num-1
    if not(a_num==0 and b_num==0):
        a_num+=1
        b_num+=1
    print(ans+min(a_num,b_num))
Buggy code:
N = int(input())
a, b, ab, ret = 0, 0, 0, 0
for i in range(N):
    s = input()
    l = len(s) - 1
    if s[0] == "B":
        b += 1
    if s[l] == "A":
        a += 1
    ret += s.count("AB")
if a == b:
    a -= 1
ab = min(a,b)
print(ret + ab)

Fixed code:
N = int(input())
a, b, ab, ret = 0, 0, 0, 0
for i in range(N):
    s = input()
    l = len(s) - 1
    if s[0] == "B" and s[l] == "A":
        ab += 1
    if s[0] == "B":
        b += 1
    if s[l] == "A":
        a += 1
    ret += s.count("AB")
if a == b and a == ab and a > 0:
    a -= 1
ab = min(a,b)
print(ret + ab)
Buggy code:
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
print(AB + min(_A,B_) +min(B_A,abs(_A-B_)) )

Fixed code:
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))
Buggy code:
n,*s=open(0)
a=b=c=d=0
for t in s:
    if t[-2]=='A'and t[0]=='B':
        d+=1
    elif t[-2]=='A':
        a+=1
    elif t[0]=='B':
        b+=1
    c+=t.count('AB')
if all(i==0for i in(a,b)):
    print(c+d-1)
    exit()
elif a==b:
    print(a+b+c+max(d-1,0))
    exit()
ans=min(a,b)+c
dif=abs(a-b)
ans+=min(dif,d)
d-=min(dif,d)
print(ans+d)

Fixed code:
n,*s=open(0)
a=b=c=d=0
for t in s:
    if t[-2]=='A'and t[0]=='B':
        d+=1
    elif t[-2]=='A':
        a+=1
    elif t[0]=='B':
        b+=1
    c+=t.count('AB')
if all(i==0for i in(a,b)):
    print(c+max(0,d-1))
    exit()
elif a==b:
    print(a+c+d)
    exit()
ans=min(a,b)+c
dif=abs(a-b)
ans+=min(dif,d)
d-=min(dif,d)
print(ans+d)
Buggy code:
N = int(input())
ans = 0
b_x = 0
b_a = 0
x_a = 0
y = 0
for _ in range(N):
  s = input()
  ans += s.find('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
elif x_a >= 1 or b_x >= 1:
  ans += b_a - 1
else:
  ans += b_a - 2
if x_a - 1 <= 0 or b_x -1 <= 0:
  ans += 0
else:
  ans += min(x_a - 1, b_x - 1)
print(ans)  

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

AB=min(a,b)
a,b=a-AB,b-AB
if not c==0:
    cnt +=c-1
    if a!=0 or b!=0:
        cnt +=1
print(cnt)

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

Fixed code:
N = int(input())
A = 0
B = 0
ans = 0
case = 0
for i in range(N):
  S = input()
  for j in range(len(S)-1):
    if S[j] == 'A' and S[j+1] == 'B':
      ans += 1
  if S[0] == 'B' and S[len(S)-1] == 'A':
    case += 1
  else:
    if S[0] == 'B':
      B += 1
    if S[len(S)-1] == 'A':
      A += 1
if case == 0:
  print(ans+min(A,B))
else:
  if A > 0 and B > 0:
    print(ans+case+1+min(A-1,B-1))
  elif A > 0 or B > 0:
    print(ans+case)
  else:
    print(ans+case-1)
Buggy code:
def ab_substrings(N: int, S: list)->int:
    res = 0
    endA = 0
    startB = 0
    both = 0

    for s in S:
        res += s.count('AB')
        if s.endswith('A'):
            if s.startswith('B'):
                both += 1
            else:
                endA += 1
        else:
            startB += 1

    if both > 0:
        res += both - 1

        if endA > 0:
            res += 1
            endA -= 1

        if startB > 0:
            res += 1
            startB -= 1

    return res + min(endA, startB)


if __name__ == "__main__":
    N = int(input())
    S = [input() for _ in range(N)]

    ans = ab_substrings(N, S)
    print(ans)


Fixed code:
def ab_substrings(N: int, S: list)->int:
    res = 0
    endA = 0
    startB = 0
    both = 0

    for s in S:
        res += s.count('AB')
        if s.endswith('A'):
            if s.startswith('B'):
                both += 1
            else:
                endA += 1
        elif s.startswith('B'):
            startB += 1

    if both > 0:
        res += both - 1

        if endA > 0:
            res += 1
            endA -= 1

        if startB > 0:
            res += 1
            startB -= 1

    return res + min(endA, startB)


if __name__ == "__main__":
    N = int(input())
    S = [input() for _ in range(N)]

    ans = ab_substrings(N, S)
    print(ans)
Buggy code:
n=int(input())
ls=[0]*n
for i in range(n):
    ls[i]=input()
numab=0
sA=0
sB=0
sBA=0
for i in range(n):
    sb=""
    for j in ls[i]:
        if sb=="A":
            if j=="B":
                numab+=1
        sb=j
    if ls[i][0]=="B":
        if ls[i][-1]=="A":
            sBA+=1
        else:
            sB+=1
    if ls[i][-1]=="A" and ls[i][0]!="B":
        sA+=1
ans=0
if sA!=0 and sB!=0:
    ans=sBA+1
elif sA==0 and sB!=0:
    ans=sBA
elif sB==0 and sA!=0:
    ans=sBA
else:
    ans=sBA-1
ans+=numab
#print(numab,sB,sA,sBA)
print(ans if ans>0 else 0)


Fixed code:
n=int(input())
ls=[0]*n
for i in range(n):
    ls[i]=input()
numab=0
sA=0
sB=0
sBA=0
for i in range(n):
    sb=""
    for j in ls[i]:
        if sb=="A":
            if j=="B":
                numab+=1
        sb=j
    if ls[i][0]=="B":
        if ls[i][-1]=="A":
            sBA+=1
        else:
            sB+=1
    if ls[i][-1]=="A" and ls[i][0]!="B":
        sA+=1
ans=0
if sA!=0 and sB!=0:
    ans=sBA+1+min(sA-1,sB-1)
elif sA==0 and sB!=0:
    ans=sBA
elif sB==0 and sA!=0:
    ans=sBA
else:
    ans=sBA-1
if ans<0:
    ans=0
ans+=numab
#print(numab,sB,sA,sBA)
print(ans)
Buggy code:
N = int(input())
ans = 0
dic = {'A':0, 'B':0, 'BA':0}
for i in range(N):
    tmp = input()
    if 'AB' in tmp:
        ans += tmp.count('AB')
    tmp = tmp.replace('AB','ZZ')
    if tmp[-1] == 'A' and tmp[0] == 'B':
        dic['BA'] += 1
    elif tmp[0] == 'B':
        dic['B'] += 1
    elif tmp[-1] == 'A':
        dic['A'] += 1

if dic['B'] >= 1 and dic['A'] >= 1 and dic['BA'] >= 1:
    ans += dic['BA'] + 1
    dic['BA'] = 0
    dic['B'] -= 1
    dic['A'] -= 1
if dic['BA'] >= 1:
    if dic['A'] == 0 and dic['B'] == 0:
        ans += dic['BA'] - 1
    elif dic['A'] == 0 or dic['B'] == 0:
        ans += dic['BA']

print(ans)

Fixed code:
N = int(input())
ans = 0
dic = {'A':0, 'B':0, 'BA':0}
for i in range(N):
    tmp = input()
    if 'AB' in tmp:
        ans += tmp.count('AB')
    tmp = tmp.replace('AB','ZZ')
    if tmp[-1] == 'A' and tmp[0] == 'B':
        dic['BA'] += 1
    elif tmp[0] == 'B':
        dic['B'] += 1
    elif tmp[-1] == 'A':
        dic['A'] += 1

if dic['B'] >= 1 and dic['A'] >= 1 and dic['BA'] >= 1:
    ans += dic['BA'] + 1
    dic['BA'] = 0
    dic['B'] -= 1
    dic['A'] -= 1
if dic['BA'] >= 1:
    if dic['A'] == 0 and dic['B'] == 0:
        ans += dic['BA'] - 1
    elif dic['A'] == 0 or dic['B'] == 0:
        ans += dic['BA']
else:
    ans += min(dic['A'],dic['B'])
print(ans)
Buggy code:
n = int(input())

ans = 0
last_a = 0
first_b = 0
cnt = 0

for i in range(n):
    s = input()
    ans += s.count("AB")
    if s[-1] == "A":
        last_a += 1
    if s[0] == "B":
        first_b += 1
    if s[-1] == "A" and s[0] == "B":
        cnt += 1

if last_a == last_b:
    ans += min(last_a, last_b) - 1
else:
    ans += min(last_a, last_b)

print(ans)

Fixed code:
n = int(input())

ans = 0
last_a = 0
first_b = 0
cnt = 0

for i in range(n):
    s = input()
    ans += s.count("AB")
    if s[-1] == "A":
        last_a += 1
    if s[0] == "B":
        first_b += 1
    if s[-1] == "A" and s[0] == "B":
        cnt += 1

if last_a == first_b & cnt > 0:
    ans += min(last_a, first_b) - 1
else:
    ans += min(last_a, first_b)

print(ans)
Buggy code:
import sys

input = sys.stdin.readline

N = int(input())
A, B, AB = 'A', 'B', 'AB'
endA, startB, startB_endA = 0, 0, 0
ans = 0

for _ in range(N):
    S = input().rstrip()

    if S[0] == B:
        startB += 1

        if S[-1] == A:
            startB -= 1
            startB_endA += 1

    elif S[-1] == A:
        endA += 1
    

    for i in range(1,len(S)):
        if S[i-1] + S[i] == AB:
            ans += 1


if startB_endA == 0:
    ans = min(endA, startB)

elif endA == 0 and startB == 0:
    ans = max(0, startB_endA - 1)

else:
    ans = startB_endA + min(endA, startB)

print(ans)

Fixed code:
import sys

input = sys.stdin.readline

N = int(input())
A, B, AB = 'A', 'B', 'AB'
endA, startB, startB_endA = 0, 0, 0
ans = 0

for _ in range(N):
    S = input().rstrip()

    if S[0] == B:
        startB += 1

        if S[-1] == A:
            startB -= 1
            startB_endA += 1

    elif S[-1] == A:
        endA += 1
    

    for i in range(1,len(S)):
        if S[i-1] + S[i] == AB:
            ans += 1


if startB_endA == 0:
    ans += min(endA, startB)

elif endA == 0 and startB == 0:
    ans += max(0, startB_endA - 1)

else:
    ans += startB_endA + min(endA, startB)

print(ans)
Buggy code:
n,*t=open(0).read().split()
n=int(n)
a=0
b=0
c=0
ans=0
for i in t:
    if i[0]=="B"and i[-1]=="A":
        a+=1
    if i[0]=="B":
        b+=1
    if i[-1]=="A":
        c+=1
    ans+=i.count("AB")
if a>0 and b>0 and c>0:
    ans+=a+1
    a=0
    b-=1
    c-=1
if a>0 and b>0:
    ans+=a
    a=0
    b-=1
if a>0 and c>0:
    ans+=a
    a=0
    c-=1
if a>0:
    ans+=a-1
    a=0
if b>0 and c>0:
    ans+=min(b,c)
print(ans)



Fixed code:
n,*t=open(0).read().split()
n=int(n)
a=0
b=0
c=0

ans=0
for i in t:
    if i[0]=="B"and i[-1]=="A":
        a+=1
    elif i[0]=="B":
        b+=1
    elif i[-1]=="A":
        c+=1
    ans+=i.count("AB")
if a>0 and b>0 and c>0:
    ans+=a+1
    a=0
    b-=1
    c-=1
if a>0 and b>0:
    ans+=a
    a=0
    b-=1
if a>0 and c>0:
    ans+=a
    a=0
    c-=1
if a>0:
    ans+=a-1
    a=0
if b>0 and c>0:
    ans+=min(b,c)
print(ans)
Buggy code:
N = int(input())
a, b, ab = 0, 0, 0

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

if a>b :
    print(ab+b)
elif a==b:
    print(ab+a-1)
else: 
    print(ab+a)

Fixed code:
N = int(input())
a, b, bxxa, ab = 0, 0, 0, 0

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

if a>b :
    print(ab+b)
elif a!=0 and a==b and bxxa==a:
    print(ab+a-1)
else: 
    print(ab+a)