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:
def main():
    N = int(input())
    s = [input() for i in range(N)]
    Acnt = 0
    Bcnt = 0
    ABcnt = 0
    for i in range(N):
        if s[i][-1] == 'A':
            Acnt += 1
        if s[i][0] == 'B':
            Bcnt += 1
        index = -1
        while True:
            index = s[i].find('AB', index + 1)
            if index == -1:
                break
            ABcnt += 1
    if Acnt == N:
        Acnt -= 1
    if Bcnt == N:
        Bcnt -= 1
    print(ABcnt + min(Acnt, Bcnt))


if __name__ == '__main__':
    main()

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 
elif A == 0 and B == 0:
    ans = AB + C - (C>0)
print(ans)

Submission status: Wrong Answer

Solution code:
N  = int(input())
ansMin = 1000000000
ans = 0
acount1 = 0
bcount1 = 0
acount2 = 0
bcount2 = 0
comn1 = 0
comn2 = 0
for i in range(N):
    tmp = input()
    ans += tmp.count('AB')
    if tmp[0] == 'B' and tmp[len(tmp) - 1] == 'A':
        comn1 = 1
    if tmp[0] == 'B':
        bcount1 += 1
    if tmp[len(tmp) - 1] == 'A':
        acount1 += 1

    if tmp[0] == 'A' and tmp[len(tmp) - 1] == 'B':
        comn2 = 1
    if tmp[0] == 'A':
        acount2 += 1
    if tmp[len(tmp) - 1] == 'B':
        bcount2 += 1

tmp1 = max(acount1, bcount1) - comn1
tmp11 = min(tmp1, min(acount1, bcount1))
tmp2 = max(acount2, bcount2) - comn2
tmp22 = min(tmp2, min(acount2, bcount2))

ans += max(tmp11, tmp22)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = []
ABcnt = 0
topABcnt = 0
topBbtmA = 0
topBbtmNO = 0
topNObtmA = 0
for _ in range(N):
  s = input()
  nowABcnt = s.count('AB')
  ABcnt += nowABcnt
  if s[0:2]=='AB':
    topABcnt += 1
  elif s[0]=='B':
    if s[-1]=='A': topBbtmA += 1
    else: topBbtmNO += 1
  elif s[-1]=='A':
    topNObtmA += 1
  if nowABcnt!=0 or s[0]=='B' or s[-1]=='A': S.append(s)
    
ans = 0


ans += min(topBbtmNO, topNObtmA)
firstSurvivorCnt = max(topBbtmNO, topNObtmA) - ans

nikaisenGet = min(firstSurvivorCnt, topBbtmA)
ans += nikaisenGet

topBbtmA -= nikaisenGet
ans += topBbtmA #topBbtmAが生き残ってたらその分足す

print(ans+ABcnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
a,b=0,0
res = 0
n1 = n
while n != 0:
    s = input()
    if s[len(s)-1] == 'A':
        a+=1
    if s[0] == 'B':
        b+=1
    res+=s.count('AB')
    n-=1
if a==b:
    if a>=n1:
        print(res+(a+b)//2 -1)
    else:
        print(res+a)
elif a>b:
    print(res+b)
else:
    print(res+a)

Submission status: Wrong Answer

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

top = [0 for _ in range(N)]
last = [0 for _ in range(N)]

res = 0
n = 0
for s in S:
    for i in range(len(s)-1):
        if s[i] == 'A' and s[i+1] == 'B':
            res += 1
            
    if s[0] == 'B':
        top[n] = 1
        
    if s[-1] == 'A':
        last[n] = 1
        
    n += 1
 
d = 0
t = 0
l = 0
for i in range(N):
    if top[i] == 1 and last[i] == 1:
        d += 1
    elif top[i] == 1:
        t += 1
    elif last[i] == 1:
        l += 1
     

if d >= 1:
    res += d - 1



if d > 0 and min(t, l) >= 1:
    res += min(t-1, l-1)
    res += 2
    
elif d > 0 and min(t, l) < 1:
    res += 1
    
else:
    res += min(t, l)
    
    
print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())
 
moji_list = []
A_count = 0
B_count = 0
AB_count = 0

if N == 1:
    moji = input()
    print(moji.count('AB'))

else:
    for i in range(N):
        moji = input()
        if moji[0] == 'B':
            B_count += 1
        if moji[-1] == 'A':
            A_count += 1
        
        AB_count += moji.count('AB')

    if A_count == B_count and A_count != 0:
        print(AB_count+B_count-1)
    
    elif A_count >= B_count:
        print(AB_count+B_count)
    else:
        print(AB_count+A_count)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = [input() for _ in range(N)]
numAB = 0
numBonly = 0
numAonly = 0
numAandB = 0
 
for i in s:
    numAB += i.count("AB")
    if i[0] == 'B' and i[-1] != 'A':
        numBonly += 1
    elif i[0] != 'B' and i[-1] == 'A':
        numAonly += 1
    elif i[0] == 'B' and i[-1] == 'A':
        numAandB += 1
 
if numAonly == numBonly:
    if numAandB <= numAonly:
        print(numAB + numAandB + numAonly)
    else:
        print(numAB + 2*numAonly + (numAandB - numAonly - 1))
else:
    print(numAB + min(numAonly, numBonly) + numAandB)

Submission status: Wrong Answer

Solution code:
N = int(input())

BA = -1
ar = 0
bl = 0
ans = 0
for i in range(N):
    s = input()
    ans += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        BA += 1
    else:
        if bl == 0 and ar == 0:
            if s[0] == 'B':
                bl += 1
            elif s[-1] == 'A':
                ar += 1
        elif bl and ar == 0:
            if s[-1] == 'A':
                ans += 1
                bl -= 1
            elif s[0] == 'B':
                bl += 1
        elif bl == 0 and ar:
            if s[0] == 'B':
                ans += 1
                ar -= 1
            elif s[-1] == 'A':
                ar += 1
        else:
            if s[-1] == 'A':
                ans += 1
                bl -= 1
            elif s[0] == 'B':
                ans += 1
                ar -= 1

if BA >= 0:
    ans += BA
    if bl:
        ans += 1
    if ar:
        ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
lasta=0
headb=0
spec=0
cnt=0
for _ in range(N):
    s=str(input())
    for i in range(len(s)-1):
        if s[i:i+2]=="AB":
            cnt+=1
    if s[0]=="B" and s[-1]=="A":
        spec+=1
    elif s[-1]=="A":
        lasta+=1
    elif s[0]=="B":
        headb+=1
if abs(headb-lasta)>=spec:
    print(min(headb,lasta)+spec+cnt)
else:
    print(spec-abs(headb-lasta)+max(headb,lasta)+cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
eni_B = 0
end_A = 0
both = 0
count = 0

for i in range(N):
    s = list(input())
    if s[0] == 'B':
        eni_B += 1
    if s[-1] == 'A':
        end_A += 1
    if s[0] == 'B' and s[-1] == 'A':
        eni_B -= 1
        end_A -= 1
        both += 1
    for j in range(len(s)-1):
        if s[j] == 'A':
            if s[j+1] == 'B':
                count += 1

m = min(end_A,eni_B)
M = max(end_A,eni_B)

if M == m:
    if both > 1:
        count = count + m + both - 1
    else:
        count = count + m
else:
    count = count + m + both

print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())

A=[]
for i in range(N):
    A.append(list(input()))
    
ans=0
flaga=0

for i in range(N):
    a=A[i].count("A")
    b=A[i].count("B")
    x=min(a,b)
    ans+=x
    if b-x>0 and flaga==1:
        ans+=1
        
    if a-x>0:
        flaga=1
    else:
        flaga=0
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

print(xABx + min(Bxxx, xxxA, N - 1))

Submission status: Wrong Answer

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

contained = sum([s.count('AB') for s in ss])
B_hoge = sum([1 for s in ss if s[0]=='B'])
hoge_A = sum([1 for s in ss if s[-1]=='A'])
B_hoge_A = sum([1 for s in ss if s[0]=='B' and s[-1]=='A'])

B_hoge -= B_hoge_A
hoge_A -= B_hoge_A
gap = abs(B_hoge - hoge_A)
# 差がB_hoge_Aより大きい場合、可能な限り差を埋める
ans = contained
if gap >= B_hoge_A:
    ans += min(B_hoge,hoge_A) + B_hoge_A
#　差がB_hoge_Aより小さい場合、差を埋めた残りでペアを作る
else:
    ans += min(B_hoge,hoge_A)
    if B_hoge_A > 0:
        ans += B_hoge_A - 1
        if B_hoge!=hoge_A:
            ans += 1
print(ans)

# print('-'*20)
# print('contained:',contained)
# print(B_hoge, hoge_A, B_hoge_A)

Submission status: Wrong Answer

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

Ans = int(co + min(s1,s2))
print (Ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
N = int(input())
counter = 0
master_parts = 0
a_parts = 0
b_parts = 0
for i in range(N):
    temp = input()
    counter += temp.count('AB')
    if temp[-1] == 'A' and temp[0] == 'B':
        master_parts += 1
    elif temp[-1] == 'A':
        a_parts += 1
    elif temp[0] == 'B':
        b_parts += 1
if a_parts and a_parts < b_parts:
    counter += (master_parts + a_parts)/2 + 1
elif b_parts and b_parts < a_parts:
    counter += (master_parts + b_parts)/2 + 1
else:
    if master_parts + a_parts != 1:
        counter += (master_parts + a_parts)/2
print(math.ceil(counter))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
#-------------------------------------------------------------------
import sys
def p(*a):
  s=" ".join(map(str,a))
  #print(s)
  sys.stderr.write(s+"\n")
#-------------------------------------------------------------------

N = int(input())

ans=a=b=ba=ab=0

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

ans+=ab
ans+=max(0,ba-1)
ans+=min(b,a)

if (abs(a-b)>0)and(ba>0): ans+=1

print(ans)


p(a,b,ba,ab)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=0
c=0
ans=0
m=0
for _ in range(N):
    s=input()
    ans+=s.count("AB")
    if s[0]=="A" or s[-1]=="A":
        a+=1
    if s[0]=="B" or s[-1]=="B":
        c+=1
    if (s[0]=="A" or s[-1]=="A") and (s[0]=="B" or s[-1]=="B"):
        m+=1
print(ans+min(a,c)-m//2)

Submission status: Wrong Answer

Solution code:
import re
n = int(input())
lst = []
ab = 0
a = 0
b = 0
for i in range(n):
  x = input()
  lst.append(x)
  if "AB" in x:
    ab += len(re.findall('AB', x))
  if x[0] == "B":
    a += 1
  if x[-1] == "A":
    b += 1
print(ab+min(a,b,n-1))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ab_count = 0
last_a_count = 0
first_b_count = 0
for s in sn:
    ab_count += s.count('AB')
    if s[-1] == 'A':
        last_a_count += 1
    if s[0] == 'B':
        first_b_count += 1

if last_a_count > 0 and first_b_count > 0:
    v = min(last_a_count, first_b_count)
else:
    v = 0
print(ab_count + v)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
S = []
ab = []  # if include ab [num, ,] if b start [, 1], if a end [, ,1]
for i in range(N):
    S.append(input())

ab_num = 0

x_A = 0
B_A = 0
B_x = 0


for i in range(N):
    s = S[i]
    for j in range(1, len(s)):
        if s[j] == 'B' and s[j-1] == 'A':
            ab_num += 1

    if s[0] == 'B':
        if s[len(s) - 1] == 'A':
            B_A += 1
        else:
            B_x += 1
    elif s[len(s) - 1] == 'A':
        x_A += 1

if B_A > 0:
    if x_A * B_x > 0:
        print(ab_num + B_A-1 + 2 + min(x_A-1, B_x-1))
    else:
        print(ab_num + B_A-1)
else:
    print(ab_num + min(x_A, B_x))

Submission status: Wrong Answer

Solution code:
N = int(input())
s = [input() for i in range(N)]
count = 0
l = []
a = 0
b = 0
ab = 0
for i in s:
  count+=i.count("AB")
for i in s:
  if list(i)[0] == "B" and list(i)[-1] != "A":
    l.append(i)
    b += 1
  elif list(i)[-1] == "A" and list(i)[0] != "B":
    l.append(i)
    a += 1
  elif list(i)[-1] == "A" and list(i)[0] == "B":
    l.append(i)
    ab += 1
if a > b + ab:
  count += b + ab
elif b > a + ab:
  count += a + ab
elif ab > a + b:
  count += ab-1 + a + b
else:
  count += max(a, b, ab)
print(count)

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
# print(countA, countB, countBA)
# print(ans)


if countA > countB:
    ans += countB
    countA -= countB
    
    if countA >= countBA:
        ans += countBA
        
    else:
        ans += countA
        countBA -= countA
        ans += countBA // 2
        if countA != 0:
            ans += 1
        
else:
    ans += countA
    countB -= countA
    
    if countB >= countBA:
        ans += countBA
        
    else:
        ans += countB
        countBA -= countB
        ans += countBA // 2
        if countB != 0:
            ans += 1
        
print(ans)

Submission status: Wrong Answer

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

base_ab=0
last_a=0
first_b=0
for s in string:
    base_ab+=s.count("AB")
    if s[-1]=="A":
        last_a+=1
    if s[0]=="B":
        first_b+=1
if n==1:
    last_a=0
print(base_ab+min(last_a,first_b))

Submission status: Wrong Answer

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

top = [0 for _ in range(N)]
last = [0 for _ in range(N)]

res = 0
n = 0
for s in S:
    for i in range(len(s)-1):
        if s[i] == 'A' and s[i+1] == 'B':
            res += 1
            
    if s[0] == 'B':
        top[n] = 1
        
    if s[-1] == 'A':
        last[n] = 1
        
    n += 1
 
d = 0
t = 0
l = 0
for i in range(N):
    if top[i] == 1 and last[i] == 1:
        d += 1
    elif top[i] == 1:
        t += 1
    elif last[i] == 1:
        l += 1
     

if d >= 1:
    res += d - 1

res += min(t, l)

if d > 0 and min(t, l) >= 1:
    res += min(t-1, l-1)
    res += 2
    
if d > 0 and min(t, l) < 1:
    res += 1
    
    
print(res)

Submission status: Wrong Answer

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

end_A = 0
start_B = 0
AB_count = 0
both = 0

for i in range(n):
    if 'AB' in s[i]:
        AB_count += s[i].count('AB')
    if s[i][0] == 'B':
        start_B += 1
    if s[i][len(s[i])-1] == 'A':
        end_A += 1
    if s[i][0] == 'B' and s[i][len(s[i])-1] == 'A':
        both += 1
start_B -= both
end_A -= both
# print(end_A, start_B, both, AB_count)
if end_A == 0 and start_B == 0:
    print(AB_count + both - 1)
elif end_A == 0:
    print(AB_count + both)
elif start_B == 0:
    print(AB_count + both)
else:
    print(AB_count + both + 1 + min(end_A-1, start_B-1))

Submission status: Wrong Answer

Solution code:
N = int(input())
s = [input() for _ in range(N)]
numAB = 0
numBonly = 0
numAonly = 0
numAandB = 0
 
for i in s:
    numAB += i.count("AB")
    if i[0] == 'B' and i[-1] != 'A':
        numBonly += 1
    elif i[0] != 'B' and i[-1] == 'A':
        numAonly += 1
    elif i[0] == 'B' and i[-1] == 'A':
        numAandB += 1
 
if numAonly == numBonly:
    if numAandB <= numAonly:
        print(numAandB + numAonly)
    else:
        print(2*numAonly + (numAandB - numAonly - 1))
else:
    print(numAB + min(numAonly, numBonly) + numAandB)

Submission status: Wrong Answer

Solution code:
N = int(input())

n_ab = 0
n_b = 0
n_a = 0

if N == 1:
    S = input()
    ans = S.count("AB")
else:
    for i in range(N):
        S = input()
        # 前後情報とABの数だけ調べておく
        n_ab += S.count("AB")
        if S[-1] == "A":
            n_a += 1
        if S[0] == "B":
            n_b += 1
    ans = n_ab+min(n_b,n_a)
    if min(n_b,n_a) == N:
        ans -= 1
print(int(ans))

Submission status: Wrong Answer

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

abf = 0
af = 0
bf = 0
ans = 0
for i in range(N):
  s_i = slist[i]
  ans += s_i.count("AB")
  if (s_i[0] == "B") and (s_i[-1] == "A"):
    abf+=1
  elif s_i[0] == "B":
    bf+=1
  elif s_i[-1] == "A":
    af+=1

if (abf > af):
  ans += af
else:
  ans += abf
if (abf > bf):
  ans += bf
else:
  ans += abf
if (af > bf) and (abf > af):
  ans += (abf - af) //2
if (bf > af) and (abf > bf):
  ans += (abf - bf) //2
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
C = [input() for i in range(n)]
N=A=B=0
for c in C:
    if c[0]=='B':
        B+=1
    if c[len(c)-1]=='A':
        A+=1
    N += c.count('AB')
print(N+min(A, B))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
f1, f2, f3 = 0, 0, 0
ans = 0
for i in range(n):
    t = input(); s.append(t)
    ans += t.count("AB")
    if t[0] == "B" and t[-1] != "A":
        f1 += 1
    if t[0] != "B" and t[-1] == "A":
        f2 += 1
    if t[0] == "B" and t[-1] == "A":
        f3 += 1
# f1: B....X
# f2: X....A
# f3: B....A
if f1 == 0:
    if f2 == 0:
        ans += max(0, f3-1)
    else:
        t = min(f2, f3)
        ans += t
        f2 -= t
        f3 -= t
        ans += f3

elif f2 == 0:
    if f1 == 0:
        ans += max(0, f3-1)
    else:
        t = min(f1, f3)
        ans += t
        f1 -= t
        f3 -= t
        if f1 == 0:
            ans += max(0, f3-1)
        else:
            ans += min(f1, 1)

elif f3 == 0:
    ans += min(f1, f2)
else:
    ans += min(f1, f2)
    t = min(f1, f2)
    f1 -= t
    f2 -= t

    if f1 == 0:
        if f2 == 0:
            ans += max(0, f3-1)
        else:
            tt = min(f2, f3)
            ans += tt
            f2 -= tt
            f3 -= tt
            ans += f3

    if f2 == 0:
        if f1 == 0:
            ans += max(0, f3-1)
        else:
            tt = min(f1, f3)
            ans += tt
            f1 -= tt
            f3 -= tt
            ans += max(0, f3-1)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

k = min(a,b,c)

if c == k:
	co += 2 * c
	co += min(a-2*k,b-2*k)
elif a == k:
	co += a
	kk = min(c-a,b-a)
	co += kk * 2
	if kk == b:
		co += c - k - kk + 1
	else:
		co += a
else:
	co += b
	kk = min(c-b,a-b)
	co += kk * 2
	if kk == a:
		co += c - k - kk + 1
	else:
		co += a


print(co)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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

counter = 0
#左端がb and 右端がa の数
ab = 0
#ab以外で右端がaの数
a = 0
#ab以外で左端がbの数
b = 0

for s in string_list:
    if 'AB' in s:
        counter += 1
    if s[0] == 'B' and s[-1] == 'A':
        ab += 1
    elif s[0] == 'B':
        b += 1
    elif s[-1] == 'A':
        a += 1

c = min(a,b)
d = max(a,b)
if d == 0 and ab > 0:
    counter += ab - 1
else:
    counter += c + ab

print(counter)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
S = []
for i in range(N):
    S.append(str(input()))
    
start_b = 0
end_a = 0
both = 0
ab_count = 0
for i in S:
    if i[0]=="B":
        start_b = start_b + 1
    if i[-1]=="A":
        end_a = end_a + 1
    if i[0]=="B" and i[-1]=="A":
        both = both + 1
    ab_count = ab_count + i.count("AB")

additional = min(start_b, end_a)
if additional == both:
    additional = additional  - 1
ans = ab_count + additional
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

# あぶれ者をたす
ans += min(count_xa, count_bx)

# あぶれ者残り
count_xx = max(count_xa, count_bx) - min(count_xa, count_bx)

# あぶれ者とBA
ans += min(count_ba, count_xx)

# 残りの両端がBAなものをくっつける、端に上で作った一個がつく
ans += max(count_ba - count_xx, 0)


print(ans)

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

for i in range(a_b):
    if a <= b:
        a += 1
    else:
        b += 1

ans = ab + min(a, b)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = []
a = 0
b = 0
both = 0
count = 0
for i in range(N):
    s = input()
    S.append(s)
    if "AB" in s:
        count += 1
    if s[0] == "B":
        b += 1
        if s[-1] == "A":
            both += 1
    if s[-1] == "A":
        a += 1
a -= both
b -= both
if a>0 and b>0:
    count += both+1 + min(a-1, b-1)
elif (a>0 and b==0) or (a==0 and b>0):
    count += both
else:
    count += both -1


print(count)

Submission status: Wrong Answer

Solution code:
import re
n = int(input())
s = []
compiled = re.compile(r"AB")

pre = 0
xa=0
bx=0
ba=0
for i in range(n):
    one = input()
    pre+=len(compiled.findall(one))
    f=one[0]
    l=one[-1]


    if f=="B" and l=="A":
        ba+=1
    elif f=="B" and l!="A":
        bx+=1
    elif f!="B" and l=="A":
        xa+=1

cnt=ba-1
if xa>=1:
    xa-=1
    cnt+1
    
if bx>=1:
    bx-=1
    cnt+1

cnt += xa if xa>bx else bx
print(cnt+pre)

Submission status: Wrong Answer

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

count_ab = sum([s.count('AB') for s in S])
count_top_b = len([1 for s in S if s[0] == 'B'])
count_last_a = len([1 for s in S if s[-1] == 'A'])
count_ba = len([1 for s in S if s[-1] == 'A' and s[0] == 'B'])

if count_ba > 0:
    a = count_last_a - count_ba
    b = count_top_b - count_ba
    len_ba_chain = count_ba + min(1, b) + min(1, a) - 1
    result = count_ab + len_ba_chain + min(b - 1, a - 1, 0)
else:
    result = count_ab + min(count_top_b, count_last_a, n - 1)
    
print(result)

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
# print(countA, countB, countBA)
# print(ans)

if countA > countB:
    ans += countB
    countA -= countB
    
    if countA >= countBA:
        ans += countBA
        
    else:
        ans += countA
        countBA -= countA
        ans += countBA // 2
        if countA != 0:
            ans += 1
        
else:
    ans += countA
    countB -= countA
    
    if countB >= countBA:
        ans += countBA
        
    else:
        ans += countB
        countBA -= countB
        ans += countBA // 2
        
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
first_b_num = 0
last_a_num = 0
ab_num = 0

for _ in range(n):
    s_list = list(input())
    for index,s in enumerate(s_list):
        if(index == 0):
            if(s == 'B'):
                first_b_num += 1
        else:
            if(s_list[index-1]+s == 'AB'):
                ab_num += 1
    if(s_list[index] == 'A'):
        last_a_num += 1

print(ab_num + min(first_b_num,last_a_num))

Submission status: Wrong Answer

Solution code:
N = input()
N = int(N)

ss = [input() for i in range(N)]

ss_ab_count = sum([s.count('AB') for s in ss])

end_a = [s[-1] == 'A' for s in ss]
start_b = [s[0] == 'B'for s in ss]

end_a_start_b = [is_end_a and is_start_b
                 for is_end_a, is_start_b in zip(end_a, start_b)]

end_a_start_b_count = sum(end_a_start_b)
end_a_count = sum(end_a)
start_b_count = sum(start_b)

# 大きい方をstart_b_countにする
if start_b_count < end_a_count:
    start_b_count, end_a_count = end_a_count, start_b_count

if end_a_start_b_count > start_b_count - end_a_count:
    end_a_start_b_count -= start_b_count - end_a_count
    end_a_count -= end_a_start_b_count // 2

count = ss_ab_count + end_a_count

print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
eni_B = 0
end_A = 0
both = 0
count = 0

for i in range(N):
    s = list(input())
    if s[0] == 'B':
        eni_B += 1
    if s[-1] == 'A':
        end_A += 1
    if s[0] == 'B' and s[-1] == 'A':
        eni_B -= 1
        end_A -= 1
        both += 1
    for j in range(len(s)-1):
        if s[j] == 'A':
            if s[j+1] == 'B':
                count += 1

m = min(end_A,eni_B)
M = max(end_A,eni_B)

if M == m:
    count = count + m + both - 1
else:
    count = count + m + both

print(count)

Submission status: Wrong Answer

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

a_tail = 0
b_head = 0
contain_ab = 0

for i in in_str:
    if i.endswith("A"):
        a_tail += 1
    if i.startswith("B"):
        b_head += 1

    contain_ab += i.count("AB")

contain_ab = contain_ab + min(a_tail, b_head)
print(contain_ab)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []

for i in range(n):
    s.append(list(input()))

x = 0    

for i in range(n):
    for j in range(1,len(s[i])):
        if s[i][j-1]=="A" and s[i][j]=="B":
            x += 1

p = 0
q = 0
r = 0
for i in range(n):
    if s[i][0]=="B" and s[i][-1]!= "A":
        p += 1
    if s[i][0]!="B" and s[i][-1]=="A":
        q += 1
    if s[i][0]== "B" and s[i][-1]=="A":
        r +=1

if p == q :
    print(x-1+min(p,q)+r)
else:
    print(x+min(p,q)+r)

Submission status: Wrong Answer

Solution code:
'''
C - AB Substrings
'''

str_list = []
end_a = []
start_b = []
ab_num = 0
for i in range(int(input())):
    str_list.append(input())
    if 'AB' in str_list[-1]:
        ab_num += 1
    if str_list[-1].endswith('A'):
        end_a.append(i)
    if str_list[-1].startswith('B'):
        start_b.append(i)

print(ab_num + min(len(end_a), len(start_b)))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = n*[""]
count = 0
nn = 0
bn = 0
na = 0
ba = 0
for i in range(n):
    s[i] = input()
    count += s[i].count("AB")
    if(s[i][0] == "B" and s[i][-1] == "A"): ba += 1
    elif(s[i][0] == "B"): bn += 1
    elif(s[i][-1] == "A"): na += 1


while(sum([ba,na,bn]) > 1):
    if(na > 0 and ba > 0 and bn > 0):
        count += ba+1
        ba = 0
        na -= 1
        bn -= 1
    elif(na > 0 and bn > 0):
        count += min([na, bn])
        na -= min([na, bn])
        bn -= min([na, bn])
    elif(na > 0 and ba > 0):
        count += ba
        na -= 1
        ba = 0
    elif(bn > 0 and ba > 0):
        count += ba
        bn -= 1
        ba = 0
    else: break


print(count)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)

def f(a,b,ba):
  if ba>0 and b>0:
    return 1 + f(a+1,b-1,ba-1)
  elif a>0 and ba>0:
    return 1 + f(a-1,b+1,ba-1)
  elif a>0 and b>0:
    return 1+ f(a-1,b-1,ba)

  return 0

N=int(input())
a=[]
b=[]

stA=0
stB=0
stBA=0

ans=0
for x in range(N):
  z=input()
  buf=z.count("AB")
  b.append((z[0],z[-1],buf))
  ans+=buf

for z in b:
  if z[0]=="B" and z[1]=="A":
    stBA+=1
  elif z[0]=="B":
    stB+=1
  elif z[1]=="A":
    stA+=1



ans=ans+f(stA,stB,stBA)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
count=countA=countB=countAB=0
for i in range(n):
    s=input()
    if "AB" in s:
        count+=1
    if s[-1]=="A":countA+=1
    if s[0]=="B":countB+=1
    if s[-1]=="A!" and s[0]=="B":countAB=0
print(count+min(countA,countB)+countAB)

Submission status: Wrong Answer

Solution code:
n = int(input())
c = 0
a = 0
b = 0
d = 0
for i in range(n):
    k = input()
    if k[0] == "B":
        if k[-1] == "A":
            d += 1
        else:
            b += 1
    elif k[-1] == "A":
        a += 1

    c += k.count("AB")

if a > 0 or b > 0:
    print(c + (min(a,b)+d))
elif d > 0:
    print(c + d -1)

Submission status: Wrong Answer

Solution code:
def task_c():
    n = int(input())
    sss = [input().strip() for _ in range(n)]
    
    def count_ab(tt):
        c = 0
        for i in range(1, len(tt)):
            if tt[i-1:i+1] == 'AB':
                c += 1
        return c
    
    c_first_b = sum(1 for ss in sss if ss[:1] == 'B')
    c_last_a = sum(1 for ss in sss if ss[-1:] == 'A')
    c_include_ab = sum(count_ab(ss) for ss in sss)
    print(min(c_first_b, c_last_a) + c_include_ab)
    
task_c()

Submission status: Wrong Answer

Solution code:
str_dict = [0, 0, 0] 
val = 0
for i in range(int(input())):
  line = input()
  val += line.count('AB')
  if line[0] == 'B' and line[-1] == 'A':
    str_dict[2] += 1
  elif line[0] == 'B':
    str_dict[0] += 1
  elif line[-1] == 'A':
    str_dict[1] += 1
val2 = min(str_dict[0], str_dict[1])
tmp = max(str_dict[0]-val2, str_dict[1]-val2)
val3 = min(tmp, str_dict[2])
val4 = str_dict[2]-val3
print(val+val2+val3+val4)