qyliang/CodeNanoFix huggingface dataset:
Buggy code:
N = int(input())
s_list = []
count = 0
n, m, l = 0, 0, 0

for i in range(N):
  s = input()
  s_list.append(s)
  count += s.count('AB')
  if s[0] == 'B' and s[-1] == 'A':
    n += 1
    continue
  elif s[-1] == 'A':
    m += 1
  elif s[0] == 'B':
    l += 1

if n < m and n < l:
  print(int(count + 2 * n + min(m - n, l - n)))
else:
  print(int(count + 2 * n))

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

if m == 0 and l == 0:
  if n == 0:
    print(int(count))
  else:
    print(int(count + (n - 1)))
else:
  print(int(count + min(n+m, n+l)))
Buggy code:

# coding: utf-8

# In[5]:

import sys
input = sys.stdin.readline
N = int(input())
S = [input() for i in range(N)]


# In[12]:

b_cnt = 0
a_cnt = 0
ab_cnt = 0
for i in range(N):
    if S[i][0]=='B'and S[i][-1]=='A':
        ab_cnt += 1
    elif S[i][0]=='B':
        b_cnt += 1
    elif S[i][-1]=='A':
        a_cnt += 1
#print(b_cnt, a_cnt)
#print(ab_cnt, a_cnt, b_cnt, '/'.join(S).count('AB') )

if abs(a_cnt-b_cnt) == 1 and ab_cnt > 0:
    ab_cnt += 1
if max(a_cnt, b_cnt) == 0 and ab_cnt > 0:
    ab_cnt -= 1
    
#if ab_cnt < 0:
#    ab_cnt = 0
#ab_cnt = ab_cnt-1 if ab_cnt!=0 else ab_cnt
print('/'.join(S).count('AB') + min(a_cnt, b_cnt, N-1) + ab_cnt)


# In[ ]:





Fixed code:

# coding: utf-8

# In[17]:

import sys
#input = sys.stdin.readline
N = int(input())
S = [input() for i in range(N)]


# In[18]:

b_cnt = 0
a_cnt = 0
ab_cnt = 0
for i in range(N):
    if S[i][0]=='B'and S[i][-1]=='A':
        ab_cnt += 1
    elif S[i][0]=='B':
        b_cnt += 1
    elif S[i][-1]=='A':
        a_cnt += 1
#print(b_cnt, a_cnt)
#print(ab_cnt, a_cnt, b_cnt, '/'.join(S).count('AB') )

#if abs(a_cnt-b_cnt) >= 1 and ab_cnt > 0:
ans = 0
if ab_cnt > 0:
    ans += ab_cnt - 1
    if b_cnt > 0:
        b_cnt -= 1
        ans += 1
    if a_cnt > 0:
        a_cnt -= 1
        ans += 1
ans += min(a_cnt, b_cnt)

"""
if max(a_cnt, b_cnt) == 0 and ab_cnt > 0:
    ab_cnt -= 1
elif min(a_cnt, b_cnt) > 0 and ab_cnt > 0:
    ab_cnt += 1
elif abs(a_cnt-b_cnt) == 0 and ab_cnt > 0:
    ab_cnt += 1
elif abs(a_cnt-b_cnt) > 0 and ab_cnt > 0:
    ab_cnt += 0
"""
    
#if ab_cnt < 0:
#    ab_cnt = 0
#ab_cnt = ab_cnt-1 if ab_cnt!=0 else ab_cnt
print('/'.join(S).count('AB') + ans)


# In[ ]:
Buggy code:
n=int(input())
lb=0
ra=0
ba=0
ans=0

import re

for _ in range(n):
    s=input()

    ans+=s.count('AB')

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

tmp=min(lb,ra,ba)
ans += 2*tmp

lb-=tmp
ra-=tmp
ba-=tmp
if lb<0:
    lb=0
if ra<0:
    ra=0
if ba<0:
    ba=0

if lb==0 and ra==0 and ba>0:
    ans+=ba-1
elif lb!=0 or ra!=0:
    ans+=min(lb,ra)
    ans+=min(lb,ba)
    ans+=min(ra,ba)


print(ans)

Fixed code:
n=int(input())
lb=0
ra=0
ba=0
ans=0

import re

for _ in range(n):
    s=input()

    ans+=s.count('AB')

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

if ba>0:
    ans+=ba-1
    ba=1

tmp=min(lb,ra,ba)
ans += 2*tmp

lb-=tmp
ra-=tmp
ba-=tmp

if lb<0:
    lb=0
if ra<0:
    ra=0
if ba<0:
    ba=0

if lb!=0 or ra!=0:
    ans+=min(lb,ra)
    ans+=min(lb,ba)
    ans+=min(ra,ba)


print(ans)
Buggy code:
n=int(input())
sum=0
a=0
b=0
ab=0
for i in range(n):
    x=input()
    m=len(x)
    if x[0]=='B' and x[m-1]=='A':
        ab+=1
    elif x[0]=='B':
        b+=1
    elif x[m-1]=='A':
        a+=1
    
    for j in range(m-1):
        if x[j]=='A' and x[j+1]=='B':
            sum+=1
    
if a==b:
    print(sum+a+ab-1)
else:
    print(sum+min(a,b)+ab)

Fixed code:
n=int(input())
sum=0
a=0
b=0
ab=0
for i in range(n):
    x=input()
    m=len(x)
    if x[0]=='B' and x[m-1]=='A':
        ab+=1
    elif x[0]=='B':
        b+=1
    elif x[m-1]=='A':
        a+=1
    
    for j in range(m-1):
        if x[j]=='A' and x[j+1]=='B':
            sum+=1
    
if ab==0:
    print(sum+min(a,b))
else:
    if a==0 and b==0:
        print(sum+ab-1)
    elif a==0 or b==0:
        print(sum+ab)
    else:
        print(sum+ab+1+min(a-1,b-1))
Buggy code:
N=int(input())
countab,counta,countb=0,0,0
for i in range(N):
	s=input()
	if (s[0]=='B')&(s[-1]!='A'):
		countb+=1
	if (s[-1]=='A')&(s[0]!='B'):
		counta+=1
	countab+=s.count('AB')
print(min(counta,countb)+countab)

Fixed code:
N=int(input())
countab,counta,countb,count=0,0,0,0
for i in range(N):
	s=input()
	if (s[0]=='B')&(s[-1]!='A'):
		countb+=1
	if (s[-1]=='A')&(s[0]!='B'):
		counta+=1
	if (s[0]=='B')&(s[-1]=='A'):
		count+=1
	countab+=s.count('AB')
if count==0: print(min(counta,countb)+countab)
elif counta+countb==0: print(count-1+countab)
else: print(min(counta,countb)+countab+count)
Buggy code:
from collections import defaultdict
from string import ascii_uppercase
import sys, bisect, math
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N = read_int()[0]
S = [read_str() for _ in range(N)]

def solve():
    _a = 0
    b_ = 0
    ba = 0
    in_ab = 0

    # search
    for i in range(N):
        if S[i][0] == 'B' and S[i][-1:] == 'A':
            ba += 1
        elif S[i][0] == 'B':
            b_ += 1
        elif S[i][-1:] == 'A':
            _a += 1
        for j in range(len(S[i]) - 1):
            if S[i][j: j + 2] == 'AB':
                in_ab += 1

    ans = 0
    ans += in_ab + ba - 1 + max(0, min(_a - 1, b_ - 1))
    print(in_ab, ba, ans)
    if _a > 0: ans += 1
    if b_ > 0: ans += 1
    return ans

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

Fixed code:
from collections import defaultdict
from string import ascii_uppercase
import sys, bisect, math
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N = read_int()[0]
S = [read_str() for _ in range(N)]

def solve():
    _a = 0
    b_ = 0
    ba = 0
    in_ab = 0

    # search
    for i in range(N):
        if S[i][0] == 'B' and S[i][-1:] == 'A':
            ba += 1
        elif S[i][0] == 'B':
            b_ += 1
        elif S[i][-1:] == 'A':
            _a += 1
        for j in range(len(S[i]) - 1):
            if S[i][j: j + 2] == 'AB':
                in_ab += 1

    ans = 0
    if ba == 0:
        ans += in_ab + min(_a, b_)
        return ans
    ans += in_ab + max(ba - 1,0) + max(0, min(_a - 1, b_ - 1))
    # print(in_ab, ba, ans)
    if _a > 0: ans += 1
    if b_ > 0: ans += 1
    return ans

if __name__ == "__main__":
    print(solve())
Buggy code:
n = int(input().rstrip())
s = [input().rstrip() for _ in range(n)]

bstart, aend = 0, 0
doubleend = 0
ab = 0
for item in s:
    if item[0] == "B" and item[-1] == "A":
        doubleend += 1
    else:
        if item[-1] == "A":
            aend += 1
        if item[0] == "B":
            bstart += 1
    ab += item.count("AB")

ab += max(doubleend - 1, 0)
n -= (doubleend - 1)
if bstart > 0:
    ab += 1
    bstart -= 1
    n -= 1
if aend > 0:
    ab += 1
    aend -= 1
    n -= 1
ab += min(aend, bstart, n-1) 
print(ab)

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

bstart, aend = 0, 0
doubleend = 0
ab = 0
for item in s:
    if item[0] == "B" and item[-1] == "A":
        doubleend += 1
    else:
        if item[-1] == "A":
            aend += 1
        if item[0] == "B":
            bstart += 1
    ab += item.count("AB")

ab += max(doubleend - 1, 0)
n -= (doubleend - 1)
if bstart > 0 and doubleend > 0:
    ab += 1
    bstart -= 1
    n -= 1
if aend > 0 and doubleend > 0:
    ab += 1
    aend -= 1
    n -= 1
ab += min(aend, bstart, n-1) 
print(ab)
Buggy code:
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

print(BA,B_,_A)

if BA > 0:
    print(BA + 1 + min(B_-1,_A-1,B_+_A-2) + inner)
else:
    print(min(B_,_A)+inner)

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

#print(BA,B_,_A)

if BA > 0:
    print(BA + 1 + min(B_-1,_A-1,B_+_A-2) + inner)
else:
    print(min(B_,_A)+inner)
Buggy code:
N = int(input())
S = [input() for _ in range(N)]

AB = 0
lastA = 0
firstB = 0
both = 0
for s in S:
  if s[-1] == 'A' and s[0] == 'B':
    both += 1
  else:
    if s[-1] == 'A':
      lastA += 1
    if s[0] == 'B':
      firstB += 1
  for i in range(len(s)-1):
    if s[i:i+2] == 'AB':
      AB += 1
x = min(lastA, firstB)
y = abs(lastA - firstB)
if y > 0:
  print(AB + min(N-1, x + both))
else:
  print(AB + min(N-1, x + both - 1))

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

AB = 0
lastA = 0
firstB = 0
both = 0
for s in S:
  if s[-1] == 'A' and s[0] == 'B':
    both += 1
  else:
    if s[-1] == 'A':
      lastA += 1
    if s[0] == 'B':
      firstB += 1
  for i in range(len(s)-1):
    if s[i:i+2] == 'AB':
      AB += 1
x = min(lastA, firstB)
y = abs(lastA - firstB)
if x >= both:
  print(AB + both * 2 + x - both)
else:
  print(AB + both-1 + int(lastA>0) + int(firstB>0) + max(0, x-1))
Buggy code:
n = int(input())

ab = 0
a = 0
b = 0
c = 0
for i in range(n):
    s = input()
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1
    if s[-1] == "A" and s[0] == "B":
        c += 1
    for i in range(len(s)-1):
        if s[i] == "A" and s[i+1] == "B":
            ab += 1
ans = ab + min(a,b)
if min(a,b) == c:
    ans = ab + min(a,b) - 1
print(ans)


Fixed code:
n = int(input())
ab = 0
a = 0
b = 0
c = 0
for i in range(n):
    s = input()
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1
    if s[-1] == "A" and s[0] == "B":
        c += 1
    for i in range(len(s)-1):
        if s[i] == "A" and s[i+1] == "B":
            ab += 1
ans = ab + min(a,b)
if a == b == c != 0:
    ans = ab + min(a,b) - 1
print(ans)
Buggy code:
from sys import stdin
N=int(stdin.readline().rstrip())
 
data=[]
ans=0
A=0
B=0
ab=0
for i in range(N):
    S=input()
    ans+=S.count("AB")
    if S[0] == "B":
        B+=1
    if S[-1] == "A":
        A+=1
    if S[-1] == "A" and S[0] == "B":
        ab+=1
 
print(ans+min(A,B)-ab)

Fixed code:
from sys import stdin
N=int(stdin.readline().rstrip())
 
data=[]
ans=0
A=0
B=0
ab=0
for i in range(N):
    S=input()
    ans+=S.count("AB")
    if S[0] == "B":
        B+=1
    if S[-1] == "A":
        A+=1
    if S[-1] == "A" and S[0] == "B":
        ab+=1
if ab == 0:
    print(ans+min(A,B))
    
elif A == B == ab:
    print(ans+ab-1)

else:
    print(ans+min(A,B))
Buggy code:
n=int(input())

ans=0
ba=0
bstart=0
aend=0
for i in range(n):
  s=input()
  for j in range(len(s)):
    if s[j]=="A":
      if not j+1==len(s):
        if s[j+1]=="B":
          ans+=1
  
  if s[0]=="B" and s[-1]=="A":
    ba+=1
  else:
    if s[0]=="B":
      bstart+=1
    if s[-1]=="A":
      aend+=1
      
print(ans,ba,bstart,aend)
      
if ba==0:
  print(ans+min(bstart,aend))
else:
  if bstart>=1 and aend>=1:
    print(ans+ba-1+2+min(bstart-1,aend-1))
  else:
    print(ans+ba-1+max(bstart,aend))
  

Fixed code:
n=int(input())

ans=0
ba=0
bstart=0
aend=0
for i in range(n):
  s=input()
  for j in range(len(s)):
    if s[j]=="A":
      if not j+1==len(s):
        if s[j+1]=="B":
          ans+=1
  
  if s[0]=="B" and s[-1]=="A":
    ba+=1
  else:
    if s[0]=="B":
      bstart+=1
    if s[-1]=="A":
      aend+=1
            
if ba==0:
  print(ans+min(bstart,aend))
else:
  if bstart>=1 and aend>=1:
    print(ans+ba-1+2+min(bstart-1,aend-1))
  else:
    print(ans+ba-1+max(min(1,bstart),min(1,aend)))
Buggy code:
N = int(input())
s = [input() for i in range(N)]
l1 = [s[i].count('AB') for i in range(N)]
l2 = [1 if s[i][0] == 'B' else 0 for i in range(N)]
l3 = [1 if s[i][-1] == 'A' else 0 for i in range(N)]
l4 = [l2[i]*l3[i] for i in range(N)]
if min(sum(l2),sum(l3)) == sum(l4):
    a = min(sum(l2),sum(l3))-1
else:
    a = min(sum(l2),sum(l3))
print(sum(l1) + a)

Fixed code:
N = int(input())
s = [input() for i in range(N)]
l1 = [s[i].count('AB') for i in range(N)]
l2 = [1 if s[i][0] == 'B' else 0 for i in range(N)]
l3 = [1 if s[i][-1] == 'A' else 0 for i in range(N)]
l4 = [l2[i]*l3[i] for i in range(N)]
if max(sum(l2),sum(l3)) == sum(l4):
    a = max(min(sum(l2),sum(l3))-1,0)
else:
    a = min(sum(l2),sum(l3))
print(sum(l1) + a)
Buggy code:
N=int(input())
a=0
b=0
ab=0
ans=0
for _ in range(N):
    s=input()
    ans+=s.count('AB')
    if s[0]=='B' and s[-1]=='A':
        ab+=1
    elif s[-1]=='A':
        a+=1
    elif s[0]=='B':
        b+=1
for _ in range(ab):
    if a>=b:
        b+=1
    else:
        a+=1
print(ans+min(a,b))


Fixed code:
N=int(input())
a=0
b=0
ab=0
ans=0
for _ 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
if ab==0:
    print(ans+min(a,b))
else:
    if a==0 and b==0:
        print(ans+ab-1)
    else:
        print(ans+min(a+ab,b+ab))
Buggy code:
xa = 0
bx = 0
ba = 0
ans = 0
N = int(input())
for _ in range(N):
    s = input()
    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

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


Fixed code:
xa = 0
bx = 0
ba = 0
ans = 0
N = int(input())

for _ in range(N):
    s = input()
    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

if xa == 0 and bx == 0 and ba != 0:
    print(ans+ba-1)
else:
    print(ans+min(xa,bx)+ba)
Buggy code:
n=int(input())
s=[input() for i in range(n)]
count=0
A=0
B=0
BA=0
for i in range(n):
    for j in range(len(s[i])-1):
        if j==0:
            if s[i][j]=='B':
                B+=1
        if j==len(s[i])-2:
            if s[i][j+1]=='A':
                A+=1
        if s[i][j:j+2]=='AB':
            count+=1
    if s[i][0]=='B' and s[i][-1]=='A':
        BA+=1
        A-=1
        B-=1
        
if BA==0:
    print(count+min(A,B))
else:
    ans=count+BA-1
    if A>=1 and B>=1:
        print(ans+2)
    elif A>=1 or B>=1:
        print(ans+1)
    else:
        print(ans)

Fixed code:
n=int(input())
s=[input() for i in range(n)]
count=0
A=0
B=0
BA=0
for i in range(n):
    for j in range(len(s[i])-1):
        if j==0:
            if s[i][j]=='B':
                B+=1
        if j==len(s[i])-2:
            if s[i][j+1]=='A':
                A+=1
        if s[i][j:j+2]=='AB':
            count+=1
    if s[i][0]=='B' and s[i][-1]=='A':
        BA+=1
        A-=1
        B-=1

if BA==0:
    print(count+min(A,B))
else:
    ans=count+BA-1
    if A>=1 and B>=1:
        print(ans+2+min(A,B)-1)
    elif A>=1 or B>=1:
        print(ans+1)
    else:
        print(ans)
Buggy code:
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

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


     

    


Fixed code:
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)
Buggy code:
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 23 13:27:43 2020

@author: NEC-PCuser
"""

N = int(input())

ans = 0
a_count = 0
b_count = 0
ab_count = 0
for i in range(N):
    s = input()
    if (s[0] == "B"):
        b_count += 1
    if (s[len(s) - 1] == "A"):
        a_count += 1
    if (s[0] == "B" and s[len(s) - 1] == "A"):
        ab_count += 1
    ans += s.count("AB")
    
    
plus = min([a_count, b_count])
    
if (plus == ab_count):
    plus -= 1

print(ans + plus)

Fixed code:
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 23 13:27:43 2020

@author: NEC-PCuser
"""

N = int(input())

ans = 0
a_count = 0
b_count = 0
ab_count = 0
for i in range(N):
    s = input()
    if (s[0] == "B" and s[len(s) - 1] == "A"):
        ab_count += 1
    else:
        if (s[0] == "B"):
            b_count += 1
        if (s[len(s) - 1] == "A"):
            a_count += 1
    
    ans += s.count("AB")
    
    
plus = min([a_count, b_count])
    
if (ab_count != 0 and a_count + b_count > 0):
    plus += ab_count
elif (ab_count != 0):
    plus += (ab_count - 1)

print(ans + plus)
Buggy code:
N = int(input())
numA = 0
numB = 0
cnt = 0
sm = 0

for i in range(N):
    mm = input()
    if mm[0]=="B":
        numB += 1
    if mm[-1]=="A":
        numA += 1
    if mm[0]=="B" and mm[-1]=="A":
        sm = 1

    for i in range(len(mm)):
        if mm[i:i+2]=="AB":
            cnt += 1

mm2 = min(numA-sm,numB-sm,N-1)
print(cnt+mm2)

Fixed code:
N = int(input())
numA = 0
numB = 0
cnt = 0
sm = 0

for i in range(N):
    mm = input()
    if mm[0]=="B":
        numB += 1
    if mm[-1]=="A":
        numA += 1
    if mm[0]=="B" and mm[-1]=="A":
        sm += 1

    for i in range(len(mm)):
        if mm[i:i+2]=="AB":
            cnt += 1

if sm == numA and sm==numB and sm>0:
    numA -= 1
    numB -= 1
mm2 = min(numA,numB,N-1)
print(cnt+mm2)
Buggy code:
n = int(input())
ans = 0
a = 0
b = 0
ba = 0
for i in range(n):
    s = input()
    ans += s.count("AB")
    if s[-1] == "A":
        if s[0] == "B":
            ba += 1
        a += 1
    if s[0] == "B":
        b += 1
if a > b:
    ans += b
elif b > a:
    ans += a
elif a == ba:
    ans += a-1
print(ans)

Fixed code:
n = int(input())
ans = 0
a = 0
b = 0
ba = 0
for i in range(n):
    s = input()
    ans += s.count("AB")
    if s[-1] == "A":
        if s[0] == "B":
            ba += 1
        a += 1
    if s[0] == "B":
        b += 1
if a > b:
    ans += b
elif b > a:
    ans += a
else:
    ans += a
    if a == ba and a > 0:
        ans -= 1
print(ans)
Buggy code:
#python 3.5.2

N = int(input())
s_list = [input() for i in range(N)]
cnt = sum([1 for s in s_list if "AB" in s])
a_end, b_start, a_b = 0, 0, 0
for s in s_list:
    if s[0] == "B" and s[-1] == "A":
        a_b += 1
    elif s[0] == "B":
        b_start += 1
    elif s[-1] == "A":
        a_end += 1
print(a_end, b_start, a_b)
print(min(a_end, b_start)+a_b+cnt)

Fixed code:
#python 3.5.2

N = int(input())
s_list = [input() for i in range(N)]
cnt = sum([s.count("AB") for s in s_list])
a_end, b_start, a_b = 0, 0, 0

for s in s_list:
    if s[0] == "B" and s[-1] == "A":
        a_b += 1
    elif s[0] == "B":
        b_start += 1
    elif s[-1] == "A":
        a_end += 1
if a_b == 0:
    cnt += min(a_end, b_start)
else:
    cnt += a_b - 1
    if a_end >= 1:
        cnt += 1
        a_end -= 1
    if b_start >= 1:
        cnt += 1
        b_start -= 1
    cnt += min(a_end, b_start)
print(cnt)
Buggy code:
N = int(input())
Sa = 0
Sb = 0
Sab = 0
ans = 0
for i in range(N):
    t = input()
    if t[-1] == 'A':
        if t[0] == 'B': Sab += 1
        else: Sa += 1
    elif t[0] == 'B': Sb += 1
    ans += t.count('AB')
ans += Sab-1
if Sab > 0:
    if Sa < Sb: Sa += 1
    else: Sb += 1
ans += min(Sa,Sb)
print(ans)

Fixed code:
N = int(input())
Sa = 0
Sb = 0
Sab = 0
ans = 0
for i in range(N):
    t = input()
    if t[-1] == 'A' and t[0] == 'B': Sab += 1
    elif t[-1] == 'A': Sa += 1
    elif t[0] == 'B': Sb += 1
    ans += t.count('AB')
if Sab > 0:
    ans += Sab-1
    if Sa > 0 and Sb > 0:
        ans += 2
        Sa -= 1
        Sb -= 1
    elif Sa < Sb: Sa += 1
    else: Sb += 1
ans += min(Sa,Sb)
print(ans)
Buggy code:
import sys


fin = sys.stdin.readline
N = int(fin())
s_list = [fin().rstrip("\n") for _ in range(N)]
num_AB_in_between = 0
s_Bhoge = 0
s_hogeA = 0
s_BA = 0
for s in s_list:
    if s[0] == "B" and s[-1] == "A":
        s_BA += 1
    elif s[0] == "B":
        s_Bhoge += 1
    elif s[-1] == "A":
        s_hogeA += 1

    for i, char in enumerate(s[:-1]):
        next_char = s[i + 1]
        if char == "A" and next_char == "B":
            num_AB_in_between += 1

# print(s_Bhoge, s_hogeA, s_BA)
if s_BA:
    num_AB_bound = 2 * s_BA - 2 + int(s_Bhoge > 0) + int(s_hogeA > 0)
    num_AB_bound += max(0, min(s_Bhoge - 1, s_hogeA - 1))
    # num_AB_bound += min(s_Bhoge - 1, s_hogeA - 1)
else:
    num_AB_bound = min(s_Bhoge, s_hogeA)
print(num_AB_in_between + num_AB_bound)


Fixed code:
import sys


fin = sys.stdin.readline
N = int(fin())
s_list = [fin().rstrip("\n") for _ in range(N)]
num_AB_in_between = 0
s_Bhoge = 0
s_hogeA = 0
s_BA = 0
for s in s_list:
    if s[0] == "B" and s[-1] == "A":
        s_BA += 1
    elif s[0] == "B":
        s_Bhoge += 1
    elif s[-1] == "A":
        s_hogeA += 1

    for i, char in enumerate(s[:-1]):
        next_char = s[i + 1]
        if char == "A" and next_char == "B":
            num_AB_in_between += 1

# print(s_Bhoge, s_hogeA, s_BA)
if s_BA:
    num_AB_bound = s_BA - 1 + int(s_Bhoge > 0) + int(s_hogeA > 0)
    num_AB_bound += max(0, min(s_Bhoge - 1, s_hogeA - 1))
    # num_AB_bound += min(s_Bhoge - 1, s_hogeA - 1)
else:
    num_AB_bound = min(s_Bhoge, s_hogeA)
print(num_AB_in_between + num_AB_bound)
Buggy code:
N = int(input())
s = []

result = 0
ABAB = 0
A = 0
B = 0

amari = 0
for i in range(N):
    s.append(input())

for S in s:
    AB = S.count("AB")
    result += AB
    if S[0] == "B" and S[-1] == "A":
        ABAB += 1
    elif S[0] == "B":
        B += 1
    else S[-1] == "A":
        A += 1


if A >= B:
    onon = B
    amari = A - B
else:
    onon = A
    amari = B - A

if ABAB >= 1:
    onon = onon + ABAB - 1

if amari >= 1:
    onon = onon + 1

print(result + onon)

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

result = 0
ABAB = 0
A = 0
B = 0
onon = 0

amari = 0
for i in range(N):
    s.append(input())

for S in s:
    AB = S.count("AB")
    result += AB
    if S[0] == "B" and S[-1] == "A":
        ABAB += 1
    elif S[0] == "B":
        B += 1
    elif S[-1] == "A":
        A += 1

if ABAB >= 1:
    onon = onon + ABAB - 1

if A >= 1 and ABAB >= 1 :
    onon += 1
    A -= 1
if B >= 1 and ABAB >= 1 :
    onon += 1
    B -= 1

if A >= 1 and B >= 1:
    if A >= B:
        onon += B
    else:
        onon += A


print(result + onon)
Buggy code:
n=int(input())
count=0
bx=0
xa=0
ba=0
for i in range(n):
    s=input()
    for j in range(0,len(s)-2):
        if s[j:j+2]=='AB':
            count=count+1
    if s[0]=='B' and s[len(s)-1]=='A':
        ba=ba+1
    if s[0]=='B' and not s[len(s)-1]=='A':
        bx=bx+1
    if not s[0]=='B' and s[len(s)-1]=='A':
        xa=xa+1
if bx*xa==0:
    if ba>=2:
        count=count+ba-1
else:
    count=count+ba+min(xa,bx)
print(count)
        


Fixed code:
n=int(input())
count=0
bx=0
xa=0
ba=0
for i in range(n):
    s=input()
    for j in range(0,len(s)-1):
        if s[j:j+2]=='AB':
            count=count+1
    if s[0]=='B' and s[len(s)-1]=='A':
        ba=ba+1
    if s[0]=='B' and not s[len(s)-1]=='A':
        bx=bx+1
    if not s[0]=='B' and s[len(s)-1]=='A':
        xa=xa+1
if bx==0 and xa==0:
    if ba>=2:
        count=count+ba-1
else:
    count=count+ba+min(xa,bx)
print(count)