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())
L = []
for n in range(N):
    L.append(input())
AB = 0
A = 0
B = 0
C = 0
for n in range(N):
    if "AB" in L[n]:
        AB += 1
    if L[n][-1:] == "A":
        if L[n][0] == "B":
            C += 1
        else:
            A += 1
    elif L[n][0] == "B":
        B += 1
ans = 0

#print(A,B,C,AB)
if A != 0 and B != 0:
    ans = AB + C + min(A,B) 
elif A != 0 or B != 0:
    ans = AB + C + min(A,B)
elif A == 0 and B == 0:
    ans = AB + C - (C>0)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[]
for i in range(n):
    s.append(str(input()))
c1=0
c2=0
c3=0
c=0
for i in range(len(s)):
    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":
        if s[i][-1]=="A":
            c1+=1
        else:
            c2+=1
    else:
        if s[i][-1]=="A":
            c3+=1
print(c+min(c2,c3)+min(abs(c2-c3),c1))

Submission status: Wrong Answer

Solution code:
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=n-1

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())

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

count_ab = 0
for s in sub_strs:
    count_ab += s.count('AB')
    
count_ba = 0
count_xa = 0
count_bx = 0

for s in sub_strs:
    if s[0] == 'B' and s[-1] == 'A':
        count_ba += 1
    if s[0] != 'B' and s[-1] == 'A':
        count_xa += 1
    if s[0] == 'B' and s[-1] != 'A':
        count_bx += 1



ans = 0
ans += count_ab

# 前後にくっつける
tmp = min(min(count_xa, count_bx), count_ba)
ans += tmp * 2

# 更新
count_ba = count_ba - tmp
count_xa = count_xa - tmp
count_bx = count_bx - tmp


if count_ba > 0:
    ans += count_ba - 1

    if count_xa > 0 or count_bx > 0:
        ans += 1
else:
    ans += min(count_xa, count_bx)




print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
abcount = 0
bleft = 0
aright = 0
banda = 0
for i in range (1,N+1):
    S = input()
    abcount += S.count("AB")
    if S[0] == "B" and S[-1] != "A":
        bleft += 1
    if S[0] != "B" and S[-1] == "A":
        aright += 1
    if S[0] == "B" and S[-1] == "A":
        banda += 1
if(bleft + aright >0):
    print(abcount+banda+min(bleft,aright))
else:
    print(abcount+banda-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
ss = [input() for i in range(n)]
ans = 0
a,b,ab = 0,0,0
for s in ss:
    ans += s.count('AB')
    if s[0] is 'B' and s[-1] is 'A':
        ab += 1
    elif s[0] is 'B':
        a += 1
    elif s[-1] is 'A':
        b += 1
m = min(a,b)
if ab != 0 and a+b>0:
    print(ans+ab+m)
elif a+b == 0:
    print(ans+ab-1)
else:
    print(ans+m)

Submission status: Wrong Answer

Solution code:
n=int(input())
string_list=[input() for i in range(n)]

Axxx = [False]*n
xxxB = [False]*n
BxxA = [False]*n
cnt = 0
i = 0
for s in string_list:
    if s.endswith('A') and s.startswith('B'):
        BxxA[i] = True
    elif s.endswith('A'):
        Axxx[i] = True
    elif s.startswith('B'):
        xxxB[i] = True
    i += 1
    cnt += s.count('AB')
    
cnt += min(Axxx.count(True), xxxB.count(True))
nBxxA = BxxA.count(True)

if nBxxA > 1:
    cnt += nBxxA - 1
    
if Axxx.count(True) != xxxB.count(True) and nBxxA > 0:
    cnt += 1
        
print(cnt)

Submission status: Wrong Answer

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

countAB = 0
firstB = 0
lastA = 0
BXA = 0

for i in range(len(s)):
    if s[i][0] == 'B' and s[i][-1] == 'A':
        BXA = BXA + 1
    elif s[i][0] == 'B':
        firstB = firstB + 1
    elif s[i][-1] == 'A':
        lastA = lastA + 1
    for j in range(len(s[i]) - 1):
        if s[i][j] == 'A' and s[i][j + 1] == 'B':
            countAB = countAB + 1

if lastA == 0 and firstB == 0:
    print(countAB + BXA - 1)
elif firstB > lastA:
    print(countAB + lastA + BXA)
else:
    print(countAB + firstB +BXA)

Submission status: Wrong Answer

Solution code:
import re

n = int(input())

strs = []

patterna = re.compile(r"A$")
patternb = re.compile(r"B")
patternab = re.compile(r"AB")

a = 0
b = 0
a_b = 0
ab = 0

for i in range(n):
    str = input()
    if 'AB' in str:
        ab += 1
    if str.startswith('B') and str.endswith('A'):
        a_b += 1
        continue
    if str.endswith('A'):
        a += 1
    if str.startswith('B'):
        b += 1


ans = ab + min(a, b)

if a == 0 and b == 0 and a_b + 2:
    ans += a_b - 1
else:
    ans += a_b
    
print(ans)

Submission status: Wrong Answer

Solution code:
a=input()
a=int(a)
count=0
ap=0
bp=0
abc=0
for j in range(a):
    app=0
    bpp=0
    switch=0
    b=input()
    ofset=0
    for i in b:
        if i=="B" and ofset==0:
            bp+=1
            bpp+=1
        elif i=="A":
            switch+=1
        elif i=="B" and switch==1:
            count+=1
            switch=0
        else:
            switch=0
        ofset+=1
    if switch==1:
        ap+=1
        app=1
    if app==1 and bpp==1:
        abc+=1
        ap-=1
        bp-=1
count+=min(ap,bp)
if abc>0:
    count+=abc-1
    if ap>0 or bp>0:
        count+=1
print(count)

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/diverta2019/tasks/diverta2019_a
# -*- coding: utf-8 -*-

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

cnt = 0
A = 0
B = 0
AB = 0

for i in s:
        if "AB" in i:
                cnt += 1
        if i[len(i)-1] == "A" and i[0] != "B":
                A += 1
        if i[len(i)-1] != "A" and i[0] == "B":
                B += 1
        if i[len(i)-1] == "A" and i[0] == "B":
                AB += 1

ans = cnt
if AB is 0 and A>0 and B>0:
        ans += min(A,B)
if AB > 0:
        ans += AB-1
        if A>0:
                ans += 1
                A -=1
        if B>0:
                ans += 1
                B -=1
        
        if A>0 and B>0:
                ans += min(A,B)


print(ans)

Submission status: Wrong Answer

Solution code:
o=int(input())
a=[]
for i in range(o):
    a+=input().split()
s="A".join(a)
s1=s.count("AB")#末尾がBの数
y="B".join(a)
y1=y.count("BA")#末尾がAの数
p="".join(a)
p1=p.count("AB")
l=s1-p1
m=y1-p1
print(max(l,m)+p1)

Submission status: Wrong Answer

Solution code:
n = int(input())
S = []
result = 0
A_count = 0
B_count = 0
BA_count = 0

for i in range(n):
    Si = str(input())
    S.append(Si)

for i in range(n):
    Si = S[i]
    result += Si.count("AB")
    if Si[0] == "B":
        B_count += 1
    if Si[-1] == "A":
        A_count += 1

result += min(A_count, B_count)
print(result)

Submission status: Wrong Answer

Solution code:
n = int(input())

s = []
k = []
ans = 0
left = 0
right = 0
for i in range(n):
    s.append(list(input()))
    k.append("".join(s[i]))


for i in range(n):      # その文字列に何個ABがあるか
    if "AB" in str(k[i]):
        for j in range(len(s[i])-1):
            if s[i][j] == "A" and s[i][j+1] == "B":
                ans += 1

    if s[i][0] == "B":
        right += 1
    if s[i][-1] == "A":
        left += 1

ans += min(left,right)

print(ans)

Submission status: Wrong Answer

Solution code:
# N < 10000
N = int(input())

# len(s) < 10

count = 0
a, b, ba = 0, 0, 0
for i in range(N):
    s = input().rstrip()
    count += s.count("AB")

    if s[-1] == 'A':
        if s[0] == 'B':
            ba += 1
        else:
            a += 1
    elif s[0] == 'B':
        b += 1

#e = int(input())
#print("*A=",a, ", B*=",b, "B*A=",ba, ",AB=", count, ",expected=", e)

if ba > 1:
    count += ba - 1
    ba = 1

if a+b>0:
    count+=max(min(a+ba,b),min (a,b+ba))
print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())  #数値入力

list_s = []
for i in range(n):
    list_s.append(input())

sum_ab = 0
sum_ab += sum([x.count('AB') for x in list_s])

ba = 0
bx = 0
xa = 0
for x in list_s:
    if (x[0] == 'B') & (x[-1] == 'A'):
        ba += 1
    elif x[0] == 'B':
        bx += 1
    elif x[-1] == 'A':
        xa += 1
        
# print(ba, bx, xa)

if ba > 1:
    sum_ab += ba - 1

sum_ab += min(bx, xa)

if (ba > 0) & (bx != xa):
    sum_ab += 1

print(sum_ab)

Submission status: Wrong Answer

Solution code:
import sys

N = int(sys.stdin.readline())

s = []

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

count = 0

for i in range(N):
    count += s[i].count('AB')

pattern_1 = []
pattern_2 = []
pattern_3 = []
for i in range(N):
    if s[i][:1] == 'B' and s[i][-1:] == 'A':
        pattern_3.append(i)
    elif s[i][:1] == 'B':
        pattern_2.append(i)
    elif s[i][-1:] == 'A':
        pattern_1.append(i)

len_1 = len(pattern_1)
len_2 = len(pattern_2)
len_3 = len(pattern_3)

count += min(len_1, len_2)


if len_1 > len_2:
    count += len_3
elif len_2 > len_1:
    count += len_3 - 1
elif len_3 > 0:
    count += len_3 - 1

print(count)

Submission status: Wrong Answer

Solution code:
import re
N = int(input())
s=[input() for _ in range(N)]

counter = 0
for n in range(N):
    counter += len(re.findall('AB', s[n]))
lb_ra = 0
lb = 0
ra = 0
frag = False
if N>1:

    for n in range(N):
        l, r = s[n][0]=='B', s[n][-1]=='A'
        if l and r:
            lb_ra += 1
        elif l:
            lb += 1
        elif r:
            ra +=1

    if ra>lb_ra:
        counter+=lb_ra
        ra = ra
        lb_ra = 0
        frag=True
    else:
        counter+=ra
        ra = ra
        lb_ra = lb_ra-ra
        frag=True
    
    if lb>lb_ra:
        counter+=lb_ra
        lb = lb
        lb_ra=0
        frag=True
    else:
        counter+=lb
        lb = lb
        lb_ra=lb_ra-lb
        frag=True
    counter+= min(lb,ra)
    
    if frag:
        counter+=lb_ra
        
print(counter)

Submission status: Wrong Answer

Solution code:
import re
aflag = 0
bflag = 0
cnt = 0
N = int(input())
for i in range(N):
    s = input()
    if s[-1:] == "A":
        aflag += 1
    if s[:1] == "B":
        bflag += 1
    if re.search("AB", s):
        cnt += 1
    if re.search("AB.*AB", s):
        cnt += 1
    if re.search("AB.*AB.*AB", s):
        cnt += 1
    if re.search("AB.*AB.*AB.*AB", s):
        cnt += 1
    if re.search("AB.*AB.*AB.*AB.*AB", s):
        cnt += 1

cnt += min(aflag, bflag)
print(cnt)

Submission status: Wrong Answer

Solution code:
import os, sys, re, math

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

cnt = 0
lastA = 0
firstB = 0
lAandfB = 0
for s in strs:
    cnt += s.count('AB')

    if s[0] == 'B' and s[-1] == 'A':
        lAandfB += 1
    elif s[-1] == 'A':
        lastA += 1
    elif s[0] == 'B':
        firstB += 1

#print('cnt = ' + str(cnt))
#print('lastA = ' + str(lastA))
#print('firstB = ' + str(firstB))
#print('lAandfB = ' + str(lAandfB))
cnt += min(lastA, firstB)
diff = max(lastA,firstB) - min(lastA, firstB)
if diff >= 0 and lAandfB >= diff:
    cnt += lAandfB
elif diff == 0 and lAandfB > 1:
    cnt += lAandfB

print(cnt)

Submission status: Wrong Answer

Solution code:

N = int(input())
A=[]
for i in range(N):
    A.append(input())
X=0
W=0
for i in A:
    i="".join(i)
    Q=i.count("AB")
    if(Q==1):
        W+=1

#B="".join(A)
#print(B)
#Y=B.count("AB")

#print(Y)
Z1=0


Z3=0
z=0
for i in A:

    #print(i[-1])
    i="".join(i)
    C=A.index(i)
    if(C!=0):
        z+=1
        C=C-z
    D=A[C]
    #print(D)#取り出したやつ

    A.pop(C)
    #print(A)

    #print(C)
    #print(A)
    Z1=0
    if(i[-1]=="A"):
        for j in A:

            if(j[0]=="B"):
                Z1+=1
        #        print(Z1)
    A.append(D)
    #print(A)
    #print(A)
    if(Z3<Z1):
        Z3=Z1



W+=Z3
print(W)

Submission status: Wrong Answer

Solution code:
n = int(input())
S = []
for i in range(n):
    s = str(input())
    S.append(s)
    
ans = 0
countA = 0
countB = 0
countBA = 0
for s in S:
    if s[0] == 'B' and s[len(s)-1] == 'A':
        countBA += 1
        
    elif s[len(s)-1] == 'A':
        countA += 1
        
    elif s[0] == 'B':
        countB += 1
        
    for i in range(len(s) - 1):
        if s[i] == 'A' and s[i+1] == 'B':
            ans += 1
            
ans += countBA -1
if countA > 0:
    ans += 1
    countA -= 1
if countB > 0:
    ans += 1
    countB -= 1
ans += min(countA, countB)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
s_list = []
for n in range(N):
    s = input()
    s_list.append(s)

count = 0
A_end = 0
B_start = 0

for s in s_list:
    if 'AB' in s:
        d = s.count('AB')
        count = count + d
    A_index = s.rfind('A')
    if A_index == len(s) - 1:
        print(s)
        A_end = A_end + 1
    B_index = s.find('B')
    if B_index == 0:
        print(s)
        B_start = B_start + 1

c = min(A_end, B_start)
print(count + c)

Submission status: Wrong Answer

Solution code:
N = int(input())
ab_list = []
for _ in range(N):
    ab_list.append(input())

a_cnt = 0
b_cnt = 0
in_cnt = 0

for tmp_list in ab_list:
    #print(tmp_list)
    if tmp_list.endswith('A'):
        a_cnt += 1
    if tmp_list.startswith('B'):
        b_cnt += 1
    in_cnt += tmp_list.count('AB')
        
# Final answer
print(min(a_cnt, b_cnt) + in_cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
si = [input() for _ in range(N)]
count = 0
A_ = 0
B_ = 0
if N !=1:
    for s in si:
        if s[-1] =="A":
            A_ +=1
        if s[0] =="B":
            B_ +=1
    for i in range(len(s)-1):
        if s[i] =="A" and s[i+1] == "B":
            count += 1
count += min(A_, B_,N-1)       

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
strs=[]
ind_a=[]
ind_b=[]
ind_both=[]
num=0
for i in range(n):
   str=input()
   num=num+str.count("AB")
   if str[len(str)-1]=="A":
       if str[0]=="B":
           ind_both.append(i)
       else:
           ind_a.append(i)
   elif str[0]=="B":
       ind_b.append(i)
big=0
small=0
both=len(ind_both)
if len(ind_a) > len(ind_b):
    big=len(ind_a)
    small=len(ind_b)
else:
    big=len(ind_b)
    small=len(ind_a)
if (big-small)>=both:
    num=num+small+both
else:
    both=both-(big-small)+1
    num=num+big+int(both/2)
print(num)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int( input() )

for i in range( 0, n ):
	m =  input() 
	print(m)
nm=0
nm1=0
cnt=m
for i in cnt:
      if i=='A' or i=='B':
              nm+=1
              nm1=max(nm1,nm)
      else:
              nm=0
print(nm1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a,b,ab,pure = 0,0,0,0
for i in range(n):
    s = input()
    pure += s.count("AB")
    if s.startswith("B"): b += 1
    if s.endswith("A"): a += 1
    if s.startswith("B") and s.endswith("A"): ab += 1

if a==ab or b==ab: ans = pure + ab - 1
else: ans = pure + min(a,b)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    ans = 0
    xb = 0
    ax = 0
    ba = 0
    for _ in range(N):
        s = str(input().strip())
        for i in range(len(s)):
            if s[i:i+2] == 'AB':
                ans += 1

        if s[0] == 'B' and s[-1] == 'A':
            ba += 1
        elif s[0] == 'B':
            xb += 1
        elif s[-1] == 'A':
            ax += 1
    print(ans, xb, ax, ba)
    x, y = min(xb, ax), max(xb, ax)
    ans += x

    if y > 0:
        while ba >= y:
            ans += y
            ba -= y

    ans += ba // 2

    return ans


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

Submission status: Wrong Answer

Solution code:

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

lastA=0
startB=0
B_A =0
AB = 0
#sをloop

for i in range(N):
  if s[i][0]=='B':
    startB+=1
    if s[i][-1]=='A':
      B_A +=1
  if s[i][-1]=='A':
    lastA+=1
  AB += s[i].count('AB')
ans=0
if startB==0:
    ans=AB+B_A
if lastA==0:
    ans=AB+B_A
if startB==0 & lastA==0:
    ans=AB+B_A-1
if startB!=0 & lastA!=0:
    ans=AB+min(startB,lastA)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = []

A = 0
B = 0
AB = 0

cnt = 0 

for _ in range(N):
    Si = input()
    cnt += Si.count('AB')
    if Si[0] == 'B' and Si[-1] == 'A':
        AB += 1
    elif Si[0] == 'B':
        B += 1
    elif Si[-1] == 'A':
        A += 1

if AB > 0:
    cnt += AB -1
    A += 1
    B += 1
    if A > 0:
        cnt += 1
        A -= 1
    if B > 0:
        cnt += 1
        B -= 1
    if A > B :
        cnt += B
    else:
        cnt += A
else: 
    if A > B :
        cnt += B
    else:
        cnt += A
print(cnt)

Submission status: Wrong Answer

Solution code:
c = int(input())
res = 0
a = 0
b = 0
lines = []
for i in range(c):
    s = input()
    res += s.count('AB')
    b += 1 if s.startswith('B') else 0
    a += 1 if s.endswith('A') else 0

if a == b:
    res -= 1

res = res + min(a, b)
print(res)

Submission status: Wrong Answer

Solution code:
n=int(input())
c1=c2=c3=0
c=0
for i in range(n):
  s=input()
  c+=s.count('AB')

  if s[0]=='B' and s[-1]=='A':
    c3+=1
  elif s[0]=='B':
    c1+=1
  elif s[-1]=='A':
    c2+=1

cc=c
c11=c1
c22=c2
c33=c3
cc+=max(0,c33-1)
if c11<c22:
  c22-=c1
  cc+=c11
  if c22>0 and c33>0:
    cc+=1
elif c11==c22:
  cc+=c11
else:
  c11-=c22
  cc+=c22
  if c11>0 and c33>0:
    cc+=1

if c3>max(c1,c2):
  if c1<c2:
    c+=c1*2
    c3-=c1
    c+=c3
  elif c1==c2:
    c+=c1*2
    c3-=c1
    c+=max(0,c3-1)
  else:
    c+=c2*2
    c3-=c2
    c+=c3
elif c3>=min(c1,c2) and c3<=max(c1,c2):
  if c1<c2:
    c+=c1*2
    c2-=c1
    c3-=c1
    c+=c3
  else:
    c+=c2*2
    c1-=c2
    c3-=c2
    c+=c3
else:
  c+=c3*2
  c1-=c3
  c2-=c3
  c+=min(c1,c2)

print(max(c,cc))

Submission status: Wrong Answer

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

count = 0
for str in s:
    count += str.count("AB")
    
first = 0
end = 0
tup = []

for i in range(n):
    str = s[i]
    if str[0] in "B":
        first += 1
        tup.append(i)
        
        
for i in range(n):
    str = s[i]
    if i in tup:
        continue
    elif str[-1] in "A":
        end += 1
        
count += first * end

print(count)

Submission status: Wrong Answer

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

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

print(ans + max(a, b))

Submission status: Wrong Answer

Solution code:
N = int(input())
inside_str = 0
tail_a = 0
head_b = 0

doubleAB = 0

for i in range(N):
  str = input()
  inside_str = inside_str + str.count('AB')
  
  if (str[-1] == 'A'):
    tail_a = tail_a + 1
  if (str[0] == 'B'):
    head_b = head_b + 1
  if(str[-1] == 'A' and str[0] == 'B'):
    doubleAB = doubleAB + 1

total_cand = inside_str + min(tail_a, head_b)

if(tail_a > head_b):
  total_cand = total_cand + min(doubleAB, tail_a - head_b)
else:
  total_cand = total_cand + min(doubleAB, head_b - tail_a)

print(total_cand)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=0
B=0
A=0
count=0
for i in range(N):
    S=input()
    a+=S.count("AB")
    if S[0]=="B"and S[len(S)-1]=="A":
        a+=1
    elif S[0]=="B":
        B+=1
        count+=1
    elif S[0]=="B":
        A+=1
        count+=1
if count==0:
    a=a-1
C=min(A,B)
D=min(C,N-1)
print(a+D)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = []
for _ in range(N):
    s.append(input())
import copy
S = copy.deepcopy(s)
both = []
A_tail = []
B_head = []
other = []
for text in S:
    if text[0] == "B" and text[-1] == "A":
        both.append(text)
    elif text[0] == "B":
        B_head.append(text)
    elif text[-1] == "A":
        A_tail.append(text)
    else:
        other.append(text)
concat = []
for both_text in both:
    concat.extend(both_text)
a_idx = []
for idx,A_tail_text in enumerate(A_tail):
    if len(B_head) >= 1:
        concat.extend(A_tail_text)
        concat.extend(B_head[0])
        B_head.pop(0)
    else:
        a_idx.append(idx)
a_rest = []
for idx in a_idx:
    a_rest.append(A_tail[idx])
cnt = 0
for i in range(len(concat)-1):
    if concat[i:i+2] == ["A","B"]:
        cnt += 1
for oth in other:
    for i in range(len(oth)-1):
        if oth[i:i+2] == "AB":
            cnt += 1
for oth in a_rest:
    for i in range(len(oth)-1):
        if oth[i:i+2] == "AB":
            cnt += 1
for oth in B_head:
    for i in range(len(oth)-1):
        if oth[i:i+2] == "AB":
            cnt += 1
print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())

cnt = 0
a_cnt = 0
b_cnt = 0
ab_cnt = 0
for i in range(n):
  s = input()
  for l in range(len(s)-1):
    if s[l] == 'A' and s[l+1] == 'B':
      cnt = cnt +1
  if s[-1] == 'A':
    a_cnt = a_cnt +1
  if s[0] == 'B':
    b_cnt = b_cnt +1
  if s[-1] == 'A' and s[0] == 'B':
    ab_cnt = ab_cnt +1

tmp = min(a_cnt, b_cnt)
if a_cnt == ab_cnt and b_cnt == ab_cnt:
  tmp = tmp - 1
#print (a_cnt)
#print (b_cnt)
#print (ab_cnt)
#print (cnt)
print(cnt + tmp)

Submission status: Wrong Answer

Solution code:
N = int(input())

ans = 0
ra = 0
lb = 0
ba = 0
not_ra_nor_lb = 0

for _ in range(N):
    s = input()
    ans += s.count("AB")
    if s[0] == "B":
        lb += 1
    if s[-1] == "A":
        ra += 1
    if (s[0] == "B") & (s[-1] == "A"):
        ba += 1
    if (s[0] != "B") & (s[-1] != "A"):
        not_ra_nor_lb += 1

if (ra == lb) & (ba >= 1):
    ans += ra - 1
else:
    ans += min(ra, lb)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
s_list = [input() for _ in range(N)]
end_a_num = 0
start_b_num = 0
same_num = 0
ans = 0
for s in s_list:
    ab_num = s.count('AB')
    ans += ab_num
    if s[-1] == 'A':
        end_a_num+=1
    if s[0]=='B':
        start_b_num+=1
    if s[-1]=='A' and s[0]=='B':
        same_num+=1


ans+= min(end_a_num,start_b_num)
if end_a_num==start_b_num:
    if end_a_num==same_num:
        ans-=1    

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[]
for i in range(n):
    s.append(str(input()))
d=0
freea=0
freeb=0
for i in range(n):
    c=0
    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":
        if s[i][-1]=="A":
            if freeb>0:
                d = d+c+1
            elif freea>0:
                d=d+c+1
            else:
                d=d+c
                freea+=1
                freeb+=1
        else:
            if freea>0:
                d=d+c+1
                freea = freea -1
            else:
                d=d+c
                freeb = freeb +1
    else:
        if s[i][-1]=="A":
            if freeb>0:
                d=d+c+1
                freeb=freeb-1
            else:
                d=d+c
                freea=freea+1
        else:
            d=d+c
print(d)

Submission status: Wrong Answer

Solution code:
N = int(input())
List = [input() for _ in range(N)]

cnt_ab = 0
cnt_a = 0
cnt_b = 0
cnt_ba = 0

for i in range(N):
    cnt_ab += List[i].count("AB")
for j in range(N):
    if (List[j][-1]=="A")&(List[j][0]!="B"):
        cnt_a+=1
for k in range(N):
    if (List[k][0]=="B")&(List[k][-1]!="A"):
        cnt_b +=1
for l in range(N):
    if (List[l][-1]=="A")&(List[l][0]=="B"):
        cnt_ba+=1

if (abs(cnt_a-cnt_b)+1>cnt_ba):
    cnt = cnt_ab+min(cnt_a, cnt_b)+cnt_ba
    print(cnt)
else:
    cnt = cnt_ab+min(cnt_a, cnt_b)+cnt_ba-1
    print(cnt)

Submission status: Wrong Answer

Solution code:
N=int(input())
countab,counta,countb=0,0,0
for i in range(N):
	s=input()
	if (s[0]=='B')&(s[-1]!='A'):
		countb+=1
	if (s[-1]=='A')&(s[0]!='B'):
		counta+=1
	countab+=s.count('AB')
print(min(counta,countb)+countab)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
# S = ["ABCA", "XBAZ", "BAD"]
# S = ["BEWPVCRWH", "ZZNQYIJX", "BAVREA", "PA", "HJMYITEOX", "BCJHMRMNK", "BP", "QVFABZ", "PRGKSPUNA"]
# S = ["RABYBBE","JOZ","BMHQUVA","BPA","ISU","MCMABAOBHZ","SZMEHMA"]


def calc_maximum_AB_sub_string(S):
    base_AB_counts = map(lambda x : x.count("AB"), S)
    base = sum(base_AB_counts)

    clipped = list(map(lambda x: x[0] + x[-1], S))
    bonus1 = len(list(filter(lambda x: x == 'BA', clipped)))
    filtered = list(filter(lambda x: x != 'BA', clipped))
    bonus2 = sum(map(lambda x: 1 if x[1] == 'A' else 0, filtered))
    bonus3 = sum(map(lambda x: 1 if x[0] == 'B' else 0, filtered))

    return base + max(bonus1 - 1, 0) + min(bonus2, bonus3) + (1 if bonus2 != bonus3 else 0)


if __name__ == "__main__":
    print(calc_maximum_AB_sub_string(S))

Submission status: Wrong Answer

Solution code:
# C

N = int(input())
cnt = 0
s = []
both = []
a_ = []
b_ = []
for i in range(N):
    s_i = input()
    if 'AB' in s_i:
        cnt+= 1

    if s_i.startswith('B') and s_i.endswith('A'):
        both.append(s_i)
    elif s_i.startswith('B'):
        b_.append(s_i)
    elif s_i.endswith('A'):
        a_.append(s_i)
    else:
        s.append(s_i)
        
if len(both):
    cnt += len(both)-1
    if len(a_):
        cnt += 1
    if len(b_):
        cnt += 1

if len(a_) < len(b_):
    cnt += len(a_)
else:
    cnt += len(b_)

if len(both):
    cnt -= 1
    
print(cnt)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
s1 = np.zeros(N)
s2 = np.zeros(N)
s3 = np.zeros(N)
co = 0
for i in range(N):
    si = str(input()).split()[0]
    #print("test",si,si[0][0],si[0][-1])
    s1[i] = (si[0]=='B')
    s2[i] = (si[-1]=='A')
    s3[i] = (s1[i]==1 and s2[i] ==1)
    co += si.count('AB')

as1 = np.sum(s1)
if as1 == N:
    as1 = N-1
as2 = np.sum(s2)
if as2 == N:
    as2 = N-1

if (np.sum(s3) == N-1 ):
    as1 = N-2
    as2 = N-2


Ans = int(co + min(as1,as2))
print (Ans)

Submission status: Wrong Answer

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

acount = 0
bcount = 0
abcount = 0
ans = 0

for i in range(len(s)):
    if s[i][0] == 'B':
        bcount += 1
    if s[i][-1] == 'A':
        acount += 1
    if s[i][0] == 'B' and s[i][-1] == 'A':
        abcount += 1
    for j in range(len(s[i])-1):
        if s[i][j] == 'A' and s[i][j+1] == 'B':
            ans += 1

if len(s) == 1:
    print(ans)
elif max(abcount,acount,bcount) == abcount:
    print(ans + min(acount,bcount) - 1)
else:
    print(ans + min(acount,bcount))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, s: "List[str]"):
    x = 0
    a = 0
    b = 0
    for i in s:
        if(i[0] == 'B' and i[-1] == 'A'):
            x += 1
        elif(i[-1] == 'A'):
            a += 1
        elif(i[0] == 'B'):
            b += 1

    # ans1 = max(0,x-1) + (min(a,b) if a==b else min(a,b)+1)    
    ans1 = max(0,x-1) 
    if(a == b):
        ans1 += min(a,b)
    else:
        if(x == 0):
            ans1 += min(a,b)
        else:
            ans1 += min(a,b)+1 

    ans2 = 0
    for i in s:
        ans2 += i.count('AB')

    print(ans1+ans2)
    
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    s = [ next(tokens) for _ in range(N) ]  # type: "List[str]"
    solve(N, s)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
S_tail_a_head_b = []
S_tail_a = []
S_head_b = []
S_ab = []
for _ in range(N):
    s = input()
    if s.find('AB') == -1 and s[0] != 'B' and s[-1] != 'A':
        continue
    elif s[0] == 'B' and s[-1] == 'A':
        S_tail_a_head_b.append(s)
    elif s[0] == 'B':
        S_head_b.append(s)
    elif s[-1] == 'A':
        S_tail_a.append(s)
    else:
        S_ab.append(s)

count = 0
for S_i in S_tail_a_head_b + S_tail_a + S_head_b + S_ab:
    count += S_i.count('AB')

count += min(len(S_tail_a), len(S_head_b))

if len(S_tail_a_head_b) > 0 and len(S_tail_a) != len(S_head_b):
    count += 1

count += len(S_tail_a_head_b) - 1

print(count)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

N = int(input().rstrip())

ba = []
headb = []
taila = []

cnt = 0

for _ in range(N):
    s = input().rstrip()
    
    if "AB" in s:
        cnt += 1
    
    if s[0] == "B" and s[-1] == "A":
        ba.append(s)
        continue
    
    if s[0] == "B":
        headb.append(s)
    
    if s[-1] == "A":
        taila.append(s)

n_ba = len(ba)
n_headb = len(headb)
n_taila = len(taila)

#print("ba {}, headb {}, taila {}".format(n_ba, n_headb, n_taila))

cnt += (n_ba+1) + min(n_headb-1, n_taila-1)


print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
S = [input() for _ in range(n)]
cnt = 0
first_b_cnt = 0
last_a_cnt = 0
b_a_cnt = 0
for s in S:
    idx = len(s) - 1
    for i in range(idx - 1):
        if s[i] == 'A' and s[i + 1] == 'B':
            cnt += 1
    if s[idx] == 'A':
        last_a_cnt += 1
    if s[0] == 'B':
        first_b_cnt += 1
    if s[idx] == 'A' and s[0] == 'B':
        b_a_cnt +=1


min_cnt = min(first_b_cnt, last_a_cnt)
min_cnt = min_cnt - 1 if b_a_cnt == (first_b_cnt + last_a_cnt) / 2 else min_cnt
print(cnt + min_cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
cnt1 = 0
cnt2 = 0
cnt3 = 0

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

print(cnt1 + min(cnt2, cnt3) - 1 if (cnt2 == cnt3 == N) else cnt1 + min(cnt2, cnt3))

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[input() for i in range(n)]
ans=sum([s[i].count("AB") for i in range(n)])
xxA=sum([1 if s[i][0]!="B" and s[i][-1]=="A" else 0 for i in range(n)])
Bxx=sum([1 if s[i][0]=="B" and s[i][-1]!="A" else 0 for i in range(n)])
BxA=sum([1 if s[i][0]=="B" and s[i][-1]=="A" else 0 for i in range(n)])
if xxA==Bxx:
    wer=xxA+BxA-1
else:    
    wer=min(xxA,Bxx)+BxA
print(ans+wer)