Problem:


Score : 400 points

Problem Statement

Snuke has N strings. The i-th string is s_i.

Let us concatenate these strings into one string after arranging them in some order.
Find the maximum possible number of occurrences of AB in the resulting string.

Constraints

1 \leq N \leq 10^{4}

2 \leq |s_i| \leq 10

s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

For example, if we concatenate ABCA, BAD and XBAZ in this order, the resulting string ABCABADXBAZ has two occurrences of AB.

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4



Sample input:
3
ABCA
XBAZ
BAD


Sample output:
2

Submission status: Wrong Answer

Solution code:
N = int(input())

a, b, c, d = 0, 0, 0, 0

for _ in range(N):
    s = input()
    
    if s[0]=="B" and s[-1]=="A":
        a += 1
    else:
        if s[0]=="B":
            b += 1
        if s[-1]=="A":
            c += 1
    d += s.count("AB")

ans = d + max(0, a-1) + min(b, c)
if b!=c and a:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

a, b, c, d = 0, 0, 0, 0

for _ in range(N):
    s = input()
    
    if s[0]=="B" and s[-1]=="A":
        a += 1
    else:
        if s[0]=="B":
            b += 1
        if s[-1]=="A":
            c += 1
    d += s.count("AB")

ans = d + max(0, a-1) + min(b, c)
if b!=c:
    ans += 1
print(ans)

Submission status: Wrong Answer

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

if min(A,B)==n:
    print(count+n-1)
else:
    print(count+min(A,B))

Submission status: Wrong Answer

Solution code:
N = int(input())

a, b, c, d = 0, 0, 0, 0

for _ in range(N):
    s = input()
    
    if s[0]=="B" and s[-1]=="A":
        d += 1
    else:
        if s[0]=="B":
            b += 1
        if s[-1]=="A":
            a += 1
    c += s.count("AB")

x = min(a, b, d)
a -= x
b -= x
d -= x
xx = min(a, b)
a -= xx
b -= xx

e = 0
if d>1:
    e += d-1

print(2*x+xx+c+e)

Submission status: Wrong Answer

Solution code:
N = int(input())

a, b, c, d = 0, 0, 0, 0

for _ in range(N):
    s = input()
    
    if s[0]=="B" and s[-1]=="A":
        d += 1
    else:
        if s[0]=="B":
            b += 1
        if s[-1]=="A":
            a += 1
    c += s.count("AB")

x = min(a, b, d)
a -= x
b -= x
d -= x
xx = min(a, b)
a -= xx
b -= xx

print(2*x+xx+c+d)

Submission status: Wrong Answer

Solution code:
N = int(input())

a, b, c = 0, 0, 0

for _ in range(N):
    s = input()
    
    if s[0]=="B":
        b += 1
    if s[-1]=="A":
        a += 1
    if s!="AB":
        c += s.count("AB")

print(min(a,b)+c)

Submission status: Wrong Answer

Solution code:
N = int(input())

a, b, c = 0, 0, 0

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

print(min(a,b)+c)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in range(n)]

cnt_AB = 0
cnt_last_A = 0
cnt_start_B = 0
cnt_start_B_last_A = 0

for i in range(n):
    cnt_AB += s[i].count('AB')
    st = s[i][0]
    la = s[i][-1]
    if st == 'B' and la == 'A':
        cnt_start_B_last_A += 1
    elif st == 'B':
        cnt_start_B += 1
    elif la == 'A':
        cnt_last_A += 1

if cnt_start_B == 0 and cnt_last_A == 0:
    print(cnt_AB)
else:
    print(cnt_AB + min(cnt_last_A,
                       cnt_start_B)+cnt_start_B_last_A)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=0
b=0
c=0
d=0
for i in range(n):
  S=input()
  for i in range(len(S)-1):
    if S[i]=="A" and S[i+1]=="B":
      c=c+1
  if S[0]=="B":
    b=b+1
  if S[-1]=="A":
    a=a+1
  if S[0]=="B" and S[-1]=="A":
    d=d+1
if a==b and b==d:
  print(c+min(a,b)-1)
else:
  print(c+min(a,b))

Submission status: Wrong Answer

Solution code:
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-1)
else:
  print(ab+min(xa,bx))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[input() for i in range(n)]
cnt=0
a_=0#hajime
b_=0
for i in a:
    if i[-1]=="A":
        a_+=1
    if i[0]=="B":
        b_+=1
    
    for j in range(1,len(i)):
      if i[j-1]=="A" and i[j]=="B":
        cnt+=1
c=min(a_,b_,n-1)
print(c+cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[input() for i in range(n)]
cnt=0
a_=0#hajime
b_=0
for i in a:
    if i[-1]=="A":
        a_+=1
    if i[0]=="B":
        b_+=1
    for j in range(1,len(i)):
      if i[j-1]=="A" and i[j]=="B":
        cnt+=1
c=min(a_,b_)
print(c+cnt)

Submission status: Wrong Answer

Solution code:
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))

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=0
a=0;b=0;ab=0
for i in range(n):
  s=input()
  ans+=s.count("AB")
  if s[0]=="B":
    b+=1
    if s[-1]=="A":
      ab+=1
      b-=1
  elif s[-1]=="A":
    a+=1
sa=max(a,b)-min(a,b)
if sa==0:
    print(ans+min(a,b)+max(ab-1,0))
else:
    print(ans+min(a,b)+ab)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=0
b=0
ab=0
x=0
for i in range(n):
  s=input()
  l=len(s)-1
  if s[0]=='B':
    if s[l]=='A':
      ab+=1
    else:
      b+=1
  elif s[l]=='A':
    a+=1
  x+=s.count('AB')
if ab>0:
  x+=(a>0)+(b>0)
x+=max(0,ab-1)
print(x)

Submission status: Wrong Answer

Solution code:
import sys
def solve():
    N = int(input())
    inside = 0
    lastA = 0
    firstB = 0
    both = 0
    for i in range(N):
        s = input()
        if s[0] == "B": 
            if s[-1] == "A": both += 1
            else: firstB += 1
        for k in range(len(s) - 1):
            if s[k] == "A" and s[k+1] == "B": inside += 1
        if s[0] != "B" and s[-1] == "A": lastA += 1
    if both > 0:
        inside += both - 1
        lastA += 1
        firstB += 1
    combine = min(lastA, firstB)

    print(inside + combine)
    return 0
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import sys
def solve():
    N = int(input())
    inside = 0
    lastA = 0
    firstB = 0
    for i in range(N):
        s = input()
        if s[0] == "B": firstB += 1
        for k in range(len(s) - 1):
            if s[k] == "A" and s[k+1] == "B":
                inside += 1
        if s[-1] == "A": lastA += 1
    combine = min(lastA, firstB)
    combine = min(N - 1, combine)
    print(inside + combine)
    return 0
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
n = int(input())
l = []
for i in range(n):
    data = input()
    l.append(data)

ans=0
import re
b_st=0
a_en=0
b_a=0

for tmp in l:
    res = re.findall('AB', tmp)
    ans += len(res)

    a_flg=False
    b_flg=False
    if tmp[0] == "B":
        b_flg=True
    if tmp[-1] == "A":
        a_flg=True

    if b_flg and a_flg:
        b_a+=1
    elif b_flg:
        b_st+=1
    elif a_flg:
        a_en+=1

ans+=min(a_en,b_st)
if b_a != 0:
    ans+=b_a-1

if a_en==b_st:
    print(ans)
else:
    print(ans+1)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
a = 0
b = 0
ba = 0
for _ in range(n):
    s = input()
    if s[0] == 'B' and s[-1] == 'A':
        ba += 1
    else:
        if s[0] == 'B':
            b += 1
        if s[-1] == 'A':
            a += 1
    ans += s.count('AB')
ans += ba - 1
if a >= 1:
    ans += 1
    a -= 1
if b >= 1:
    ans += 1
    b -= 1
ans += min(a, b)
print(max(ans, 0))

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
a = 0
b = 0
ba = 0
for _ in range(n):
    s = input()
    if s[0] == 'B' and s[-1] == 'A':
        ba += 1
    else:
        if s[0] == 'B':
            b += 1
        if s[-1] == 'A':
            a += 1
    ans += s.count('AB')
ans += ba - 1
if a >= 1:
    ans += 1
    a -= 1
if b >= 1:
    ans += 1
    b -= 1
ans += min(a, b)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = []
count = 0
BA = 0
Ba = 0
bA = 0
for i in range(N):
    a = input()
    s.append(a)
    count += a.count('AB')
    if a[0] == 'B':
        if a[-1] =='A':
            BA += 1
        else:
            Ba += 1
    elif a[-1] == 'A':
        bA +=1
if not BA:
    count += min(Ba,bA)
else:
    if bA and bA:
        count += BA + min(bA,Ba)
    else:
        count += BA-1
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = []
count = 0
BA = 0
Ba = 0
bA = 0
for i in range(N):
    a = input()
    s.append(a)
    count += a.count('AB')
    if a[0] == 'B':
        if a[-1] =='A':
            BA += 1
        else:
            Ba += 1
    elif a[-1] == 'A':
        bA +=1
if not BA:
    count += min(Ba,bA)
else:
    if bA:
        count += BA + min(bA,Ba)
    else:
        count += min(BA,Ba)
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in range(n)]
a = 0
b = 0
ab = 0
a_b = 0
for i in s:
    if i[0] == 'B' and i[-1] == 'A':
        a_b += 1
    if i[0] == 'B':
        b += 1
    if i[-1] == 'A':
        a += 1
    for j in range(len(i)-1):
        if i[j:j+2] == 'AB':
            ab += 1
if a_b == a == b:
    b -= 1
print(ab+min(a, b))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in range(n)]
a = 0
b = 0
ab = 0
for i in s:
    if i[0] == 'B':
        b += 1
    if i[-1] == 'A':
        a += 1
    for j in range(len(i)-1):
        if i[j:j+2] == 'AB':
            ab += 1
print(ab+min(a, b, n-1))

Submission status: Wrong Answer

Solution code:
#a,b,x=map(int,input().split())

n=int(input())
s=[input() for i in range(n)]

ans=0
ab=0
xa=0
bx=0
for i in range(n):
    ans+=s[i].count('AB')

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

cnt=min(ab+xa,ab+bx) if (xa!=0 or bx!=0) else ab-1
print(ans+cnt if cnt>0 else ans)
print(ans,cnt)

Submission status: Wrong Answer

Solution code:
#a,b,x=map(int,input().split())

n=int(input())
s=[input() for i in range(n)]

ans=0
ab=0
xa=0
bx=0
for i in range(n):
    ans+=s[i].count('AB')

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

cnt=min(ab+xa,ab+bx) if (xa!=0 or bx!=0) else ab-1
print(ans+cnt)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = [input() for i in range(N)]
    ans = 0
    ba = 0
    b = 0
    a = 0
    for s in A:
        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
    ans += (ba - 1 if ba > 1 else 0)
    if ba > 0:
        b += 1
        a += 1
    ans += min(a, b)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for i in range(N)]
ABcnt = 0
Acnt = 0
Bcnt = 0
BAcnt = 0
for s in S:
    for i in range(len(s)-1):
        if s[i] == 'A' and s[i+1] == 'B':
            ABcnt += 1
    if s[0] == 'B' and s[-1] == 'A':
        BAcnt += 1
    elif s[0] == 'B':
        Bcnt += 1
    elif s[-1] == 'A':
        Acnt += 1

if Acnt == 0 and Bcnt == 0:
    ans = ABcnt + BAcnt - 1
else:
    ans = ABcnt + BAcnt + min(Acnt,Bcnt)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for i in range(N)]
ABcnt = 0
Acnt = 0
Bcnt = 0
BAcnt = 0
for s in S:
    for i in range(len(s)-1):
        if s[i] == 'A' and s[i+1] == 'B':
            ABcnt += 1
    if s[0] == 'B' and s[-1] == 'A':
        BAcnt += 1
    elif s[0] == 'B':
        Bcnt += 1
    elif s[-1] == 'A':
        Acnt += 1

if Acnt == 0 and Bcnt == 0:
    ans = ABcnt + BAcnt - 1
elif Acnt*Bcnt == 0:
    ans = ABcnt + BAcnt
else:
    ans = ABcnt + BAcnt + min(Acnt,Bcnt)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for i in range(N)]
ABcnt = 0
Acnt = 0
Bcnt = 0
for s in S:
    if s[0] == 'B':
        Bcnt += 1
    if s[-1] == 'A':
        Acnt += 1
    for i in range(len(s)-1):
        if s[i] == 'A' and s[i+1] == 'B':
            ABcnt += 1
ans = ABcnt + min(Acnt,Bcnt)
print(ans)

Submission status: Wrong Answer

Solution code:
#k,a,b=map(int,input().split())
n=int(input())
s=[input() for i in range(n)]
fin_a=0
ini_b=0
ab=0
for i in s:
    if i[0]=='B' and i[-1]=='A':
        ab+=1
    elif i[0]=='B':
        ini_b+=1
    elif i[-1]=='A':
        fin_a+=1

s='+'.join(s)
ans=s.count('AB')
print(ans+min(fin_a+ab,ini_b+ab) if not (fin_a==0 and ini_b==0) else ans+max(ab-1,0))
print(ini_b,fin_a,ab)

Submission status: Wrong Answer

Solution code:
#k,a,b=map(int,input().split())
n=int(input())
s=[input() for i in range(n)]
fin_a=0
ini_b=0
ab=0
for i in s:
    if i[0]=='B' and i[-1]=='A':
        ab+=1
    elif i[0]=='B':
        ini_b+=1
    elif i[-1]=='A':
        fin_a+=1

s='+'.join(s)
ans=s.count('AB')
print(ans+min(fin_a+ab,ini_b+ab) if not (fin_a==0 and ini_b==0) else ans+ab-1)

Submission status: Wrong Answer

Solution code:
#k,a,b=map(int,input().split())
n=int(input())
s=[input() for i in range(n)]
a_count=0
b_count=0
fin_a=0
ini_b=0
ab=0
for i in s:
    if i[0]=='B' and i[-1]=='A':
        ab+=1
    elif i[0]=='B':
        ini_b+=1
    elif i[-1]=='A':
        fin_a+=1

s='+'.join(s)
ans=s.count('AB')
print(ans+min(fin_a+ab,ini_b+ab) if not (ab==1 and fin_a==0 and ini_b==0) else ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[input() for i in range(n)]
a_count=0
b_count=0
fin_a=0
ini_b=0
for i in s:
    if i[0]=='B':
        ini_b+=1
    if i[-1]=='A':
        fin_a+=1

s='+'.join(s)
ans=s.count('AB')
print(ans+min(fin_a,ini_b))

Submission status: Wrong Answer

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
li = [stdin.readline().rstrip() for _ in range(n)]
b_sta = 0
a_last = 0
b_a = 0
ab = 0
for i in li:
    ab += i.count("AB")
    if i[0] == "B" and i[-1] == "A":
        b_a += 1
    elif i[0] == "B":
        b_sta += 1
    elif i[-1] == "A":
        a_last += 1
if b_a == 0:
    print(ab+min(a_last,b_sta))
else:
    if a_last != 0 or b_sta != 0:
        print(ab+b_a+min(a_last,b_sta))
    else:
        print(ab-1)

Submission status: Wrong Answer

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
li = [stdin.readline().rstrip() for _ in range(n)]
b_sta = 0
a_last = 0
ab = 0
for i in li:
    ab += i.count("AB")
    if i[0] == "B" and i[-1] == "A":
        if a_last < b_sta:
            a_last += 1
        else:
            b_sta += 1
    elif i[0] == "B":
        b_sta += 1
    elif i[-1] == "A":
        a_last += 1
print(ab+max(a_last,b_sta))

Submission status: Wrong Answer

Solution code:
N = int(input())
count = 0
a = 0
b = 0
c = 0

for _ in range(N):
    s = input()
    count += s.count("AB")
    c += (s[0] == "B" and s[-1] == "A")
    a += (s[0] == "B" and s[-1] != "A")
    b += (s[0] != "B" and s[-1] == "A")

c = c - 1 if c >= 1 else 0
print(count + min([a, b]) + c + (a != b))

Submission status: Wrong Answer

Solution code:
N = int(input())
count = 0
a = 0
b = 0
c = 0

for _ in range(N):
    s = input()
    count += s.count("AB")
    c += (s[0] == "B" and s[-1] == "A")
    a += (s[0] == "B" and s[-1] != "A")
    b += (s[0] != "B" and s[-1] == "A")


print(count + min([a, b]) + c - 1 + (a != b))

Submission status: Wrong Answer

Solution code:
N = int(input())
count = 0
a = 0
b = 0

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

print(count + min([a, b]))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
counter1 = 0
counter2 = 0
for s in S:
    if s[-1] == "A":
        counter1 += 1
    if s[0] == "B":
        counter2 += 1
ans = 0
ans += min(counter1, counter2)
for s in S:
    ans += s.count("AB")
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

bot_a_cnt, top_b_cnt, bXa_cnt = 0, 0, 0
ab_cnt = 0

for i in range(N):
  S = input()
  top_b_cnt += 1 if S[0] == "B" else 0
  bot_a_cnt += 1 if S[-1] == "A" else 0
  bXa_cnt += 1 if S[0] == "B" and S[-1] == "A" else 0
  for j in range(len(S)-1):
    if S[j:j+2] == "AB":
      ab_cnt += 1
      
#print(bot_a_cnt, top_b_cnt, bXa_cnt, ab_cnt)
print(ab_cnt + min(bot_a_cnt, top_b_cnt) - (1 if bot_a_cnt == top_b_cnt and top_b_cnt == bXa_cnt else 0))

Submission status: Wrong Answer

Solution code:
n = int(input())
lst = [input() for _ in range(n)]

fin_A = 0
top_B = 0
BtoA = 0
cnt = 0
for s in lst:
    cnt += s.count('AB')
    if s[0]=='B' and s[-1]=='A':
        BtoA += 1
    else:
        if s[0] == 'B':
            top_B += 1
        if s[-1] == 'A':
            fin_A += 1
cnt += BtoA + min(top_B, fin_A+1)
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
lst = [input() for _ in range(n)]

fin_A = 0
top_B = 0
cnt = 0
for s in lst:
    cnt += s.count('AB')
    if s[0] == 'B':
        top_B += 1
    if s[-1] == 'A':
        fin_A += 1
cnt += min(top_B, fin_A)
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())

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


if a == n:
    a -=1
if b == n:
    b -=1

print(ans + min(a,b))

Submission status: Wrong Answer

Solution code:
n = int(input())

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


if a == n or b == n:
    a,b = a-1,b-1

print(ans + min(a,b))

Submission status: Wrong Answer

Solution code:
n = int(input())

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


print(ans + min(a,b))

Submission status: Wrong Answer

Solution code:
n=int(input())
ab_num=0
last_a=0
first_b=0
for i in range(n):
    s=input()
    if(s[0]=='B'):
        first_b+=1
    if(s[-1]=='A'):
        last_a+=1

    ab_idx=s.find('AB')
    while(ab_idx!=-1):
        ab_num+=1
        ab_idx=s.find('AB',ab_idx+1)

sole_ab=0
if(last_a!=first_b):
    sole_ab=min(last_a,first_b)
else:
    sole_ab=last_a-1
print(ab_num + sole_ab)

Submission status: Wrong Answer

Solution code:
n=int(input())
ab_num=0
last_a=0
first_b=0
for i in range(n):
    s=input()
    if(s[0]=='B'):
        first_b+=1
    if(s[-1]=='A'):
        last_a+=1

    ab_idx=s.find('AB')
    while(ab_idx!=-1):
        ab_num+=1
        ab_idx=s.find('AB',ab_idx+1)
print(ab_num + min(last_a,first_b))

Submission status: Wrong Answer

Solution code:
import sys
N = int(sys.stdin.readline())
S = list(map(str,sys.stdin.read().rstrip().split("\n")))

ans = 0
A = 0
B = 0
BA = 0
for s in S:
	ans += s.count('AB')
	if  s[0] == 'B' and s[-1] == 'A':
		BA += 1
		continue
	elif s[0] == 'B':
		B += 1
	elif s[-1] == 'A':
		A += 1

if A + B == 0:
	ans = max(0,BA-1)
else:
	ans += BA
	ans += min(A,B)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N = int(sys.stdin.readline())
S = list(map(str,sys.stdin.read().rstrip().split("\n")))

ans = 0
A = 0
B = 0
BA = 0
for s in S:
	ans += s.count('AB')
	if  s[0] == 'B' and s[-1] == 'A':
		BA += 1
		continue
	elif s[0] == 'B':
		B += 1
	elif s[-1] == 'A':
		A += 1

if A + B == 0:
	print(max(0,BA-1))
	sys.exit()
else:
	ans += BA
	ans += min(A,B)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N = int(sys.stdin.readline())
S = list(map(str,sys.stdin.read().rstrip().split("\n")))

ans = 0
A = 0
B = 0
BA = 0
for s in S:
	ans += s.count('AB')
	if  s[0] == 'B' and s[-1] == 'A':
		BA += 1
		continue
	elif s[0] == 'B':
		B += 1
	elif s[-1] == 'A':
		A += 1

if A + B == 0:
	print(BA-1)
else:
	ans += BA
	ans += min(A,B)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N = int(sys.stdin.readline())
S = list(map(str,sys.stdin.read().rstrip().split("\n")))

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

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N = int(sys.stdin.readline())
S = list(map(str,sys.stdin.read().rstrip().split("\n")))

ans = 0
A = 0
B = 0
BA = 0
for s in S:
	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 A + B == 0:
	print(BA-1)
	sys.exit()


if A+B >= BA:
	ans += BA
	BA = 0
else:
	s,t = divmod(BA,A+B)
	ans += s*(A+B)
	BA -= s*(A+B)

if A >= B:
	ans += B
	C = A - B
	if C == 0:
		print(ans)
		sys.exit()
else:
	ans += A
	C = B - A

if BA > 0:
	if C >= BA:
		ans += BA
	else:
		s,t = divmod(BA,C)
		ans += s*C
		ans += t-1


print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N = int(sys.stdin.readline())
S = list(map(str,sys.stdin.read().rstrip().split("\n")))

ans = 0
A = 0
B = 0
BA = 0
for s in S:
	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 A + B == 0:
	print(BA//2)
	sys.exit()


if A+B >= BA:
	ans += BA
	BA = 0
else:
	s,t = divmod(BA,A+B)
	ans += s*(A+B)
	BA -= s*(A+B)

if A >= B:
	ans += B
	C = A - B
	if C == 0:
		print(ans)
		sys.exit()
else:
	ans += A
	C = B - A

if BA > 0:
	if C >= BA:
		ans += BA
	else:
		s,t = divmod(BA,C)
		ans += s*C
		ans += t//2


print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N = int(sys.stdin.readline())
S = list(map(str,sys.stdin.read().rstrip().split("\n")))

ans = 0
A = 0
B = 0
BA = 0
for s in S:
	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 A+B >= BA:
	ans += BA
	BA = 0
else:
	ans += A+B
	BA -= A+B

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

if BA > 0:
	if C >= BA:
		ans += BA
	else:
		ans += C
		BA -= C
		ans += BA//2

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n=int(input())
data=[0]*2; f=1
for i in range(n):
    s=input()
    count+=s.count("AB")
    if s[0]=="B" and s[-1]=="A":
        data[0]+=1; data[1]+=1
    elif s[0]=="B":
        data[1]+=1; f=0
    elif s[-1]=="A":      
        data[0]+=1; f=0
count+=min(data)-f
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in range(n)]
ans = 0
w = 0
a = 0
b = 0

for c in s:
    ans += c.count("AB")
    if c[0] == "B" and c[-1] == "A":
        w += 1
    elif c[0] == "B":
        b += 1
    elif c[-1] == "A":
        a += 1

if a == b == 0:
    w -= 1
ans += min(w,0) + min(a,b)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in range(n)]
ans = 0
w = 0
a = 0
b = 0

for c in s:
    ans += c.count("AB")
    if c[0] == "B" and c[-1] == "A":
        w += 1
    elif c[0] == "B":
        b += 1
    elif c[-1] == "A":
        a += 1

if a == b == 0:
    w -= 1
ans += w + min(a,b)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in range(n)]
ans = 0
w = 0
a = 0
b = 0

for c in s:
    ans += c.count("AB")
    if c[0] == "B" and c[-1] == "A":
        w += 1
    elif c[0] == "B":
        b += 1
    elif c[-1] == "A":
        a += 1

ans += w + min(a,b)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
li = []
for i in range(n):
  s = list(input())
  li.append(s)
ans = 0
for i in range(n):
  for j in range(len(li[i])-1):
    if li[i][j] == "A" and li[i][j+1] == "B":
      ans += 1
x = 0
y = 0
for i in range(n):
  if li[i][0] == "B":
    x += 1
  if li[i][-1] == "A":
    y += 1
if x == y:
  print(ans + x -1)
else:
  print(ans + min(x,y))

Submission status: Wrong Answer

Solution code:
n = int(input())
li = []
for i in range(n):
  s = list(input())
  li.append(s)
ans = 0
for i in range(n):
  for j in range(len(li[i])-1):
    if li[i][j] == "A" and li[i][j+1] == "B":
      ans += 1
x = 0
y = 0
for i in range(n):
  if li[i][0] == "B":
    x += 1
  if li[i][-1] == "A":
    y += 1
print(ans + min(x,y))

Submission status: Wrong Answer

Solution code:
N = int(input())
 
ABcount = 0
Acount = 0
Bcount = 0
Aset = set()
Bset = set()
for i in range(N):
    s = input()
 
    if s[0] == "B":
        Bcount += 1
        Bset.add(s)
 
    if s[-1] == "A":
        Acount += 1
        Aset.add(s)
    
    buf = ""
    for char in s:
        if buf + char == "AB":
            ABcount += 1
        buf = char
    
if Acount > Bcount:
    print(ABcount + Bcount)
elif Acount < Bcount:
    print(ABcount + Acount)
else:
    if Aset == Bset:
        print(ABcount + Acount - 1)
    else:
        print(ABcount + Acount)

Submission status: Wrong Answer

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

ab_count = 0
for s in S:
    buf = ""
    for char in s:
        if buf + char == "AB":
            ab_count += 1
        buf = char

s_first_B = set()
s_last_A = set()

for s in S:
    if s[0] == "B":
        s_first_B.add(s)
    if s[-1] == "A":
        s_last_A.add(s)

new_ab_count = min(len(s_first_B), len(s_last_A))

if s_first_B == s_last_A:
    print(ab_count + new_ab_count - 1)
else:
    print(ab_count + new_ab_count)

Submission status: Wrong Answer

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

ab_count = 0
for s in S:
    buf = ""
    for char in s:
        if buf + char == "AB":
            ab_count += 1
        buf = char

s_first_B = set()
s_last_A = set()

for s in S:
    if s[0] == "B":
        s_first_B.add(s)
    if s[-1] == "A":
        s_last_A.add(s)

new_ab_count = min(len(s_first_B), len(s_last_A))

if len(s_last_A - s_first_B) == 0:
    print(ab_count + new_ab_count - 1)
else:
    print(ab_count + new_ab_count)

Submission status: Wrong Answer

Solution code:
n=int(input())

cnt_x=0
cnt_r_b=0
cnt_l_a=0
cnt_ab=0
for i in range(n):
 s=input()
 if s[0]=='B' and s[-1]=='A':
  cnt_x+=1
 if s[0]=='B':
  cnt_r_b+=1
 if s[-1]=='A':
  cnt_l_a+=1
 cnt_ab+=s.count('AB')

if cnt_x==cnt_r_b==cnt_l_a:
 print(max(cnt_ab+min(cnt_r_b,cnt_l_a)-1,0))
else:
 print(cnt_ab+min(cnt_r_b,cnt_l_a))

Submission status: Wrong Answer

Solution code:
n=int(input())

cnt_x=0
cnt_r_b=0
cnt_l_a=0
cnt_ab=0
for i in range(n):
 s=input()
 if s[0]=='B' and s[-1]=='A':
  cnt_x+=1
 if s[0]=='B':
  cnt_r_b+=1
 if s[-1]=='A':
  cnt_l_a+=1
 cnt_ab+=s.count('AB')

if cnt_r_b==cnt_l_a==0:
 print(0)
elif cnt_x==cnt_r_b==cnt_l_a:
 print(cnt_ab+min(cnt_r_b,cnt_l_a)-1)
else:
 print(cnt_ab+min(cnt_r_b,cnt_l_a))

Submission status: Wrong Answer

Solution code:
n=int(input())

cnt_x=0
cnt_r_b=0
cnt_l_a=0
cnt_ab=0
for i in range(n):
 s=input()
 if s[0]=='B' and s[-1]=='A':
  cnt_x+=1
 if s[0]=='B':
  cnt_r_b+=1
 if s[-1]=='A':
  cnt_l_a+=1
 cnt_ab+=s.count('AB')

if cnt_x==cnt_r_b==cnt_l_a:
 print(cnt_ab+min(cnt_r_b,cnt_l_a)-1)
else:
 print(cnt_ab+min(cnt_r_b,cnt_l_a))

Submission status: Wrong Answer

Solution code:
n=int(input())

cnt_x=0
cnt_r_b=0
cnt_l_a=0
cnt_ab=0
for i in range(n):
 s=input()
 if s[0]=='B' and s[-1]=='A':
  cnt_x+=1
 if s[0]=='B':
  cnt_r_b+=1
 if s[-1]=='A':
  cnt_l_a+=1
 cnt_ab+=s.count('AB')
if n==1:
 print(cnt_ab)
elif cnt_x==1 and cnt_r_b==1 and cnt_l_a==1:
 print(cnt_ab+min(cnt_r_b,cnt_l_a)-1)
else:
 print(cnt_ab+min(cnt_r_b,cnt_l_a))

Submission status: Wrong Answer

Solution code:
n=int(input())

cnt_r_b=0
cnt_l_a=0
cnt_ab=0
for i in range(n):
 s=input()
 if s[0]=='B':
  cnt_r_b+=1
 if s[-1]=='A':
  cnt_l_a+=1
 cnt_ab+=s.count('AB')
if n==1:
 print(cnt_ab)
else:
 print(cnt_ab+min(cnt_r_b,cnt_l_a))

Submission status: Wrong Answer

Solution code:
from math import ceil
n=int(input())
s=[input() for i in range(n)]
cba=0
ca=0
cb=0
ans=0
for i in range(n):
  if s[i][0]=='B':
    if s[i][len(s[i])-1]=='A':
      cba+=1
    else:
      cb+=1
  if s[i][0]!='B' and s[i][len(s[i])-1]=='A':
    ca+=1
  ans+=s[i].count('AB')
if cba>0:
  ans+=cba-1
  if cb>0 and ca>0:
    ans+=2+min(cb-1,ca-1)
  else:
    ans+=1
else:
  ans+=min(cb,ca)
print(ans)

Submission status: Wrong Answer

Solution code:
from _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor
# https://atcoder.jp/contests/arc067/tasks/arc067_a
# C - Factors of Factorial
"""
need to consider the case that ticket is not enough to lower everything
"""

N = gi()
ans = 0
cnt = [0] * 3
for i in range(N):
    s = gw()
    if s[0] == 'B':
        if s[-1] == 'A':
            cnt[0] += 1
        else:
            cnt[1] += 1
    elif s[-1] == 'A':
        cnt[2] += 1
    for j in range(len(s) - 1):
        if s[j] == 'A' and s[j + 1] == 'B':
            ans += 1

ans += min(cnt[1], cnt[2])
if (cnt[1] + cnt[2]):
    ans += cnt[0]
else:
    ans += cnt[0] - 1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
from math import factorial, ceil, floor
from collections import defaultdict
sys.setrecursionlimit(200000)
input = sys.stdin.readline

# N, M, = map(int, input().split())
# N = int(input())
# L = [int(v) for v in input().split()]
# L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
# S = input().strip()

N, = map(int, input().split())
L = [list(input().strip())  for _ in range(N)]

b = defaultdict(list)
a = defaultdict(list)
c = defaultdict(list)
for v in L:
    if v[0] == "B":
        if v[-1] == "A":
            c[v[0]].append("".join(v))
        else:
            b[v[0]].append("".join(v))
    else:
        a[v[-1]].append("".join(v))

s = ""
for v in a["A"]:
    s += v

    if len(c["B"]) > 0:
        p = c["B"][0]
        s += p
        c["B"].remove(p)

    if len(b["B"]) > 0:
        p = b["B"][0]
        s += p
        b["B"].remove(p)

for v in c["B"]:
    s += v

    if len(b["B"]) > 0:
        p = b["B"][0]
        s += p
        b["B"].remove(p)

for v in b["B"]:
    s += v

for k in a:
    if k == "A":
        continue
    for v in a[k]:
        s += v

print(s.count("AB"))