ZhenlongDai/ACPR huggingface dataset:
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys
input = sys.stdin.readline

n = int(input())
a = 0
b = 0
ba = 0
ans = 0

for i in range(n):
  s = input().strip()
  leng = len(s)
  if s[0] == 'B' and s[-1] == 'A':
    ba += 1
    a += 1
    b += 1
  elif s[-1] == 'A':
    a += 1
  elif s[0] == 'B':
    b += 1
  for j in range(leng - 1):
    if s[j] == 'A' and s[j+1] == 'B':
      ans += 1

if ba == b == a:
  ans += min(b,a) - 1
else:
  ans += min(b,a)
print(ans)

Failing test case input:
3
AABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
import sys
input = sys.stdin.readline

n = int(input())
a = 0
b = 0
ba = 0
ans = 0

for i in range(n):
  s = input().strip()
  leng = len(s)
  if s[0] == 'B' and s[-1] == 'A':
    ba += 1
    
    
  elif s[-1] == 'A':
    a += 1
  elif s[0] == 'B':
    b += 1
  for j in range(leng - 1):
    if s[j] == 'A' and s[j+1] == 'B':
      ans += 1

if ba == 0:
  ans += min(a,b)
elif ba > 0:
  if a+b > 0:
    ans += ba + min(a,b)
  else:
    ans += ba - 1
print(ans)

Problem: p03049
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n = int(input())
s = [input() for _ in range(n)]

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

a -= b_a
b -= b_a
print(ab + min(a, b) + b_a)

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)]

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

a -= b_a
b -= b_a
if a == 0 and b == 0:
  print(ab + max(b_a-1, 0))
else:
  print(ab + min(a, b) + b_a)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
s = [input() for _ in range(N)]
a = 0
b = 0
c = 0
d = 0
for i in range(N):
    d += s[i].count("AB")
    if s[i][0] != 'B' and s[i][-1] == 'A':
        a += 1
    if s[i][0] == 'B' and s[i][-1] != 'A':
        b += 1
    if s[i][0] == 'B' and s[i][-1] == 'A':
        c += 1
if a == 0 and b == 0:
    print(d+c-1)
else:
    print(d+min(a+c,b+c))

Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())
s = [input() for _ in range(N)]
a = 0
b = 0
c = 0
d = 0
for i in range(N):
    d += s[i].count("AB")
    if s[i][0] != 'B' and s[i][-1] == 'A':
        a += 1
    if s[i][0] == 'B' and s[i][-1] != 'A':
        b += 1
    if s[i][0] == 'B' and s[i][-1] == 'A':
        c += 1
if a == 0 and b == 0:
    if c == 0:
        print(d)
    else:
        print(d+c-1)
else:
    print(d+min(a+c,b+c))
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
s = []
N = int(input())
for i in range(N):
    s.append(input())

count_A = 0
count_B = 0
count_AB = 0
count = 0

for i in range(N):
    flag = False
    for j in range(len(s[i])):
        if j == 0 and s[i][j] == "B":
            count_B += 1
            flag = True
        if s[i][j] == "A":
            if j == len(s[i]) - 1:
                count_A += 1
                if flag:
                    count_A -= 1
                    count_B -= 1
                    count_AB += 1
            elif s[i][j+1] == "B":
                count += 1

if count_A == 0 and count_B == 0:
    count += count_AB - 1

else:
    if count_A <= count_B:
        count += count_A
    else:
        count += count_B

    count += count_AB

print(count)

Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
s = []
N = int(input())
for i in range(N):
    s.append(input())

count_A = 0
count_B = 0
count_AB = 0
count = 0

for i in range(N):
    flag = False
    for j in range(len(s[i])):
        if j == 0 and s[i][j] == "B":
            count_B += 1
            flag = True
        if s[i][j] == "A":
            if j == len(s[i]) - 1:
                count_A += 1
                if flag:
                    count_A -= 1
                    count_B -= 1
                    count_AB += 1
            elif s[i][j+1] == "B":
                count += 1

if count_A == 0 and count_B == 0:
    if count_AB > 0:
        count += count_AB - 1

else:
    if count_A <= count_B:
        count += count_A
    else:
        count += count_B

    count += count_AB

print(count)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    cunt_a=0
    cunt_b=0
    cunt_ba=0
    cunt_ab=0
    for i in range(n):
        s=input()
        if s[0]=='B' and s[-1]=='A':
            cunt_ba+=1
        elif s[0]=='B': 
            cunt_b+=1
        elif s[-1]=='A':
            cunt_a+=1
        for j in range(len(s)-1):
            if s[j:j+2]=="AB":
                cunt_ab+=1
    print(cunt_ab+cunt_ba+min(cunt_a,cunt_b))

                
if __name__=='__main__':
    main()

Failing test case input:
3
BDAA
AAX[
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    cunt_a=0
    cunt_b=0
    cunt_ba=0
    cunt_ab=0
    for i in range(n):
        s=input()
        if s[0]=='B' and s[-1]=='A':
            cunt_ba+=1
        elif s[0]=='B': 
            cunt_b+=1
        elif s[-1]=='A':
            cunt_a+=1
        for j in range(len(s)-1):
            if s[j:j+2]=="AB":
                cunt_ab+=1
    if cunt_a==0 and cunt_b==0 and cunt_ba>0:
        print(cunt_ab+cunt_ba-1)
    else:
        print(cunt_ab+cunt_ba+min(cunt_a,cunt_b))
    
if __name__=='__main__':
    main()
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from sys import exit

N = int(input())

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

count = 0
a_count = 0
b_count = 0
a_b_count = 0

for s in S:
	if s[0] == 'B' and not s[-1] == 'A':
		b_count += 1
	if not s[0] == 'B' and s[-1] == 'A':
		a_count += 1
	if s[0] == 'B' and s[-1] == 'A':
		a_b_count += 1
	for i in range(len(s) - 1):
		if s[i] == 'A':
			if s[i + 1] == 'B':
				count += 1

if a_b_count == 0:
	print(count + min(a_count, b_count))
else:
	a_count += 1
	b_count += 1
	print(count + min(a_count, b_count) + a_b_count - 1)


Failing test case input:
3
BDAA
AAX[
BDA

Expected output:
1


Actual output:
2


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

N = int(input())

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

count = 0
a_count = 0
b_count = 0
a_b_count = 0

for s in S:
	if s[0] == 'B' and not s[-1] == 'A':
		b_count += 1
	if not s[0] == 'B' and s[-1] == 'A':
		a_count += 1
	if s[0] == 'B' and s[-1] == 'A':
		a_b_count += 1
	for i in range(len(s) - 1):
		if s[i] == 'A':
			if s[i + 1] == 'B':
				count += 1

if a_b_count == 0:
	print(count + min(a_count, b_count))
else:
	if not (a_count == 0 and b_count == 0):
		a_count += 1
		b_count += 1
	print(count + min(a_count, b_count) + a_b_count - 1)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
def main():
	N = int(input())
	S = [""]*N
	ans = 0
	a_end = 0
	b_begin = 0
	a_end_b_begin = 0
	for i in range(N):
		S[i] = input()
		if "AB" in S[i]:
			ans += 1
		if S[i][0] == "B":
			if S[i][-1] == "A":
				a_end_b_begin += 1
				continue
			b_begin += 1
		if S[i][-1] == "A":
			a_end += 1
	ans += min(b_begin, a_end)
	ans += min(abs(b_begin-a_end), a_end_b_begin)
	dif = a_end_b_begin - abs(b_begin-a_end)
	if dif > 0:
		ans += dif

	print(ans)


if __name__ == '__main__':
	main()

Failing test case input:
3
BDAA
[AAX
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
def main():
	N = int(input())
	ans = 0
	a_end = 0
	b_begin = 0
	a_end_b_begin = 0
	for i in range(N):
		S = input()
		ans += S.count("AB")
		if S[0] == "B":
			if S[-1] == "A":
				a_end_b_begin += 1
				continue
			b_begin += 1
		if S[-1] == "A":
			a_end += 1
	if a_end_b_begin > 0:
		ans += a_end_b_begin if a_end+b_begin>0 else a_end_b_begin-1
	ans += min(a_end, b_begin)
	print(ans)


if __name__ == '__main__':
	main()
Problem: p03049
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
N = int(input())
a = 0
b = 0
c = 0
v = 0

for i in range(N):
    S = input()
    for j in range(len(S)-1):
        if S[j:j+2] == 'AB':
            c += 1
    if S[0] == 'B' and S[-1] == 'A':
        v += 1
        continue
    if S[0] == 'B':
        b += 1
    if S[-1] == 'A':
        a += 1

if min(a, b) >= 1:
    print(c + v + min(a, b))
else:
    print(c + max(0, v-1))


Failing test case input:
7
EBBYBAR
JOZ
BMHQUVA
BPA
SIV
MBMABAOBHZ
SZMEIMA

Expected output:
3


Actual output:
2


Fixed submission (verdict: Accepted):
N = int(input())
a = 0
b = 0
c = 0
v = 0

for i in range(N):
    S = input()
    for j in range(len(S)-1):
        if S[j:j+2] == 'AB':
            c += 1
    if S[0] == 'B' and S[-1] == 'A':
        v += 1
        continue
    if S[0] == 'B':
        b += 1
    if S[-1] == 'A':
        a += 1

if a >= 1 or b >= 1:
    print(c + v + min(a, b))
else:
    print(c + max(0, v-1))

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
s= [input() for i in range(n)]
ans1=0
firstB=0
lastA=0
same=0
for i in s:
    ans1+=i.count("AB")
    if i[0]=="B":
        firstB+=1
    if i[-1]=="A":
        lastA+=1
    if i[0]=="B" and i[-1]=="A":
        same+=1
if same==min(firstB,lastA):
    print(ans1+same-1)
else:
    print(ans1+min(firstB,lastA))

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 i in range(n)]
ans1=0
firstB=0
lastA=0
m=n
for i in s:
    ans1+=i.count("AB")
    if i[0]=="B":
        firstB+=1
    if i[-1]=="A":
        lastA+=1
    if i[0]!="B" and i[-1]!="A":
        m-=1
if min(firstB,lastA)==m:
    if m==0:
        print(ans1)
    else:
        print(ans1+m-1)
else:
    print(ans1+min(firstB,lastA))

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

Bxxx = 0
xxxA = 0
BxxA = 0
AB = 0
for i in range(N):
    s = input()
    AB += s.count('AB')
    if s[0] == 'B' and s[-1] != 'A':
        Bxxx += 1
    elif s[-1] == 'A' and s[0] != 'B':
        xxxA += 1
    elif s[0] == 'B' and s[-1] == 'A':
        BxxA += 1

pair = min(Bxxx, xxxA)

if abs(Bxxx - xxxA) > BxxA:
    
    pair += BxxA
    BxxA = 0
else:
    
    pair += abs(Bxxx - xxxA)
    BxxA -= abs(Bxxx - xxxA)


if BxxA > 0:
    pair += BxxA 

print(AB + pair)


Failing test case input:
3
BDAA
AAX[
BDA

Expected output:
1


Actual output:
2


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

Bxxx = 0
xxxA = 0
BxxA = 0
AB = 0
for i in range(N):
    s = input()
    AB += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        BxxA += 1
    elif s[0] == 'B':
        Bxxx += 1
    elif s[-1] == 'A':
        xxxA += 1

if BxxA == 0:
    print(AB + min(Bxxx, xxxA))
elif Bxxx + xxxA == 0:
    print(AB + BxxA - 1)
else:
    print(AB + min(Bxxx, xxxA) + BxxA)
Problem: p03049
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n = int(input())
a_end_count = 0
b_start_count = 0
ab_count = 0
a_or_b_count = 0
for _ in range(n):
  s = input()
  if s[-1] == 'A':
    a_end_count += 1
  if s[0] == 'B':
    b_start_count += 1
  if s[-1] == 'A' or s[0] == 'B':
    a_or_b_count += 1
  ab_count += s.count('AB')
ab_count += min([a_or_b_count - 1, a_end_count, b_start_count])
print(ab_count)

Failing test case input:
3
AABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
n = int(input())
a_end_count = 0
b_start_count = 0
ab_count = 0
a_or_b_count = 0
for _ in range(n):
  s = input()
  if s[-1] == 'A':
    a_end_count += 1
  if s[0] == 'B':
    b_start_count += 1
  if s[-1] == 'A' or s[0] == 'B':
    a_or_b_count += 1
  ab_count += s.count('AB')
ab_count += max(0, min([a_or_b_count - 1, a_end_count, b_start_count]))
print(ab_count)

Problem: p03049
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n=int(input())
s=['' for i in range(n)]
for i in range(n):
  s[i]=input()

cnt=0
cnta=0
cntb=0
cntab=0
ans=0
for i in range(n):
    cnt+=s[i].count('AB')
    if s[i][-1]=='A' and s[i][0]=='B':
        cntab+=1
    elif s[i][-1]=='A':
        cnta+=1
    elif s[i][0]=='B':
        cntb+=1

if cntab==0:
  ans=cnt+min(cnta,cntb)
elif cnta+cntb > 0:
  ans=cnt+cntab+min(cnta,cntb)

if cnta==0 and cntb==0:
  ans=cnt+cntab-1
  
print(ans)



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

Expected output:
0


Actual output:
-1


Fixed submission (verdict: Accepted):
n=int(input())
s=['' for i in range(n)]
for i in range(n):
  s[i]=input()

cnt=0
cnta=0
cntb=0
cntab=0
ans=0
for i in range(n):
    cnt+=s[i].count('AB')
    if s[i][-1]=='A' and s[i][0]=='B':
        cntab+=1
    elif s[i][-1]=='A':
        cnta+=1
    elif s[i][0]=='B':
        cntb+=1

if cntab==0:
  ans=cnt+min(cnta,cntb)
else:
  ans=cnt+cntab+min(cnta,cntb)

if cnta==0 and cntb==0:
  if cntab > 0:
    ans=cnt+cntab-1
  else:
    ans=cnt
print(ans)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
s=[]
a=0
b=0
ab=0
aob=0

for _ in range(N):
    S=input()
    if "AB" in S:
        ab+=S.count("AB")
    if S[0]=="B" and S[-1]=="A":
        aob+=1
    elif S[0]=="B":
        b+=1
    elif S[-1]=="A":
        a+=1
ans=ab

if a+aob==N:
    a-=1
if b+aob==N:
    b-=1
    
if a>b+aob:
    ans+=b+aob
elif b>a+aob:
    ans+=a+aob
else:
    ans+=min(a,b)+aob


print(ans)

Failing test case input:
3
BDAA
AAX[
BDA

Expected output:
1


Actual output:
2


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

ab=0
a=0
b=0
boa=0

for _ in range(N):
    s=input()
    ab+=s.count("AB")
    if s[0]=="B" and s[-1]=="A":
        boa+=1
    elif s[0]=="B":
        b+=1
    elif s[-1]=="A":
        a+=1
ans=ab

if boa>0:
    ans+=boa-1
    if b>0:
        ans+=1
        b-=1
    if a>0:
        ans+=1
        a-=1
ans+=min(a,b)
print(ans)
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n=int(input())

a=0
b=0
ab=0

for i in range(n):
    line=input()
    if line[0]=='B':
        b=b+1
    if line[len(line)-1]=='A':
        a=a+1
    for j in range(len(line)-1):
        if line[j]=='A' and line[j+1]=='B':
            ab=ab+1

if b>a:
    print(ab+a)
else:
    print(ab+b)


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
flag=0

for i in range(n):
    line=input()
    if flag!=1 and (line[len(line)-1]!='A' and line[0]=='B') or (line[len(line)-1]=='A' and line[0]!='B'):
        flag=1
    if line[0]=='B':
        b=b+1
    if line[len(line)-1]=='A':
        a=a+1
    for j in range(len(line)-1):
        if line[j]=='A' and line[j+1]=='B':
            ab=ab+1

if flag==0 and a!=0 and b!=0:
    a,b=a-1,b-1

if b>a:
    print(ab+a)
else:
    print(ab+b)

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


Failing test case input:
3
BDAA
AAX[
BDA

Expected output:
1


Actual output:
2


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

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
ab,xa,bx,bxa = 0,0,0,0
for i in range(n):
  s = input()
  if s[-1] == 'A' and s[0] == 'B':
    bxa += 1
  if s[-1] == 'A':
    xa += 1
  if s[0] == 'B':
    bx += 1
  for j in range(len(s)-1):
    if s[j]+s[j+1] == 'AB':
      ab += 1
if bxa == max(xa,bx):
  print(ab+bxa-1)
else:
  print(ab+min(xa,bx))

Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
n = int(input())
ab,xa,bx,bxa = 0,0,0,0
for i in range(n):
  s = input()
  if s[-1] == 'A' and s[0] == 'B':
    bxa += 1
  if s[-1] == 'A':
    xa += 1
  if s[0] == 'B':
    bx += 1
  for j in range(len(s)-1):
    if s[j]+s[j+1] == 'AB':
      ab += 1
if bxa == xa == bx:
  print(ab+bxa-bool(bxa))
else:
  print(ab+min(xa,bx))
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"])
outcount = len([s for s in Ss if s[-1]=="A"])
if headcount==outcount:
    print(sum(count) + min(headcount, outcount)-1)
else:
    print(sum(count)+min(headcount,outcount))

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())
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.4.3), verdict: Wrong Answer):
N = int(input())

b_start = 0
a_end = 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":
        b_start += 1
    if s[-1] == "A":
        a_end += 1

print(inner + min(N-1,a_end,b_start))            

Failing test case input:
3
BDAA
AAX[
BDA

Expected output:
1


Actual output:
2


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



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

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

cnt = 0
a_cnt = 0
b_cnt = 0
ab_cnt = 0
for x in s_list:
    cnt += x.count("AB")
    if x[-1] == "A":
        a_cnt += 1
    if x[0] == "B":
        b_cnt += 1
    if (x[-1] == "A")&(x[0] == "B"):
        ab_cnt += 1
if ab_cnt==a_cnt==b_cnt:
    print(cnt+min(a_cnt,b_cnt)-1)
else:
    print(cnt+min(a_cnt,b_cnt))

Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


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

cnt = 0
a_cnt = 0
b_cnt = 0
ab_cnt = 0
for x in s_list:
    cnt += x.count("AB")
    if (x[-1] == "A")&(x[0] != "B"):
        a_cnt += 1
    if (x[0] == "B")&(x[-1] != "A"):
        b_cnt += 1
    if (x[-1] == "A")&(x[0] == "B"):
        ab_cnt += 1
if ab_cnt == 0:
    print(cnt+min(a_cnt,b_cnt))
elif (a_cnt+b_cnt>0)&(ab_cnt != 0):
    print(cnt+ab_cnt+min(a_cnt,b_cnt))
elif (a_cnt+b_cnt==0):
    print(cnt+ab_cnt-1)
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):





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




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


if abs(a_cnt-b_cnt) == 0:
    ab_cnt -= 1
if ab_cnt < 0:
    ab_cnt = 0

print('/'.join(S).count('AB') + min(a_cnt, b_cnt, N-1) + ab_cnt)








Failing test case input:
9
BWEPVCRWH
ZQOZYIJX
AVRAEB
Q@
TYMJIHDOX
JNMRMHKCA
AP
ZBAGVQ
PRGKSPUNA

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):





import sys

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




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




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)


    



print('/'.join(S).count('AB') + ans)

Problem: p03049
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
def sol(_inp=input):
    n=int(_inp())
    words=[_inp() for _ in range(n)]
    ab_in_words=sum(w.count('AB') for w in words)
    ends_A=sum(w.endswith('A') for w in words)
    starts_B=sum(w.startswith('B') for w in words)
    sands=sum((w.endswith('A') and w.startswith('B')) for w in words)
    if ends_A==sands and starts_B==sands:
        return str(ab_in_words+sands-1)
    return str(ab_in_words+min(ends_A,starts_B))
print(sol())

Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
def sol(_inp=input):
    n=int(_inp())
    words=[_inp() for _ in range(n)]
    ab_in_words=sum(w.count('AB') for w in words)
    ends_A=sum(w.endswith('A') for w in words)
    starts_B=sum(w.startswith('B') for w in words)
    sands=sum((w.endswith('A') and w.startswith('B')) for w in words)
    if ends_A==sands and starts_B==sands and sands>0:
        return str(ab_in_words+sands-1)
    return str(ab_in_words+min(ends_A,starts_B))
print(sol())
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n= int(input())
a = 0
b = 0
ab = 0
for i in range(n):
    s = input()
    for j in range(len(s)-1):
        if s[j] == "A" and s[j+1] == "B":
            ab+=1
    if s[len(s)-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1
if a == b and a == 0:
    print(ab)
elif a != b:
    print(ab + min(a,b))
elif a == b:
    print(ab + a -1)

Failing test case input:
9
BWEPVCRWH
ZQNZYIJX
AVRAEB
@Q
HJMYITDOX
KNMRMHKCB
AP
ZBAGVQ
PRGKSPUNA

Expected output:
1


Actual output:
0


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

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

Failing test case input:
7
EBBYBAR
JOZ
BMHQUVA
BPA
SIV
MCMABAOBIZ
SZMEHMA

Expected output:
3


Actual output:
2


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

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S = [input() for _ in range(N)]
count = 0
l = []
A,B = 0,0
for s in S:
  c = s.count("AB")
  count += c
  if s[-1] == "A" or s[0] == "B":
    l.append(s[0] + s[-1])
    if s[-1] == "A":
      A += 1
    if s[0] == "B":
      B += 1
ans = min(A,B)
print(count + 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)]
count = 0
l = []
A,B,BA = 0,0,0
for s in S:
  c = s.count("AB")
  count += c
  if s[-1] == "A" or s[0] == "B":
    l.append(s[0] + s[-1])
    if s[-1] == "A" and s[0] == "B":
      BA += 1
    elif s[-1] == "A":
      A += 1
    elif s[0] == "B":
      B += 1
ans = 0
if BA == 0:
  ans = min(A,B)
elif A+B > 0:
  ans = BA + min(A,B)
elif  A+B == 0:
  ans = BA -1 + min(A,B)
print(count + ans)
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
last_a = 0
first_b = 0
both = 0
ans = 0
for i in range(n):
  s = input()
  a, b = s[-1], s[0]
  if a == "A" and b == "B":
    both += 1
  elif a == "A":
    last_a += 1
  elif b == "B":
    first_b += 1
  for j in range(len(s)-1):
    if s[j] == "A" and s[j+1] == "B":
      ans += 1
if last_a == 0 and first_b == 0:
  ans += both - 1
else:
  ans += min(last_a,first_b) + both
print(ans)

Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


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

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

head_b = 0
last_a = 0
ab = 0
ex = 0

for i in range(n) :
    tmp = input()
    ab += tmp.count('AB')
    if tmp[0] == 'B' :
        head_b += 1
    if tmp[len(tmp) - 1] == 'A' :
        last_a += 1
    if tmp[0] == 'B' and tmp[len(tmp) - 1] == 'A' :
        ex = 1

print(ab + min(head_b, last_a) - ex)

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

Expected output:
3


Actual output:
2


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

b = 0
a = 0
ab = 0
ex = 0

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

if max(a, b) > ex or ex == 0 :
    print(ab + min(a, b))
else :
    print(ab + min(a, b) - 1)

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

countA = 0
countB = 0
countAB = 0
for i in range(n):
    s = input()

    findAB = 0
    while(findAB != -1):
        findAB = s.find("AB", findAB)
        if(findAB != -1):
            countAB += 1
            findAB += 1

    if(s[0]=="B"):
        countB += 1
    if(s[len(s)-1]=="A"):
        countA += 1

ans = min(countA, countB) + countAB
print(ans)

Failing test case input:
3
BDAA
AAX[
BDA

Expected output:
1


Actual output:
2


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

countA = 0
countB = 0
countAB = 0
kaburi = 0
for i in range(n):
    s = input()

    findAB = 0
    while(findAB != -1):
        findAB = s.find("AB", findAB)
        if(findAB != -1):
            countAB += 1
            findAB += 1

    flag = False
    if(s[0]=="B"):
        countB += 1
        flag = True
    if(s[len(s)-1]=="A"):
        countA += 1
        if(flag):
            kaburi += 1

ans = min(countA, countB) + countAB
if(countA==countB and countA==kaburi):
    if(countA != 0):
        ans -= 1
print(ans)
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
s = [input() for u in range(n)]
a = sum([u.count("AB") for u in s])
b = len([1 for u in s if u.endswith("A")])
c = len([1 for u in s if u.startswith("B")])
d = len([1 for u in s if u.endswith("A") and u.startswith("B")])
if d==1:
  print(a)
elif (b==c and c==d):
  print(a+b-1)
else:
  print(a+min(b,c))

Failing test case input:
3
AABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
n = int(input())
s = [input() for u in range(n)]
a = sum([u.count("AB") for u in s])
b = len([1 for u in s if u.endswith("A")])
c = len([1 for u in s if u.startswith("B")])
d = len([1 for u in s if u.endswith("A") and u.startswith("B")])
if (b>0 and b==c and c==d):
  print(a+b-1)
else:
  print(a+min(b,c))

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


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

Expected output:
0


Actual output:
-1


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 i in range(n)]
A_cnt=0
B_cnt=0
AB_cnt=0
for s in S:
        if S[0]=="B" and S[-1]=="A":
                AB_cnt+=1
        elif s[-1]=="A":
                A_cnt+=1
        elif s[0]=="B":
                B_cnt+=1

cnt=0
for i in range(n):
        S[i].append("T")
        for j in range(len(S[i])):
                if S[i][j]=="A" and S[i][j+1]=="B":
                        cnt+=1
if A_cnt==B_cnt==0 and 0<AB_cnt:
        print(AB_cnt-1+cnt)
else:
        print(cnt+min(A_cnt,B_cnt))


Failing test case input:
7
EBBYBAR
JOZ
BMHQUVA
BPA
SIV
MBMABAOBHZ
SZMEIMA

Expected output:
3


Actual output:
1


Fixed submission (verdict: Accepted):
n=int(input())
S=[list(input())for i in range(n)]
A_cnt=0
B_cnt=0
AB_cnt=0
for s in S:
        if s[0]=="B" and s[-1]=="A":
                AB_cnt+=1
        elif s[-1]=="A":
                A_cnt+=1
        elif s[0]=="B":
                B_cnt+=1

cnt=0
for i in range(n):
        S[i].append("T")
        for j in range(len(S[i])):
                if S[i][j]=="A" and S[i][j+1]=="B":
                        cnt+=1
if A_cnt==B_cnt==0 and 0<AB_cnt:
        print(AB_cnt-1+cnt)
else:
        print(cnt+min(A_cnt+AB_cnt,B_cnt+AB_cnt))
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S = [None] * N
for i in range(N):
    S[i] = input()

S.sort(key=lambda s: not s.endswith('A'))

ans = 0
endswith_a = 0
startswith_b = 0
both_ab = 0
for s in S:
    ans += s.count('AB')
    a = s.endswith('A')
    b = s.startswith('B')
    endswith_a += 1 if a else 0
    startswith_b += 1 if b else 0
    both_ab += 1 if a and b else 0

if both_ab == endswith_a or both_ab == startswith_b:
    if endswith_a == startswith_b:
        ans += both_ab - 1
    else:
        ans += both_ab
else:
    ans += min(endswith_a, startswith_b)

print(ans)


Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


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

ans = 0
endswith_a = 0
startswith_b = 0
both_ab = 0
for s in S:
    ans += s.count('AB')
    a = s.endswith('A')
    b = s.startswith('B')
    endswith_a += 1 if a else 0
    startswith_b += 1 if b else 0
    both_ab += 1 if a and b else 0

if both_ab == endswith_a or both_ab == startswith_b:
    if endswith_a == startswith_b and both_ab > 0:
        ans += both_ab - 1
    else:
        ans += both_ab
else:
    ans += min(endswith_a, startswith_b)

print(ans)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S = []
for i in range(N):
    S.append(input())

count_AB = 0
lefter_B = 0
righter_A = 0
for i in range(N):
    count_AB += S[i].count('AB')
    if S[i][0] == 'B':
        lefter_B += 1
    if S[i][len(S[i])-1] == 'A':
        righter_A += 1       
if min(lefter_B,righter_A) == len(S):
    Appearing_AB = min(lefter_B,righter_A) - 1
else:
    Appearing_AB = min(lefter_B,righter_A)


print(count_AB + Appearing_AB)



Failing test case input:
3
BDAA
AAX[
BDA

Expected output:
1


Actual output:
2


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

count_AB = 0
lefter_B = 0
righter_A = 0
only_lefter_B = 0
only_righter_A = 0
for i in range(N):
    count_AB += S[i].count('AB')
    if S[i][0] == 'B':
        lefter_B += 1
    if S[i][len(S[i])-1] == 'A':
        righter_A += 1
    if (S[i][0] == 'B')and(S[i][len(S[i])-1] != 'A'):
        only_lefter_B += 1
    if (S[i][0] != 'B')and(S[i][len(S[i])-1] == 'A'):
        only_righter_A += 1
        
if (only_lefter_B==0) and (only_righter_A==0):
    Appearing_AB = max(min(lefter_B,righter_A) - 1 , 0)
else:
    Appearing_AB = min(lefter_B,righter_A)


print(count_AB + Appearing_AB)
Problem: p03049
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
N = int(input())

countA = 0
countB = 0
countAB = 0
countBA = 0
for i in range(N):
    s = input()
    countAB += s.count('AB')
    if s[0] == 'B':
        countB += 1
    if s[-1] == 'A':
        countA += 1
    if s[0] == 'B' and s[-1] == 'A':
        countBA += 1
if countA == countBA and countB == countBA:
    print(countAB + countBA - 1)
else:
    print(countAB + min(countA, countB))


Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


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

countA = 0
countB = 0
countAB = 0
countBA = 0
for i in range(N):
    s = input()
    countAB += s.count('AB')
    if s[0] == 'B':
        countB += 1
    if s[-1] == 'A':
        countA += 1
    if s[0] == 'B' and s[-1] == 'A':
        countBA += 1
if countA != 0 and countA == countBA and countB == countBA:
    print(countAB + countBA - 1)
else:
    print(countAB + min(countA, countB))

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
x = int(input())
a = 0
b = 0
c = 0
f = 0
bun = []
for p in range(x):
    bun = input()
    if bun[0] == "B":
        b +=1
    if bun[len(bun)-1] == "A":
        a += 1
        if bun[0] == "B":
            f += 1
    for k in range(len(bun)-1):
        if bun[k] == "A":
            if bun[k+1] == "B":
                c += 1




if f == a:
    if a == 0:
        a -= 1
if a < b:
    print(a+c)
else:
    print(b+c)


Failing test case input:
3
BD>B
@XB\
DAC

Expected output:
0


Actual output:
-1


Fixed submission (verdict: Accepted):
x = int(input())
a = 0
b = 0
c = 0
f = 0
bun = []
for p in range(x):
    bun = input()
    if bun[0] == "B":
        b +=1
    if bun[len(bun)-1] == "A":
        a += 1
        if bun[0] == "B":
            f += 1
    for k in range(len(bun)-1):
        if bun[k] == "A":
            if bun[k+1] == "B":
                c += 1



if f == a:
    if a == b:
        if a > 0:
            a -= 1
if a < b:
    print(a+c)
else:
    print(b+c)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
count=0
LastA=0
FirstB=0
LA=0
for i in range(0,N):
    f=input()
    count=count+f.count('AB')
    if f[0]=='B' and f[-1]=='A':
        LA=LA+1
    elif f[0]=='B':
        FirstB=FirstB+1
    elif f[-1]=='A':
        LastA=LastA+1
if LA>0:
    count=count+LA
if LastA==0 and FirstB==0:
    count=count-1
elif FirstB>=LastA:
    count=count+LastA
else:
    count=count+FirstB
print(count)

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

Expected output:
0


Actual output:
-1


Fixed submission (verdict: Accepted):
N=int(input())
count=0
LastA=0
FirstB=0
LA=0
for i in range(0,N):
    f=input()
    count=count+f.count('AB')
    if f[0]=='B' and f[-1]=='A':
        LA=LA+1
    elif f[0]=='B':
        FirstB=FirstB+1
    elif f[-1]=='A':
        LastA=LastA+1
count=count+LA
if LastA==0 and FirstB==0 and LA!=0:
    count=count-1
elif FirstB>=LastA:
    count=count+LastA
else:
    count=count+FirstB
print(count)
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

N = int(input())
s = []

naka=0
a_kazu=0
b_kazu=0
b_a_kazu=0
for i in range(N):
    a = str(input())
    naka+=a.count("AB")
    if (a[0]=="B" and a[-1]=="A"):
        b_a_kazu+=1
    elif a[0]=="B":
        b_kazu+=1
    elif a[-1]=="A":
        a_kazu+=1
if b_a_kazu>0:
    a_kazu+=1
    b_kazu+=1
    b_a_kazu-=1
print(naka+b_a_kazu+min(a_kazu, b_kazu))

Failing test case input:
3
BDAA
[AAX
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):

N = int(input())

naka=0
a_kazu=0
b_kazu=0
b_a_kazu=0
for i in range(N):
    a = str(input())
    naka+=a.count("AB")
    if (a[0]=="B" and a[-1]=="A"):
        b_a_kazu+=1
    elif a[0]=="B":
        b_kazu+=1
    elif a[-1]=="A":
        a_kazu+=1
if b_a_kazu==0:
    print(naka+min(a_kazu, b_kazu))
else:
    if (a_kazu==0 and b_kazu==0):
        print(naka+(b_a_kazu-1))
    elif (a_kazu==0 or b_kazu==0):
        print(naka+(b_a_kazu))
    else:
        print(naka+(b_a_kazu-1)+min(a_kazu+1, b_kazu+1))

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S = [input() for _ in range(N)]
first_c = []
last_c = []
XA = 0
BA = 0
BX = 0

ans = 0

for s in S:
    first_c.append(s[0])
    last_c.append(s[len(s)-1])
    ans += s.count("AB")

for i in range(len(S)):
    if first_c[i] == "B" and last_c[i] == "A":
        BA += 1
    elif first_c[i] == "B" and last_c[i] != "A":
        BX += 1
    elif first_c[i] != "B" and last_c[i] == "A":
        XA += 1

if BX > 0 and XA > 0:
    ans += min(BX, XA) + BA
elif BX > 0 or XA > 0:
    ans += BA
else:
    ans += BA-1

print(ans)


Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())
S = [input() for _ in range(N)]
first_c = []
last_c = []
XA = 0
BA = 0
BX = 0

ans = 0

for s in S:
    first_c.append(s[0])
    last_c.append(s[len(s)-1])
    ans += s.count("AB")

for i in range(len(S)):
    if first_c[i] == "B" and last_c[i] == "A":
        BA += 1
    elif first_c[i] == "B" and last_c[i] != "A":
        BX += 1
    elif first_c[i] != "B" and last_c[i] == "A":
        XA += 1

if BX > 0 and XA > 0:
    ans += min(BX, XA) + BA
elif BX > 0 or XA > 0:
    ans += BA
elif BA > 1:
    ans += BA-1

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

def inp():
  return sys.stdin.readline().strip()

N = int(inp())
inAB = 0
sB = 0
eA = 0
for _ in range(N):
  s = inp()
  if 'AB' in s:
    inAB += 1
  if s[0] == 'B':
    sB += 1
  if s[-1] == 'A':
    eA += 1

ans = inAB + min(sB, eA)
if N == sB == eA:
  ans -= 1
print(ans)

Failing test case input:
3
BDAA
[AAX
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
import sys

def inp():
  return sys.stdin.readline().strip()

N = int(inp())
inAB = 0
sB = 0
eA = 0
both = 0
for _ in range(N):
  s = inp()
  inAB += s.count('AB')
  if s[0] == 'B' and s[-1] == 'A':
    both += 1
    continue
  if s[0] == 'B':
    sB += 1
  if s[-1] == 'A':
    eA += 1

pair = min(eA, sB)
if sB > 0 or eA > 0:
  ans = both + pair
else:
  ans = both - 1
print(inAB + max(ans, 0))

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
s = [input() for u in range(n)]
a = sum([u.count("AB") for u in s])
b = len([1 for u in s if u.endswith("A")])
c = len([1 for u in s if u.startswith("B")])
d = len([1 for u in s if u.endswith("A") and u.startswith("B")])
if (b==c and c==d):
  print(a+b-1)
else:
  print(a+min(b,c))

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 u in range(n)]
a = sum([u.count("AB") for u in s])
b = len([1 for u in s if u.endswith("A")])
c = len([1 for u in s if u.startswith("B")])
d = len([1 for u in s if u.endswith("A") and u.startswith("B")])
if (b>0 and b==c and c==d):
  print(a+b-1)
else:
  print(a+min(b,c))

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





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




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


if abs(a_cnt-b_cnt) == 0:
    ab_cnt -= 1

print('/'.join(S).count('AB') + min(a_cnt, b_cnt, N-1) + ab_cnt)








Failing test case input:
7
FBBYBAR
JOZ
BMHQUVA
BPA
SIV
MBMABAOBHZ
SZMEIMA

Expected output:
3


Actual output:
1


Fixed submission (verdict: Accepted):





import sys

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




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




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)


    



print('/'.join(S).count('AB') + ans)
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
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 countBA>0:
    count+=countBA-1+min(countB+1,countA+1)

else:
    count+=min(countB,countA)
    
print(count)
 
        
    
    
            
        
        


     

    


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 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)

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


Failing test case input:
7
EBBYBAR
JOZ
BMHQUVA
BPA
SIV
MBMABAOBHZ
SZMEIMA

Expected output:
3


Actual output:
2


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

cnt = 0
enda = 0
startb = 0
ba = 0
for s in strs:
    cnt += s.count("AB")
    enda += 1 if (s[-1] == "A") else 0
    startb += 1 if (s[0] == "B") else 0
    ba += 1 if (s[-1] == "A" and s[0] == "B") else 0

cnt -= 1 if (enda-ba == 0 and startb-ba == 0) else 0

print(cnt + min(enda, startb))


Failing test case input:
3
AABD
ZABX
AEB

Expected output:
2


Actual output:
1


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

cnt = 0
enda = 0
startb = 0
ba = 0
for s in strs:
    cnt += s.count("AB")
    enda += 1 if (s[-1] == "A") else 0
    startb += 1 if (s[0] == "B") else 0
    ba += 1 if (s[0] == "B" and s[-1] == "A") else 0

cnt -= 1 if (ba != 0 and enda == ba and startb == ba) else 0

print(cnt + min(enda, startb))

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    cunt_a=0
    cunt_b=0
    cunt_ba=0
    cunt_ab=0
    for i in range(n):
        s=input()
        if s[0]=='B' and s[-1]=='A':
            cunt_ba+=1
        elif s[0]=='B': 
            cunt_b+=1
        elif s[-1]=='A':
            cunt_a+=1
        for j in range(len(s)-1):
            if s[j:j+2]=="AB":
                cunt_ab+=1
    if cunt_a==0 and cunt_b==0:
        print(cunt_ab+cunt_ba-1)
    else:
        print(cunt_ab+cunt_ba+min(cunt_a,cunt_b))
    
if __name__=='__main__':
    main()

Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    cunt_a=0
    cunt_b=0
    cunt_ba=0
    cunt_ab=0
    for i in range(n):
        s=input()
        if s[0]=='B' and s[-1]=='A':
            cunt_ba+=1
        elif s[0]=='B': 
            cunt_b+=1
        elif s[-1]=='A':
            cunt_a+=1
        for j in range(len(s)-1):
            if s[j:j+2]=="AB":
                cunt_ab+=1
    if cunt_a==0 and cunt_b==0 and cunt_ba>0:
        print(cunt_ab+cunt_ba-1)
    else:
        print(cunt_ab+cunt_ba+min(cunt_a,cunt_b))
    
if __name__=='__main__':
    main()
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()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
ans = 0
a=0
b=0

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

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):
import sys
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
ans = 0
a=0
b=0
c=0

for i in range(n):
    s = ns()
    ans += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        c += 1
    elif s[0] == 'B':
        b += 1
    elif s[-1] == 'A':
        a += 1

ans += min(n-1,a,b)+c
if a+b == 0 and c > 0:
    ans -= 1

print(ans)

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

import itertools

N = int(input())
li = []
lasta = 0
firstb = 0
ab = 0
cnt = 0



for i in range(N):
    s = input()
    cnt += s.count('AB')
    li = list(s)
    if li[len(li)-1] == 'A' and li[0] != 'B':
        lasta += 1
    if li[0] == 'B' and li[len(li)-1] != 'A':
        firstb += 1
    if li[len(li)-1] == 'A' and li[0] == 'B':
        lasta += 1
        firstb += 1
        ab += 1

if ab % 2 == 0:
    ab = int(ab / 2)
elif ab % 2 == 1:
    pass


if lasta == firstb:
    cnt += lasta - ab
else:
    cnt += max(lasta, firstb) - ab

print(cnt)


Failing test case input:
9
BWEPVCRWH
ZQNZYIJX
AVRAEB
@Q
TYMJIHDOX
KNMRMHKCA
AP
ZBAGVQ
PRGKSPUNA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):

import itertools

N = int(input())
li = []
lasta = 0
firstb = 0
ab = 0
cnt = 0



for i in range(N):
    s = input()
    cnt += s.count('AB')
    li = list(s)
    if li[-1] == 'A' and li[0] != 'B':
        lasta += 1
    if li[0] == 'B' and li[-1] != 'A':
        firstb += 1
    if li[-1] == 'A' and li[0] == 'B':
        ab += 1



if ab == 0:
    cnt += min(lasta,firstb)
elif ab != 0 and lasta + firstb > 0:
    cnt += ab + min(lasta, firstb)
elif ab != 0 and lasta + firstb == 0:
    cnt += ab - 1

print(cnt)