ZhenlongDai/ACPR huggingface dataset:
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
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+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)

Failing test case input:
9
BEWPVCRWH
ZQNZYIJX
AVRAEB
AQ
HJMYITEOX
BCKHMRMNK
OA
ZBAGVQ
PRGKSPUNA

Expected output:
2


Actual output:
4


Fixed submission (verdict: Accepted):
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)

Problem: p03049
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
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:
    ans -= 1
print(ans)



Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
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)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
Ss=[input() for s in range(N)]
count = [s.count("AB") for s in Ss if "AB" in s]
headcount = len([s for s in Ss if s[0]=="B" and s[-1]!="A"])
outcount = len([s for s in Ss if s[-1]=="A" and s[0]!="B"])
multicount = len([s for s in Ss if s[0]=="B" and s[-1]=="A"])
print(sum(count)+min(headcount,outcount)+multicount)

Failing test case input:
3
BDAA
AAX[
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
N = int(input())
Ss=[input() for s in range(N)]
count = [s.count("AB") for s in Ss]
headcount = len([s for s in Ss if s[0]=="B" and s[-1]!="A"])
outcount = len([s for s in Ss if s[-1]=="A" and s[0]!="B"])
multicount = len([s for s in Ss if s[0]=="B" and s[-1]=="A"])
if outcount==0 and headcount==0:
    if multicount<=0:
        print(sum(count))
    else:
        print(sum(count)+multicount-1)
else:
    print(sum(count)+min(headcount,outcount)+multicount)
Problem: p03049
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n = int(input())
s = [input() for i in range(n)]
count,a,b,ab = 0,0,0,0
for word in s:
  while "AB" in word:
    count += 1
    word = word.replace("AB","")
  if word[0] == "B" and word[-1] =="A":
    ab += 1
  elif word[0] == "B":
    b += 1
  elif word[-1] == "A":
    a += 1
  else:
    pass
if a != b:
  ab += 1
print(min([a,b])+ab-1+count)

Failing test case input:
7
RABZBBF
JOX
VAUQHMB
BPB
RVI
MCM@BAOBHZ
RZMDJMA

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
n = int(input())
s = [input() for i in range(n)]
count,a,b,ab = 0,0,0,0
for word in s:
  count += word.count("AB")
  if word[0] == "B" and word[-1] =="A":
    ab += 1
  elif word[0] == "B":
    b += 1
  elif word[-1] == "A":
    a += 1
  else:
    pass
if ab == 0:
  print(min([a,b])+count)
else:
  if a == 0 and b == 0:
    print(ab-1+count)
  elif a == 0 or b == 0:
    print(ab+count)
  else:
    print(ab+1+min([a-1,b-1])+count)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
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()
ans=min(a,b)+c
dif=abs(a-b)
ans+=min(dif,d)
d-=min(dif,d)
print(ans+max(0,d-(a==b)))

Failing test case input:
3
AD@B
\XB@
DBC

Expected output:
0


Actual output:
-1


Fixed submission (verdict: Accepted):
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)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
r = 0
nBA = 0
nA = 0
nB = 0
for i in range(N):
  s = input()
  r += s.count("AB")
  if s[0] == "B" and s[-1] == "A":
    nBA += 1
  elif s[0] == "B":
    nB += 1
  elif s[-1] == "A":
    nA += 1
r += min(nA, nB)
if nA > 0 or nB > 0:
  r += nBA
else:
  r += nBA-1
print(r)


Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())
r = 0
nBA = 0
nA = 0
nB = 0
for i in range(N):
  s = input()
  r += s.count("AB")
  if s[0] == "B" and s[-1] == "A":
    nBA += 1
  elif s[0] == "B":
    nB += 1
  elif s[-1] == "A":
    nA += 1
r += min(nA, nB)
if nA > 0 or nB > 0:
  r += nBA
else:
  r += max(0, nBA-1)
print(r)
Problem: p03049
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
N = int(input())
topb = 0
taila = 0
both = 0
cnt = 0
for _ in range(N):
    s = input()
    cnt +=s.count("AB")

    if s[0] == "B" and s[-1] == "A":
        both += 1
    elif s[0] == "B":
        topb += 1
    elif s[-1] == "A":
        taila += 1

con_cnt_min,con_cnt_max = min(topb,taila),max(topb,taila)
use_both = min(both,con_cnt_min+con_cnt_max)
cnt += use_both
both -= use_both

print(cnt + con_cnt_min + both)



Failing test case input:
3
BDAA
AAX[
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
N = int(input())
topb = 0
taila = 0
both = 0
cnt = 0
for _ in range(N):
    s = input()
    cnt +=s.count("AB")

    if s[0] == "B" and s[-1] == "A":
        both += 1
    elif s[0] == "B":
        topb += 1
    elif s[-1] == "A":
        taila += 1
add_cnt = 0
if taila == 0 and topb == 0 and both >0:
    add_cnt = both -1
else:
    add_cnt = min(taila + both,topb + both)

print(cnt + add_cnt)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
word = []
ab = 0
a = 0
b = 0
mid = 0

for i in range(n):
    s = str(input())
    if s[0] == "B" and s[-1] == "A":
        ab += 1
    elif s[0] == "B":
        a += 1
    elif s[-1] == "A":
        b += 1
    for k in range(len(s) - 1):
        if s[k] == "A" and s[k + 1] == "B":
            mid += 1



mid += ab - 1
if a == 0 and b == 0:
    mid += 0
elif a == 0 or b == 0:
    mid += 1
else:
    mid += 2 + min(a-1, b-1)

print(mid)


Failing test case input:
3
AABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
n = int(input())
word = []
ab = 0
a = 0
b = 0
mid = 0

for i in range(n):
    s = str(input())
    if s[0] == "B" and s[-1] == "A":
        ab += 1
    elif s[0] == "B":
        a += 1
    elif s[-1] == "A":
        b += 1
    for k in range(len(s) - 1):
        if s[k] == "A" and s[k + 1] == "B":
            mid += 1


if ab > 0:
    mid += ab - 1
    if a == 0 and b == 0:
        mid += 0
    elif a == 0 or b == 0:
        mid += 1
    else:
        mid += 2 + min(a - 1, b - 1)

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

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
ans=A=B=0
for _ in range(N):
    S=input()
    ans+=S.count("AB")
    if S[0]=='B':B+=1
    if S[-1]=='A':A+=1
print(ans+(A-1 if A==B==N else min(A,B)))

Failing test case input:
3
BDAA
[AAX
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
N=int(input())
ans=A=B=BA=0
for _ in range(N):
    S=input()
    ans+=S.count("AB")
    if S[0]=='B'and S[-1]=='A':BA+=1
    elif S[0]=='B':B+=1
    elif S[-1]=='A':A+=1
if BA!=0:
    ans+= BA if A+B>0 else BA-1
print(ans+min(A,B))
Problem: p03049
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N = int(input())
ans = 0
both = 0
start_B = 0
end_A = 0
for _ in range(N):
    s = input()
    ans += s.count("AB")
    isB = s.startswith("B")
    isA = s.endswith("A")
    if isA and isB:
        both += 1
    if isA:
        end_A += 1
    if isB:
        start_B += 1
ans += min(start_B, end_A) - both
ans += both
if start_B == both and both == end_A:
    ans = max(0, ans - 1)
print(ans)

Failing test case input:
3
AABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())
ans = 0
both = 0
start_B = 0
end_A = 0
for _ in range(N):
    s = input()
    ans += s.count("AB")
    isB = s.startswith("B")
    isA = s.endswith("A")
    if isA and isB:
        both += 1
    if isA:
        end_A += 1
    if isB:
        start_B += 1
ans += min(start_B, end_A)
if start_B == both and both == end_A and start_B != 0:
    ans = max(0, ans - 1)
print(ans)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
start=0
end=0
cnt=0
for i in range(N):
    s=input()
    cnt+=s.count('AB')
    if s[0] == 'B':
        start+=1
    if s[-1] == 'A':
        end+=1
print(cnt+min(start,end,N-1))


Failing test case input:
3
BDAA
[AAX
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
N=int(input())
start=0
end=0
r=0
cnt=0
for i in range(N):
    s=input()
    cnt+=s.count('AB')
    if s[0] == 'B':
        start+=1
    if s[-1] == 'A':
        end+=1
    if s[0] == 'B' and s[-1] == 'A':
        r+=1
if start == end and start == r and end > 0:
    cnt -=1
print(cnt+min(start,end))

Problem: p03049
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import sys 

input()
bsn = 0
aen = 0
cen = 0
t = 0
for line in sys.stdin:
    st = line.strip()
    t += st.count("AB")
    if st[0] == "B" and st[-1] == "A":
        cen += 1
    elif st[0] == "B":
        bsn += 1
    elif st[-1] == "A":
        aen += 1

if aen != 0:
    if bsn != 0:
        if cen != 0:
            x = min(bsn-1, aen-1)
            print(t+cen+1+x)
        else:
            x = min(bsn, aen)
            print(t+x)
    else:
        print(t+cen)
else:
    if bsn != 0:
        print(t+cen)
    else:
        print(t+cen-1)




Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
import sys 
import re

input()
bsn = 0
aen = 0
cen = 0
t = 0
for line in sys.stdin:
    st = line.strip()
    t += st.count("AB")
    if st[0] == "B" and st[-1] == "A":
        cen += 1
    elif st[0] == "B":
        bsn += 1
    elif st[-1] == "A":
        aen += 1

if aen != 0:
    if bsn != 0:
        if cen != 0:
            x = min(bsn-1, aen-1)
            print(t+cen+1+x)
        else:
            x = min(bsn, aen)
            print(t+x)
    else:
        print(t+cen)
else:
    if bsn != 0:
        print(t+cen)
    else:
        if cen != 0:
            print(t+cen-1)
        else:
            print(t)
Problem: p03049
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
N=int(input())
numca=0
numbc=0
numba=0
ans=0
for i in range(N):
    s=input()
    for j in range(len(s)):
        if j==0 and s[j]=="B":
            numbc+=1
        elif j==len(s)-1 and s[j]=="A":
            numca+=1
        elif j!=len(s)-1 and s[j]=="A" and s[j+1]=="B":
            ans+=1
    if s[0]=="B" and s[-1]=="A":
        numbc-=1
        numca-=1
        numba+=1
if numba==0:
    print(ans+min(numbc,numca))
    quit()
ans+=numba-1
ans+=min(numbc+1,numca+1)
print(ans)


Failing test case input:
3
BDAA
[AAX
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
N=int(input())
numca=0
numbc=0
numba=0
ans=0
for i in range(N):
    s=input()
    for j in range(len(s)):
        if j==0 and s[j]=="B":
            numbc+=1
        elif j==len(s)-1 and s[j]=="A":
            numca+=1
        elif j!=len(s)-1 and s[j]=="A" and s[j+1]=="B":
            ans+=1
    if s[0]=="B" and s[-1]=="A":
        numbc-=1
        numca-=1
        numba+=1
if numba==0:
    print(ans+min(numbc,numca))
    quit()
ans+=numba-1
if numbc==0 and numca==0:
    print(ans)
    quit()
if numbc==0 or numca==0:
    print(ans+1)
    quit()
ans+=min(numbc+1,numca+1)
print(ans)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
A = 0
B = 0
ans = 0

S = list()

for _ in range(N):
    S.append(input())

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

if A == N:
    A -= 1
if B == N:
    B -= 1

if A > B:
    ans += B
else:
    ans += A

print(ans)


Failing test case input:
3
BDAA
[AAX
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
N = int(input())
A = 0
B = 0
AB = 0
ans = 0

S = list()

for _ in range(N):
    S.append(input())

for s in S:
    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:
    ans += min(A, B)
else:
    if A + B != 0:
        ans += AB + min(A, B)
    else:
        ans += AB - 1

print(ans)
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())

ans = 0
last_a_num = 0
first_b_num = 0
multi_ba_num = 0
for _ in range(N):
    s = input()
    ans += s.count('AB')
    if s[-1] == 'A' and s[0] == 'B':
        multi_ba_num += 1
    elif s[-1] == 'A':
        last_a_num += 1
    elif s[0] == 'B':
        first_b_num += 1

connect_num = multi_ba_num + min(last_a_num, first_b_num)
if last_a_num + first_b_num == 0:
    connect_num = multi_ba_num - 1
ans += connect_num

print(ans)

Failing test case input:
3
AD@B
\XB@
DBC

Expected output:
0


Actual output:
-1


Fixed submission (verdict: Accepted):
N = int(input())

ans = 0
last_a_num = 0
first_b_num = 0
multi_ba_num = 0
for _ in range(N):
    s = input()
    ans += s.count('AB')
    if s[-1] == 'A':
        last_a_num += 1
    if s[0] == 'B':
        first_b_num += 1
    if s[-1] == 'A' and s[0] == 'B':
        multi_ba_num += 1

connect_num = min(last_a_num, first_b_num)
if last_a_num == first_b_num and last_a_num == multi_ba_num:
    if connect_num > 0:
        connect_num -= 1
ans += connect_num

print(ans)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
B_start_cnt = 0
A_end_cnt = 0
AB_cnt = 0
BA_cnt = 0
for i in range(N):
    s = input()
    if s[0] == "B" and s[-1] == "A":
        BA_cnt += 1
    if s[0] == "B":
        B_start_cnt += 1
    if s[-1] == "A":
        A_end_cnt += 1
    AB_cnt += s.count('AB')
ans = min(A_end_cnt,B_start_cnt)+AB_cnt
if A_end_cnt == AB_cnt and B_start_cnt == AB_cnt:
    ans -= 1
print(ans)

Failing test case input:
3
BDAA
[AAX
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
N = int(input())
B_start_cnt = 0
A_end_cnt = 0
AB_cnt = 0
BA_cnt = 0
for i in range(N):
    s = input()
    if s[0] == "B" and s[-1] == "A":
        BA_cnt += 1
    if s[0] == "B" and s[-1] != "A":
        B_start_cnt += 1
    if s[-1] == "A" and s[0] != "B":
        A_end_cnt += 1
    AB_cnt += s.count('AB')
ans = AB_cnt

if BA_cnt == 0:
    ans += min(A_end_cnt,B_start_cnt)
elif max(A_end_cnt,B_start_cnt)>0:
    ans += BA_cnt+min(A_end_cnt,B_start_cnt)
else:
    ans += BA_cnt-1
print(ans)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
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:
	print(ans+min(a,b))
else:
    print(ans+min(a,b)-cnt+1)

Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
3


Fixed submission (verdict: Accepted):
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)
Problem: p03049
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
N=int(input())
s=''
point=0
q1,q2,q3=0,0,0 
for j in range(N):
    n=str(input())
    point+=n.count("AB")
    if n[0]=="B" and n[len(n)-1]=="A":
      q1+=1  
    elif n[0]=="B":
      q2+=1
    elif n[len(n)-1]=="A":
      q3+=1
    else:
      pass
q2-=1
q3-=1
print(q1-1+2+min(q3,q2)+point)

    


Failing test case input:
3
BDAA
AAX[
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
N=int(input())
s=''
point=0
q1,q2,q3=0,0,0 
for j in range(N):
    n=str(input())
    point+=n.count("AB")
    if n[0]=="B" and n[len(n)-1]=="A":
      q1+=1  
    elif n[0]=="B":
      q2+=1
    elif n[len(n)-1]=="A":
      q3+=1
    else:
      pass
if q1>=1:
  if q2==0 and q3==0:
    print(q1-1+point)
  elif q2==0 or q3==0:
    print(q1-1+1+point)
  else:
    print(q1-1+point+2+min(q2-1,q3-1))
else:
  print(min(q2,q3)+point)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):



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

m = 0
f = 0
b = 0
t = 0
for s in S:
    if s.startswith('B'):
        m += 1
    if s.endswith('A'):
        f += 1
    if s.startswith('B')*s.endswith('A'):
        m -= 1
        f -= 1
        b += 1
    t += s.count('AB')
ans = min(f,m)+b+t
if f == 0 and m == 0:
    ans -= 1
print(max(ans,0))

Failing test case input:
3
AABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):



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

m = 0
f = 0
b = 0
t = 0
for s in S:
    if s.startswith('B'):
        m += 1
    if s.endswith('A'):
        f += 1
    if s.startswith('B')*s.endswith('A'):
        m -= 1
        f -= 1
        b += 1
    t += s.count('AB')
if f == 0 and m == 0:
    b -= 1
ans = min(f,m)+max(b,0)+t

print(max(ans,0))

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
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))


Failing test case input:
3
AABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
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))
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S = [input() for _ in range(N)]

ans = cnt_a = cnt_b = 0
for s in S:
    ans += s.count("AB")
    if s[-1] == "A":
        cnt_a += 1
    if s[0] == "B":
        cnt_b += 1
ans += min(cnt_a, cnt_b, N - 1)

print(ans)


Failing test case input:
3
BDAA
AAX[
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
N = int(input())
S = [input() for _ in range(N)]

ans = cnt_b = cnt_a = cnt_ba = 0
for s in S:
    ans += s.count("AB")
    if s[0] == "B":
        cnt_b += 1
    if s[-1] == "A":
        cnt_a += 1
    if s[0] == "B" and s[-1] == "A":
        cnt_ba += 1

if cnt_a == cnt_ba and cnt_b == cnt_ba:
    cnt_a -= 1
    cnt_b -= 1
ans += max(min(cnt_a, cnt_b, N - 1), 0)

print(ans)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
s = [list(input()) for i in range(n)]
a,b,c = 0,0,0
t = 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":
                        c += 1
        if s[i][0] == "B":
                b += 1
        if s[i][-1] == "A":
                a += 1
        if s[i][0] == "B" and s[i][-1] == "A":
                t += 1

if a > b:
        d = b
else:
        d = a

if t == a:
        d -= 1

print(d + c)

Failing test case input:
7
RABZBBF
JPX
BMHQUAV
BPC
VRI
MCN@BAOBIZ
AMZDMJR

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
n = int(input())
s = [list(input()) for i in range(n)]
a,b,c = 0,0,0
t = 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":
                        c += 1
                else:
                        continue
        if s[i][0] == "B":
                b += 1
        if s[i][-1] == "A":
                a += 1
        if s[i][0] == "B" and s[i][-1] == "A":
                t += 1
 
if a > b:
        d = b
else:
        d = a

if  t == b and a == t:
        d -= 1
if  t == 0 and a == 0 and b ==0:
        d += 1

print(d + c)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
start=0
end=0
cnt=0
for i in range(N):
    s=input()
    cnt+=s.count('AB')
    if s[0] == 'B':
        start+=1
    if s[-1] == 'A':
        end+=1
print(cnt+min(start,end))


Failing test case input:
3
BDAA
[AAX
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
N=int(input())
start=0
end=0
r=0
cnt=0
for i in range(N):
    s=input()
    cnt+=s.count('AB')
    if s[0] == 'B':
        start+=1
    if s[-1] == 'A':
        end+=1
    if s[0] == 'B' and s[-1] == 'A':
        r+=1
if start == end and start == r and end > 0:
    cnt -=1
print(cnt+min(start,end))
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
Ss=[input() for s in range(N)]
count = [s.count("AB") for s in Ss if "AB" in s]
headcount = len([s for s in Ss if s[0]=="B" and s[-1]!="A"])
outcount = len([s for s in Ss if s[-1]=="A" and s[0]!="B"])
multicount = len([s for s in Ss if s[0]=="B" and s[-1]=="A"])
if outcount==0 and headcount==0:
    print(sum(count)+multicount-1)
else:
    print(sum(count)+min(headcount,outcount)+multicount)

Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())
Ss=[input() for s in range(N)]
count = [s.count("AB") for s in Ss]
headcount = len([s for s in Ss if s[0]=="B" and s[-1]!="A"])
outcount = len([s for s in Ss if s[-1]=="A" and s[0]!="B"])
multicount = len([s for s in Ss if s[0]=="B" and s[-1]=="A"])
if outcount==0 and headcount==0:
    if multicount<=0:
        print(sum(count))
    else:
        print(sum(count)+multicount-1)
else:
    print(sum(count)+min(headcount,outcount)+multicount)

Problem: p03049
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n = int(input())

cnt_a = 0   
cnt_b = 0   
cnt_ba = 0  

ans = 0
for _ in range(n):
    s = input()
    ans += s.count('AB')
    if s.endswith('A') and s.startswith('B'):
        cnt_ba += 1
    else:
        if s.endswith('A'):
            cnt_a += 1
        if s.startswith('B'):
            cnt_b += 1

if cnt_a == 0:
    ans += cnt_ba - 1
    if cnt_b > 0:
        ans += 1
else:
    ans += cnt_ba
    cnt_a -= 1
    if cnt_b > 0:
        ans += 1
        cnt_b -= 1
    
    ans += min(cnt_a, cnt_b)

print(ans)

Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
n = int(input())

cnt_a = 0   
cnt_b = 0   
cnt_ba = 0  

ans = 0
for _ in range(n):
    s = input()
    ans += s.count('AB')
    if s.endswith('A') and s.startswith('B'):
        cnt_ba += 1
    else:
        if s.endswith('A'):
            cnt_a += 1
        if s.startswith('B'):
            cnt_b += 1

if cnt_ba == 0:
    ans += min(cnt_a, cnt_b)
else:
    ans += cnt_ba-1
    if cnt_a > 0:
        cnt_a -= 1
        ans += 1
    if cnt_b > 0:
        cnt_b -= 1
        ans += 1
    
    ans += min(cnt_a, cnt_b)

print(ans)

Problem: p03049
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n = int(input())
cnt_a = cnt_b = 0
ans = 0
for _ in range(n):
    s = input()
    ans += s.count('AB')
    if s[0] == 'B':
        cnt_b += 1
    if s[-1] == 'A':
        cnt_a += 1


if cnt_a == cnt_b == n:
    ans += n - 1
else:
    ans += min(cnt_a, cnt_b)
print(ans)


Failing test case input:
3
BDAA
AAX[
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
n = int(input())
cnt_a = cnt_b = cnt_ba = 0
ans = 0
for _ in range(n):
    s = input()
    ans += s.count('AB')
    if s[0] == 'B':
        cnt_b += 1
        if s[-1] == 'A':
            cnt_ba += 1
    if s[-1] == 'A':
        cnt_a += 1


if cnt_a == cnt_b == n:
    ans += n - 1
elif cnt_ba > 0 and cnt_a == cnt_b == cnt_ba:
    ans += cnt_ba - 1
else:
    ans += min(cnt_a, cnt_b)
print(ans)
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
a=0
B=0
A=0
count1=0
count2=0
for i in range(N):
    S=input()
    a+=S.count("AB")
    if S[0]=="B"and S[len(S)-1]=="A":
        a+=1
        count1+=1
    elif S[0]=="B":
        B+=1
        count2+=1
    elif S[0]=="A":
        A+=1
        count2+=1
if count1!=0:
    A+=1
    B+=1
if count2==0:
    a=a-1
C=min(A,B)
D=min(C,N-1)
print(a+D)

Failing test case input:
9
BEWPVCRWH
ZQNZYIJX
AERVAB
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Expected output:
4


Actual output:
3


Fixed submission (verdict: Accepted):
N=int(input())
a=0
b=0
B=0
A=0
count1=0
count2=0
for i in range(N):
    S=input()
    a+=S.count("AB")
    if S[0]=="B"and S[len(S)-1]=="A":
        count1+=1
        if count1==1:
            A+=1
            B+=1
        else:
            b+=1
    elif S[0]=="B":
        B+=1
        count2+=1
    elif S[len(S)-1]=="A":
        A+=1
        count2+=1
if count1>1 and count2==0:
    b=b-1
C=min(A+b,B+b)
print(a+C)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
a = 0
b = 0
ans = 0
for i in range(n):
  s = input()
  ans += s.count("AB")
  a += s[-1] == "A"
  b += s[0] == "B"
ans += min(n-1,a,b)
print(ans)


Failing test case input:
3
BDAA
[AAX
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
n = int(input())
a = 0
b = 0
x = 0
ans = 0
for i in range(n):
  s = input()
  ans += s.count("AB")
  a += s[-1] == "A"
  b += s[0] == "B"
  x += (s[-1] == "A") and (s[0] == "B")
if a == x == b != 0:
  ans += x-1
else:
  ans += min(n-1,a,b)
print(ans)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
s_list = [input() for _ in range(N)]

ans = 0
start_B = 0
end_A = 0
for s in s_list:
  ans += s.count("AB")
  if s[0] == "B":
    start_B += 1
  if s[-1] == "A":
    end_A += 1

ans += min(start_B, end_A)
print(ans)

Failing test case input:
3
BDAA
AAX[
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
N = int(input())
s_list = [input() for _ in range(N)]

ans = 0
start_B = 0
end_A = 0
B_A = 0
for s in s_list:
  ans += s.count("AB")
  if s[0] == "B" and s[-1] == "A":
    B_A += 1
  elif s[0] == "B":
    start_B += 1
  elif s[-1] == "A":
    end_A += 1

if B_A > 0 and start_B + end_A == 0:
  ans += B_A - 1
else:
  ans += min(start_B, end_A) + B_A

print(ans)
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from sys import stdin










N = int(stdin.readline().rstrip().split()[0])

_A = 0
AB = 0
B_ = 0
B_A = 0
for _ in range(N):
  s = stdin.readline().rstrip().split()[0]
  AB += s.count("AB")
  a=0
  b=0
  if s[0] == "B":
    b = 1
  if s[-1] == "A":
    a = 1
  if a == 1 and b == 1:
    B_A += 1
  _A += a
  B_ += b

if _A == B_ == B_A:
  print(AB+B_A-1)
else:
  _AB_ = min(_A, B_)
  print(AB+_AB_)



Failing test case input:
3
AD@B
\XB@
DBC

Expected output:
0


Actual output:
-1


Fixed submission (verdict: Accepted):
from sys import stdin










N = int(stdin.readline().rstrip().split()[0])

_A = 0
AB = 0
B_ = 0
B_A = 0
for _ in range(N):
  s = stdin.readline().rstrip().split()[0]
  AB += s.count("AB")
  a=0
  b=0
  if s[0] == "B":
    b = 1
  if s[-1] == "A":
    a = 1
  if a == 1 and b == 1:
    B_A += 1
  _A += a
  B_ += b

if _A == B_ == B_A == 0:
  print(AB)
elif _A == B_ == B_A:
  print(max(0, AB+B_A-1))
else:
  _AB_ = min(_A, B_)
  print(AB+_AB_)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n=int(input())
ans=0
a=0
b=0
flg=1
for _ in range(n):
  s=input()
  for i in range(len(s)-1):
    if s[i:i+2]=="AB":
      ans+=1
  if s[0]=="B":
    b+=1
    if s[-1]!="A":
      flg=0
  if s[-1]=="A":
    a+=1
    if s[0]!="B":
      flg=0
ans+=min(a,b)
print(ans)


Failing test case input:
3
BDAA
AAX[
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
n=int(input())
ans=0
a=0
b=0
flg=1
for _ in range(n):
  s=input()
  for i in range(len(s)-1):
    if s[i:i+2]=="AB":
      ans+=1
  if s[0]=="B":
    b+=1
    if s[-1]!="A":
      flg=0
  if s[-1]=="A":
    a+=1
    if s[0]!="B":
      flg=0
if flg:
  ans+=max(0,min(a,b)-1)
else:
  ans+=min(a,b)
print(ans)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
s = []
ca = 0
cb = 0
cab = 0
for i in range(n):
    tmp = input()
    s.append(tmp)
    if tmp[0] == "B" and tmp[-1] == "A":
        cab += 1
    elif tmp[-1] == "A":
        ca += 1
    elif tmp[0] == "B":
        cb += 1
cnt = 0
for ss in s:
    cnt += ss.count("AB")
if ca == cb == 0 and cab > 0:
    cnt -= 1
print(cnt + min(ca, cb))


Failing test case input:
7
FBBYBAR
JOZ
AVUQHMB
BPA
ISV
MCMABAOBHZ
SZMEIMA

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
n = int(input())
cnt = 0
ca = 0
cb = 0
cab = 0
for i in range(n):
    tmp = input()
    cnt += tmp.count("AB")
    if tmp[0] == "B" and tmp[-1] == "A":
        cab += 1
    elif tmp[0] == "B" and tmp[-1] != "A":
        cb += 1
    elif tmp[0] != "B" and tmp[-1] == "A" :
        ca += 1
if ca == cb == 0 and cab > 0:
    cnt -= 1
print(cnt + cab + min(ca, cb))
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
sl = [input() for _ in range(n)]
sb,la,cnt,both = 0,0,0,0
for s in sl:
  cnt += s.count("AB")
  if s[0] == "B":
    sb += 1
  if s[-1] == "A":
    la += 1
  if s[0] + s[-1] == "BA":
    both += 1
if sb == both or la == n:
  cnt -= 1
print(cnt + min(la,sb))

Failing test case input:
7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Expected output:
4

Actual output:
3


Fixed submission (verdict: Accepted):
n = int(input())
sl = [input() for _ in range(n)]
sb,la,cnt,both = 0,0,0,0
for s in sl:
  cnt += s.count("AB")
  if s[0] + s[-1] == "BA":
    both += 1
  elif s[0] == "B":
    sb += 1
  elif s[-1] == "A":
    la += 1
if both:
  cnt += both-1
  if la:
    cnt += 1
    la -= 1
  if sb:
    cnt += 1
    sb -= 1
print(cnt + min(la,sb))

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S = [input() for _ in range(N)]
Alist = [word for word in S if word[-1] == 'A']
Blist = [word for word in S if word[0] == 'B']
ABlist = [word for word in S if word[0] == 'B' and word[-1] == 'A']
Slist = [word.count('AB') for word in S]
C = min(len(Alist),len(Blist))
if len(ABlist) == len(Alist) and len(ABlist) == len(Blist):
    print(sum(Slist)+len(ABlist)-1)
else:
    print(sum(Slist)+min(C,N-1))


Failing test case input:
3
AD@B
\XB@
DBC

Expected output:
0


Actual output:
-1


Fixed submission (verdict: Accepted):
N = int(input())
S = [input() for _ in range(N)]
Alist = [word for word in S if word[-1] == 'A']
Blist = [word for word in S if word[0] == 'B']
ABlist = [word for word in S if word[0] == 'B' and word[-1] == 'A']
Slist = [word.count('AB') for word in S]
C = min(len(Alist),len(Blist))
if len(Alist) == 0 and len(Blist) == 0:
    print(sum(Slist))
elif len(ABlist) == len(Alist) and len(ABlist) == len(Blist):
    print(sum(Slist)+len(ABlist)-1)
else:
    print(sum(Slist)+min(C,N-1))

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())

AB_count = 0
A_end = 0
B_start = 0
B_start_and_A_end = 0
for i in range(N):
    string = input()
    AB_count += string.count("AB")
    
    if string[0] == "B":
        B_start += 1
    if string[-1] == "A":
        A_end += 1
    if string[0] == "B" and string[-1] == "A":
        B_start_and_A_end += 1
    
new_AB = min(A_end,B_start)
if B_start_and_A_end == A_end and B_start_and_A_end == B_start:
    new_AB -= 1

print(AB_count+new_AB)

Failing test case input:
3
AD@B
\XB@
DBC

Expected output:
0


Actual output:
-1


Fixed submission (verdict: Accepted):
N = int(input())

AB_count = 0
A_end = 0
B_start = 0
B_start_and_A_end = 0
for i in range(N):
    string = input()
    AB_count += string.count("AB")
    
    if string[0] == "B":
        B_start += 1
    if string[-1] == "A":
        A_end += 1
    if string[0] == "B" and string[-1] == "A":
        B_start_and_A_end += 1
    
new_AB = min(A_end,B_start)
if B_start_and_A_end == A_end and B_start_and_A_end == B_start and B_start_and_A_end != 0:
    new_AB -= 1

print(AB_count+new_AB)
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
ans = 0
first_b = 0
last_a = 0
same = 0

N = int(input())

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

    if s[0] == "B" and s[-1] == "A":
        same +=1

ans += min(first_b, last_a)
if first_b == last_a  and first_b == same:
    ans -=1
print(ans)

Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
ans = 0
first_b = 0
last_a = 0
same = 0

N = int(input())

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

ans += min(first_b, last_a)
if same > 0 and first_b ==  last_a and first_b == same:
    ans -=1
print(ans)

Problem: p03049
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import sys
from operator import itemgetter
from collections import deque
sys.setrecursionlimit(10*5)

ans=0
p,q,r=0,0,0
n=int(input())
s=[]
for i in range(n):
    a=input()
    st=a[0]
    ed=a[-1]
    ans+=a.count('AB')
    if st=='B' and ed=='A':
        p+=1
    elif ed=='A':
        q+=1
    elif st=='B':
        r+=1
if q>0 and r>0:
    ans+=p+1+min(q-1,r-1)
elif q==0:
    ans+=p-1 +(1 if r>0 else 0)
elif r==0:
    ans+=p-1 +(1 if q>0 else 0)
    
print(ans)

Failing test case input:
3
AD@B
\XB@
DBC

Expected output:
0


Actual output:
-1


Fixed submission (verdict: Accepted):
import sys
from operator import itemgetter
from collections import deque
sys.setrecursionlimit(10*5)

ans=0
p,q,r=0,0,0
n=int(input())
s=[]
for i in range(n):
    a=input()
    st=a[0]
    ed=a[-1]
    ans+=a.count('AB')
    if st=='B' and ed=='A':
        p+=1
    elif ed=='A':
        q+=1
    elif st=='B':
        r+=1
if q>0 and r>0:
    ans+=max(0, p+1+min(q-1,r-1) )
elif q==0:
    ans+=max(0, p-1 +(1 if r>0 else 0) )
elif r==0:
    ans+=max(0, p-1 +(1 if q>0 else 0) )
    
print(ans)
Problem: p03049
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

N=one_int()

strs=[]
for i in range(N):
    strs.append(one_str())

count = 0
head_B=0
tail_A=0
dual_num=0
for s in strs:
    count +=s.count("AB")
    head_B += 0 if s[0]!="B" else 1
    tail_A += 0 if s[-1]!="A" else 1 
    
    if s[0]=="B" and s[-1]=="A":
        dual_num+=1
    

if dual_num==head_B==tail_A:
    print(min(head_B, tail_A) -1 +count)
else:
    print(min(head_B, tail_A)+count)

Failing test case input:
3
AABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

N=one_int()

strs=[]
for i in range(N):
    strs.append(one_str())

count = 0
head_B=0
tail_A=0
dual_num=0
for s in strs:
    count +=s.count("AB")
    head_B += 0 if s[0]!="B" else 1
    tail_A += 0 if s[-1]!="A" else 1 
    
    if s[0]=="B" and s[-1]=="A":
        dual_num+=1
    

if N==1:
    print(count)
elif dual_num==head_B==tail_A and head_B>=1:
    print(min(head_B, tail_A) -1 +count)
else:
    print( min(head_B, tail_A)+count)

Problem: p03049
Buggy submission (Python (3.8.2), verdict: Wrong Answer):



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)

Failing test case input:
3
ABCA
XBAZ
BEA

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):



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)
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys

readline = sys.stdin.readline


def main():
    N = int(readline())
    cnt = 0; leftB = 0; rightA = 0
    for _ in range(N):
        s = readline()[:-1]
        for i in range(len(s)-1):
            subs = s[i:i+2]
            cnt += subs == 'AB'
        leftB += s[0] == 'B'
        rightA += s[-1] == 'A'
    
    ans = cnt + min(N-1, leftB, rightA)
    print(ans)

if __name__ == "__main__":
    main()


Failing test case input:
3
BDAA
[AAX
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
import sys

readline = sys.stdin.readline


def main():
    N = int(readline())
    cnt = 0; leftB = 0; rightA = 0; same = 0
    for _ in range(N):
        s = readline()[:-1]
        for i in range(len(s)-1):
            subs = s[i:i+2]
            cnt += subs == 'AB'
        leftB += s[0] == 'B'
        rightA += s[-1] == 'A'
        same += s[0] == 'B' and s[-1] == 'A'
    
    if same == leftB == rightA:
        ans = cnt + max(min(N-1, same-1), 0)
    else:
        ans = cnt + min(N-1, leftB, rightA)
    print(ans)

if __name__ == "__main__":
    main()

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n=int(input())
s=[]
ans=0
a=0
b=0
ab=0
for i in range(n):
	x=input()
	s.append(x)
	for j in range(len(x)-1):
		if x[j]=="A" and x[j+1]=="B":
			ans+=1
	if x[0]=="B" and x[-1]=="A":
		ab+=1
	elif x[0]=="B":
		b+=1
	elif x[-1]=="A":
		a+=1
if a<b:
	ab-=(b-a)
	a=b
elif a>b:
	ab-=(a-b)
	b=a
print(ans+min(n-1,ab+a))

Failing test case input:
3
BDAA
AAX[
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
n=int(input())
s=[]
ans=0
a=0
b=0
ab=0
for i in range(n):
	x=input()
	s.append(x)
	for j in range(len(x)-1):
		if x[j]=="A" and x[j+1]=="B":
			ans+=1
	if x[0]=="B" and x[-1]=="A":
		ab+=1
	elif x[0]=="B":
		b+=1
	elif x[-1]=="A":
		a+=1
if a==b==0:
	print(ans+max(ab-1,0))
else:
	print(ans+max(0,ab+min(a,b)))

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
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=BA-1
if B>0:
  ans+=1
  B-=1
if A>0:
  ans+=1
  A-=1
ans+=min(A,B)
print(ans+cnt)

Failing test case input:
3
AABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
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)
Problem: p03049
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
N = int(input())
cnt, A, B = 0, 0, 0
for _ in range(N):
    S = input().rstrip()
    flg = False
    for s in S:
        if s=="A":
            flg=True
        elif flg==True and s=="B":
            cnt += 1
        else: flg = False
    if S[0]=="B": B+=1
    if S[-1]=="A": A+=1
if A==B==N: print(cnt+A-1)
else: print(cnt+min(A,B))

Failing test case input:
3
BDAA
AAX[
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
N = int(input())
cnt, A, B, C = 0, 0, 0, 0
for _ in range(N):
    S = input().rstrip()
    flg = False
    for s in S:
        if s=="A":
            flg=True
        elif flg==True and s=="B":
            cnt += 1
            flg = False
        else: flg = False
    if S[0]=="B": B+=1
    if S[-1]=="A": A+=1
    if S[0]=="B" and S[-1]=="A": C+=1
if A==B==C and A!=0: print(cnt+A-1)
else: print(cnt+min(A,B))

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
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 == banum:
        print(ct + min(_anum, b_num) + (banum - 1))
    else:
        print(ct + min(_anum, b_num) + banum)
else:
    print(ct + min(_anum, b_num))

Failing test case input:
3
ABCA
ZABX
BEA

Expected output:
3


Actual output:
2


Fixed submission (verdict: Accepted):
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))
Problem: p03049
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
N = int(input())
count_A_right = 0
count_B_left  = 0
AB_count = 0
same_count = 0

for _ in range (N):
    S = input()
    temp = 0
    if S[0] == 'B':
        count_B_left +=1
        temp +=1
    if S[-1] == 'A':
        count_A_right +=1
        temp +=1
    if temp ==2:
        same_count+=1
        count_A_right -=1
        count_B_left  -=1
    for i in range (len(S)-1):
        if S[i:i+2] == 'AB':
            AB_count +=1

AB_count += same_count-1
count_A_right +=1
count_B_left  +=1
if count_A_right==1 and count_B_left ==1:
    print(AB_count)
else:
    print(AB_count+min(count_A_right,count_B_left))

Failing test case input:
3
AD@B
\XB@
DBC

Expected output:
0


Actual output:
-1


Fixed submission (verdict: Accepted):
N = int(input())
count_A_right = 0
count_B_left  = 0
AB_count = 0
same_count = 0

for _ in range (N):
    S = input()
    temp = 0
    if S[0] == 'B':
        count_B_left +=1
        temp +=1
    if S[-1] == 'A':
        count_A_right +=1
        temp +=1
    if temp ==2:
        same_count+=1
        count_A_right -=1
        count_B_left  -=1
    for i in range (len(S)-1):
        if S[i:i+2] == 'AB':
            AB_count +=1

if same_count-1>=0:
    AB_count += same_count-1
    count_A_right +=1
    count_B_left  +=1
    if count_A_right==1 and count_B_left ==1:
        print(AB_count)
    else:
        print(AB_count+min(count_A_right,count_B_left))
else:
    print(AB_count+min(count_A_right,count_B_left))

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())

a = 0
b = 0
c = 0
ans = 0

for _ in range(N):
    s = input()
    flag = False
    for i in range(len(s)):
        if (flag == True) and (s[i] == "B"):
            ans += 1
            flag = False
        elif (flag == False) and (s[i] == "A"):
            flag = True
        elif (flag == True):
            flag = False
    if (s[0] == "B" and s[-1] == "A"):
        c += 1
    elif (s[0] == "B"):
        b += 1
    elif (s[-1] == "A"):
        a += 1
if c > 0:
    ans += c - 1
    c = 1
    if (a > 0 or b > 0):
        ans += 1
print(ans + min(a, b))

Failing test case input:
3
AABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())

a = 0
b = 0
c = 0
ans = 0

for _ in range(N):
    s = input()
    flag = False
    for i in range(len(s)):
        if (flag == True) and (s[i] == "B"):
            ans += 1
            flag = False
        elif (s[i] == "A"):
            flag = True
        else:
            flag = False
    if (s[0] == "B" and s[-1] == "A"):
        c += 1
    elif (s[0] == "B"):
        b += 1
    elif (s[-1] == "A"):
        a += 1
if c > 0:
    ans += c - 1
    c = 1
    if (a > 0 or b > 0):
        ans += 1
print(ans + min(a, b))