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:
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
if a == b:
  c = a-1
else:
  c = min(a,b,n-1)
print(ab+c)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
N = int(input())
ans = 0
l_B = 0
r_A = 0
lbra = 0
for _ in range(N):
	tmp = input()
	ans += tmp.count("AB")
	if tmp[0] == "B":
		if tmp[-1] == "A":
			lbra += 1
		else:
			l_B += 1
	elif tmp[-1] == "A":
		r_A += 1
print(ans + lbra + min(l_B, r_A))

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)

import math
if countA > countB:
    ans += countB
    countA -= countB
    
    if countBA > 1:
        ans += countBA - 1
        if countA > 0:
            ans += 1
            
    if countBA == 1:
        if countA > 0:
            ans += 1
        
        
else:
    ans += countA
    countB -= countA
    
    if countBA > 1:
        ans += countBA - 1
        if countB > 0:
            ans += 1
            
    if countBA == 1:
        if countB > 0:
            ans += 1
        
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())

ans=0
ba=0
bstart=0
aend=0
for i in range(n):
  s=input()
  for j in range(len(s)):
    if s[j]=="A":
      if not j+1==len(s):
        if s[j+1]=="B":
          ans+=1
  
  if s[0]=="B" and s[-1]=="A":
    ba+=1
  else:
    if s[0]=="B":
      bstart+=1
    if s[-1]=="A":
      aend+=1
      
print(ans,ba,bstart,aend)
      
if ba==0:
  print(ans+min(bstart,aend))
else:
  if bstart>=1 and aend>=1:
    print(ans+ba-1+2+min(bstart-1,aend-1))
  else:
    print(ans+ba-1+max(bstart,aend))

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
 
ans = numAB + max(0, numAandB-1)

if numBonly > 0:
    ans += 1
if numAonly > 0:
    ans += 1
if numBonly > 1 and numAonly > 1:
    ans += min(numBonly-1, numAonly-1)

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
result=0
fb=0# first is b
la=0# last is a
lafb=0
for i in range(N):
    s=input()
    result+=s.count('AB')
    if s[0]=="B" and s[-1]!="A":
        fb+=1
    elif s[-1]=="A" and s[0]!="B":
        la+=1
    elif s[-1]=="A" and s[0]=="B":
        lafb+=1


mn=min(la,fb)-1
result+=mn+lafb+1
print(result)

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
        lb_ra=0
        frag=True
    else:
        counter+=lb
        lb = lb
        lb_ra=lb_ra-lb
        frag=True
    if frag:
        counter+=lb_ra
        
print(counter)

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+=1
print(count+min(countA,countB) if countAB!=0 else count+min(countA,countB)-countAB)

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 or x == 0):
        if(a == 0 or b == 0):
            pass
        else:
            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())
cntA, cntB, cntBoth, ans = 0, 0, 0, 0

for i in range(N):
    s = input()
    L = len(s)
    if s[0] == 'B':
        cntB += 1
    if s[L-1] == 'A':
        cntA += 1
    if s[0] == 'B' and s[L-1] == 'A':
        cntBoth += 1
    for i in range(L-1):
        if s[i:i+2] == 'AB':
            ans += 1
# print(ans, cntA, cntB, cntBoth)
ans += min(cntA, cntB, N-1)
if cntA == cntB and cntA == cntBoth:
    ans -= 1
print(ans)

Submission status: Wrong Answer

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

ba = []
fo = []
no = []
flag = False
for i in range(N):
    flag = False
    if s[i][-1] == 'A':
        ba.append(i)
        flag = True
    if s[i][0] == 'B':
        fo.append(i)
        flag = True
    if (flag):
        no.append(i)


ba_l = len(ba)
fo_l = len(fo)
no_l = len(no)
ans_mn = 0
ans_mn = min(ba_l, fo_l)
ans_ma = 0
ans_ma = max(ba_l, fo_l)
ba = []
fo = []
no = []
if ba_l > fo_l:
    for i in range(N):
        flag = False
        if s[i][-1] == 'A':
            ba.append(i)
            flag = True
        elif s[i][0] == 'B':
            fo.append(i)
            flag = True
        elif (flag == False):
            no.append(i)
else:
    for i in range(N):
        flag = False
        if s[i][0] == 'B':
            fo.append(i)
            flag = True
        elif s[i][-1] == 'A':
            ba.append(i)
            flag = True
        elif (flag == False):
            no.append(i)
  
ba_l = len(ba)
fo_l = len(fo)
no_l = len(no)

ans = ''
if ba_l > fo_l:
    for i in range(ba_l):
        ans += s[ba[i]]
        if i < fo_l:
            ans += s[fo[i]]
# else ba_l < fo_l:
else:
    for i in range(fo_l):
        ans += s[fo[i]]
        if i < ba_l:
            ans += s[ba[i]]

for i in range(no_l):
    ans += s[no[i]]

print(ans.count('AB')+1)

Submission status: Wrong Answer

Solution code:
#c
n = int(input())
stringsA = 0
stringsB = 0
both = 0
ans = 0
if n == 1:
    string = input()
    print(string.count("AB"))
else:
    for i in range(n):
        string = input()
        ans += string.count("AB")
        if string[-1] == 'A' and string[0] == 'B':
            both += 1
        elif string[-1] == 'A':
            stringsA += 1
        elif string[0] == 'B':
            stringsB += 1
    if stringsA == stringsB and both > 1:
        both -= 1
    elif stringsA == stringsB and both == 1:
        both = 0
    print(ans + min(stringsA,stringsB) + both)

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]
    if "AB" in Si:
        result += 1
    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=[input() for _ in range(n)]
ans=0
for i in s:
    ans+=i.count("AB")
endA=[i for i in s if i[-1]=="A"]
startB=[i for i in s if i[0]=="B"]
AND=list(set(endA) & set(startB))
MIN=min(len(endA),len(startB))
tmp=endA if max(len(endA),len(startB))==len(endA) else startB
if list(set(tmp)-set(AND)):
    print(ans+MIN)
else:
    print(ans+MIN-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
S = [input() for i in range(n)]
cnt = 0
a = 0
b = 0
ba = 0
for i in range(n):
    cnt += S[i].count("AB")
    if S[i].startswith("B") and S[i].endswith("A"):
        ba += 1
    elif S[i].startswith("B"):
        a += 1
    elif S[i].endswith("A"):
        b += 1
if ba >= 1:
    cnt += ba - 1
if a >= 1:
    cnt += 1
if b >= 1:
    cnt += 1
print(cnt)

Submission status: Wrong Answer

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

a = [0] * N
b = [0] * N

ans = 0
for i in range(N):
    if s[i][0] == "B":
        b[i] = 1
    if s[i][-1] == "A":
        a[i] = 1
    for j in range(0,len(s[i])-1):
        if s[i][j] == "A" and s[i][j+1] == "B":
            ans += 1

special = 0

for i in range(N):
    if a[i] == 1 and b[i] == 1:
        special += 1
        a[i] = 0
        b[i] = 0


counta = a.count(1)
countb = b.count(1)




if special > 0:
    ans += special - 1
    if counta > countb:
        countb += 1
    elif countb > counta:
        counta += 1
    else:
        #counta == countb
        ans += 1

if counta > countb:
    ans += countb
elif countb > counta:
    ans += counta
else:
    ans += counta


print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
str = []
for i in range(n):
  s.append(list(input()))
cntA = 0
cntB = 0
for i in range(n):
  if s[i][-1] == 'A':
    cntA += 1
  elif s[i][0] == 'B':
    cntB += 1
memo = min(cntA, cntB)
ans = []
for i in range(len(s)):
  for j in range(len(s[i])):
    ans.append(s[i][j])
cnt = 0
for i in range(len(ans)-1):
  if ans[i] == 'A' and ans[i+1] == 'B':
    cnt += 1
print(cnt+memo)

Submission status: Wrong Answer

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
ans=0
ba=0
bx=0
xa=0
for i in range(n):
  s = stdin.readline().rstrip()
  for j in range(len(s)-1):
    if s[j] == 'A' and s[j+1] == 'B':
      ans +=1
  if( s[0] == 'B' and s[-1] == 'A' ):
    ba +=1
  elif s[0] == 'B':
    bx +=1
  elif s[-1] == 'A':
    xa +=1

ans += min(xa,bx)
if ba>0:
  ans += (ba-1)
  if xa!=0 and xa!=bx:
    ans += 1

print(ans)

Submission status: Wrong Answer

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

count_ab = 0
count_a = 0
count_b = 0
count_double = 0

for i, word in enumerate(s):
    if word.find('AB') != -1:
        count_ab+=1
    if word[0] =='B':
        count_b+=1
    if word[len(word)-1] == 'A':
        count_a+=1
    if word[0] == 'B' and word[len(word)-1] == 'A':
        count_double+=1

if count_a>count_b:
    ans = count_ab + min(count_b, count_a-count_double)
else:
    ans = count_ab + min(count_a, count_b-count_double)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
ca = 0
cb = 0
c = 0
for i in range(n):
    tmp = input()
    s.append(tmp)
    if tmp[-1] == "A" and tmp[0] == "B":
        c+= 1
    if tmp[-1] == "A":
        ca += 1
    if tmp[0] == "B":
        cb += 1
cnt = 0
for ss in s:
    cnt += ss.count("AB")
if c >= min(ca, cb):
    cnt -= 1
print(cnt + min(ca, cb))

Submission status: Wrong Answer

Solution code:
N = int(input()) 
s = [input() for i in range(N)] 
ans = 0
X = 0
x = 0
y = 0
for n in range(N):
    if s[n].count('AB') > 0:
        ans += s[n].count('AB')
    if s[n][0] != 'B' and s[n][-1] == 'A':
        X += 1
    if s[n][0] == 'B' and s[n][-1] != 'A':
        x += 1
    if s[n][0] == 'B' and s[n][-1] == 'A':
        y += 1
if X-x <= 0:
    ans += X + y
if X-x >0 and X-x-y >0:
    ans += x + y
if X-x >0 and X-x-y <=0:
    ans += X
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
B_=0
_A=0
AB = 0
B_A=0
for i in range(n):
    s = input()
    AB += s.count('AB')
    if s[0] == 'B' and s[-1] == "A":
        B_A += 1
    elif s[0] == 'B':
        B_ += 1
    elif s[-1] == 'A':
        _A += 1
    

print(AB + (B_A-1) + min(B_, _A) + 1)

Submission status: Wrong Answer

Solution code:
N = int(input())

n_ab = 0
n_b = 0
n_a = 0
n_both = 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
        if S[-1] == "A" and S[0] == "B":
            n_both += 1
    ans = n_ab+min(n_b,n_a)
    if n_both == n_a and n_both == n_b:
        ans -= 1

print(int(ans))

Submission status: Wrong Answer

Solution code:
n = int(input())
words = [str(input()) for i in range(n)]
counter = 0
beginb = 0
enda = 0
mix = 0
for word in words:
    for index, letter in enumerate(word):
        if index<len(word)-1:
            if word[index] == 'A' and word[index+1] == 'B':
                counter += 1
    if word[0] == "B" and word[-1] == "A":
        mix += 1
    else:
        if word[0] == "B":
            beginb += 1
        if word[-1] == "A":
            enda += 1

if beginb == 0 and enda == 0:
    print(counter+min(beginb,enda)-1+mix)
else:
    print(counter+min(beginb,enda)+mix)

Submission status: Wrong Answer

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

ans = 0

# 文字列の中身のAB
for d in data:
    ans += d.count("AB")

# 結合後のいくつになるかを数える => 何個ペアができるか確認すればいい
A_key = 0
B_key = 0
for d in data:
    # check A_key
    if d[-1] == "A":
        A_key += 1
    
    if d[0] == "B":
        B_key += 1

print(ans + min(A_key, B_key))

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
if B_hoge==1 and hoge_A==1 and B_hoge_A>0: 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())
strs = [input() for _ in range(n)]

ans = 0

# 各文字列でABの出現数
for s in strs:
    ans += s.count('AB')

# b ではじまり a で終わるもの
starts_and_ends = set(s for s in strs if s.startswith('B') and s.endswith('A'))

# b ではじまるもの
starts_with_b = set(s for s in strs if s.startswith('B')) - starts_and_ends

# a で終わるもの
end_with_a = set(s for s in strs if s.endswith('A')) - starts_and_ends

# 先頭を一個切り出す。
count = [len(end_with_a), len(starts_with_b), len(starts_and_ends)]

if count[0] == 0 and count[2] > 0:
    count[0] = 1
    count[2] -= 1

for i in range(count[2]):
    count[0] += 1
    count[1] += 1

print(ans + min(count[0], count[1]))

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)

if end_a == start_b:
    ab_num += min(len(end_a), len(start_b)) - 1
else:
    ab_num += min(len(end_a), len(start_b))

print(ab_num)

Submission status: Wrong Answer

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

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

print(c + v + min(a, b))

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 and abf > bf:
  ans += af
  ans += bf
  ans += (abf - max([af,bf])) // 2
elif abf > af and abf <= bf:
  ans += af
  ans += abf
elif abf <= af and abf > bf:
  ans += bf
  ans += abf
elif abf <= af and abf <= bf:
  ans += 2*abf 

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[1] != 'B' and tmp[len(tmp) - 1] == 'B' and tmp[len(tmp) - 2 != 'A']:
        comn2 = 1
    if tmp[0] == 'A' and tmp[1] !='B':
        acount2 += 1
    if tmp[len(tmp) - 1] == 'B' and tmp[len(tmp) - 2] != 'A':
        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 = []

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

x = 0    

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

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

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

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 and end_a_num==same_num:
    ans-=1    

print(ans)

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 or x == 0):
        ans1 += min(a,b)
    elif((a == 0 or b == 0) and x == 0):
        pass
    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_list = [input() for _ in range(n)]
res_list = []
res = 0
for s in s_list:
    res += s.count('AB')+(0.3 if s[0]=='B' else 0)+(0.4 if s[-1]=='A' else 0)
import math
print(math.ceil(res))

Submission status: Wrong Answer

Solution code:
print(0)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for i in range(N)]
ans = 0
BFirst = 0
ALast = 0
for i in range(len(S)):
    if S[i].endswith("A") and not S[i].startswith("AB"):
        ALast += 1
    if S[i].startswith("B") and not S[i].endswith("AB"):
        BFirst+=1
    if "AB" in S[i]:
        tmpIndex = S[i].find("AB")
        while tmpIndex != -1:
            ans += 1
            tmpIndex = S[i].find("AB",tmpIndex+1)

print(ans + min(BFirst, ALast))

Submission status: Wrong Answer

Solution code:
N = int(input())

BA = -1
ar = 0
bl = 0
ans = 0
for _ 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

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

print(ans)

Submission status: Wrong Answer

Solution code:
headA = 0
headB = 0
tailA = 0
tailB = 0
c = 0
for i in range(int(input())):
    t = input()
    if t[:2] == 'AB':
        c+=1
        temp = 'ZZ'+t[2:]
        if 'AB' in temp[-2:]:
            c += temp.count('AB')
        elif temp[-1] == 'A':
            tailA +=1
            c += temp.count('AB')
        else:
            c += temp.count('AB')
    elif 'AB' in t[-2:]:
        c +=1
        t = t[0:-2]+'ZZ'
        if t[0] == 'B':
            headB+=1
        c+= t.count('AB')
    else:
        if t[0]== 'B':
            headB +=1
        if t[-1] == 'A':
            tailA +=1
        c+=t.count('AB')
print(c+min(headB,tailA))

Submission status: Wrong Answer

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

ba = []
fo = []
no = []
flag = False
for i in range(N):
    flag = False
    if s[i][-1] == 'A':
        ba.append(i)
        flag = True
    if s[i][0] == 'B':
        fo.append(i)
        flag = True
    if (flag):
        no.append(i)


ba_l = len(ba)
fo_l = len(fo)
no_l = len(no)
ans_mn = 0
ans_mn = min(ba_l, fo_l)
ans_ma = 0
ans_ma = max(ba_l, fo_l)
ba = []
fo = []
no = []
if ba_l > fo_l:
    for i in range(N):
        flag = False
        if s[i][-1] == 'A':
            ba.append(i)
            flag = True
        elif s[i][0] == 'B':
            fo.append(i)
            flag = True
        elif (flag == False):
            no.append(i)
else:
    for i in range(N):
        flag = False
        if s[i][0] == 'B':
            fo.append(i)
            flag = True
        elif s[i][-1] == 'A':
            ba.append(i)
            flag = True
        elif (flag == False):
            no.append(i)
  
ba_l = len(ba)
fo_l = len(fo)
no_l = len(no)

ans = ''
if ba_l > fo_l:
    for i in range(ba_l):
        ans += s[ba[i]]
        if i < fo_l:
            ans += s[fo[i]]
# else ba_l < fo_l:
else:
    for i in range(fo_l):
        ans += s[fo[i]]
        if i < ba_l:
            ans += s[ba[i]]
print('1')
print(ans)

for i in range(no_l):
    ans += s[no[i]]

print(ans.count('AB')+1)

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)])
wer=min(xxA,Bxx)+BxA
print(ans+wer)

Submission status: Wrong Answer

Solution code:
N = int(input())

cnt = 0
l = 0
r = 0
b = 0
for i in range(N):
    s_i = input()
    cnt += s_i.count("AB")

    if s_i[0] == "B" and s_i[len(s_i) - 1] == "A":
        b += 1
    elif s_i[0] == "B":
        l += 1
    elif s_i[len(s_i) - 1] == "A":
        r += 1
    else:
        continue

p = min(l, r, b)

cnt += p * 2

if b - p == 0:
    cnt += min(l - p, r - p)
elif r - p == 0:
    cnt += b - p - 1
    cnt += min(1, l - p)
elif l - p == 0:
    cnt += b - p - 1
    cnt += min(1, r - p)


print(cnt)

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)

import math
if countA > countB:
    ans += countB
    countA -= countB
    
    ans += countBA - 1
    if countA > 0:
        ans += 1
        
        
else:
    ans += countA
    countB -= countA
    

    ans += countBA - 1
    if countB > 0:
        ans += 1
        
print(ans)

Submission status: Wrong Answer

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


total = 0
a = 0
b = 0
ba = 0

for _s in s:
    total += _s.count('AB')
    if _s[-1] == 'A':
        a += 1
    if _s[0] == 'B':
        b += 1
    if _s[-1] == 'A' and _s[0] == 'B':
        ba += 1


if a > ba or b > ba:
    pass
else:
    b -= 1

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

Submission status: Wrong Answer

Solution code:
n = int(input())
 
g1 = []
g2 = []
g3 = []
 
cnt = 0
 
for i in range(n):
    s = input()
    cnt += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        g3.append(s)
    elif s[0] == "B":
        g1.append(s)
    elif s[-1] == "A":
        g2.append(s)
        
a = len(g1)
b = len(g2)
c = len(g3)
 
if a == b:
    if a == c:
        cnt += c * 2
    elif a > c:
        cnt += c
        cnt += a
    elif a < c:
        cnt += a
        cnt += c
elif a > b:
    cnt += b
    cnt += c
elif b > a:
    cnt += a
    cnt += c
 
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
count = 0
topB = 0
bottomA = 0
for i in range(N):
    s = str(input())
    count += s.count("AB")
    if s[0] == "B":
        topB += 1
    if s[-1] == "A":
        bottomA += 1
if topB != bottomA:
    print(count + min(topB,bottomA))
else:
    print(count + min(topB,bottomA)-1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N = int(input())
S = [None] * N
cnt = 0
B_X = 0
X_A = 0
B_A = 0

for i in range(N):
  S[i] = input()
  cnt += S[i].count('AB')
  left_B_frag = S[i][0] == 'B'
  right_A_flag = S[i][-1] == 'A'
  if left_B_frag and right_A_flag:
    B_A += 1
  elif left_B_frag:
    B_X += 1
  elif right_A_flag:
  	X_A += 1

if B_A >= 2:
  cnt += B_A - 1
  B_A = 1
if B_X > X_A:
  cnt += B_A + X_A + B_X-X_A
else:
  cnt += B_A + B_X + X_A-B_X
# print(B_A, B_X, X_A)
print(cnt)

Submission status: Wrong Answer

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

c = 0
a = 0
b = 0
ab = 0

for i in range(0, n):
    for j in range(1, len(s[i])):
        if s[i][j-1:j+1] == "AB":
            c += 1
    if s[i][-1] == "A" and s[i][0] == "B":
        ab += 1
    else: 
        if s[i][0] == "B":
            b += 1
        if s[i][-1] == "A":
            a += 1

s = c
m = min(a, b)
s += m
a -= m
b -= m

if ab > 0:
    s += ab - 1
    if a > 0:
        s += 1
    if b > 0:
        s += 1

print(s)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N = int(input())


def ret(string):
    return (1 if string[0] == "B" else 0, string.count("AB"), 1 if string[-1] == "A" else 0)


b, mid, a = 0, 0, 0
for i in range(N):
    db, dmid, da = ret(input().rstrip())
    b += db
    mid += dmid
    a += da

print(min(a, b, N-1)+mid)

Submission status: Wrong Answer

Solution code:
N=int(input())
a1=0
a2=0
c1=0
c2=0
ans=0
m=0
for _ in range(N):
    s=input()
    ans+=s.count("AB")
    if s[0]=="A":
        a1+=1
    if s[-1]=="A":
        a2+=1
    if s[0]=="B":
        c1+=1
    if s[-1]=="B":
        c2+=1
    
print(ans+min(a2,c1)+min(a1,c2))

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
A = 0
B = 0
for i in range(N):
  s = input()
  ans += s.count('AB')
  if s[len(s)-1] == 'A':
    A+=1
  if s[0] == 'B':
    B+=1
if A!=0 and B!=0:
  if A>B:
    print(ans+B)
  elif A<B:
    print(ans+A)
  else:
    print(ans+A-1)
else:
  print(ans)

Submission status: Wrong Answer

Solution code:

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

count = 0
A_count = 0
B_count =0
AB_count = 0
# 文字列の中にある"AB"の個数(下限)
for s in str_list:
    if "AB" in s:
        count += 1
    # 末尾がAの文字列の個数
    if s[-1] == "A":
        A_count += 1
    if s[0] == "B":
        B_count += 1
    if s[-1] == "A" and s[0] == "B":
        AB_count += 1

set_num = min([A_count, B_count])

if A_count == B_count and A_count == AB_count:
    print(count + set_num - 1)
else:
    print(count + set_num)

Submission status: Wrong Answer

Solution code:
import sys
from fractions import gcd
from functools import reduce
from itertools import combinations

import numpy as np

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


def solve():
    N = ii()
    # S = imi(N, str)
    first_B = []
    last_A = []
    num_AB = 0
    # 重複要素
    dupl = []
    for _ in range(N):
        s = ii(str)
        num_AB += s.count("AB")
        if s[0] == "B" and s[-1] == "A":
            dupl.append(s)
            continue
        if s[0] == "B":
            first_B.append(s)
        if s[-1] == "A":
            last_A.append(s)
    comb_AB = min(len(first_B), len(last_A))
    ans = num_AB + comb_AB
    if dupl:
        ans += len(dupl)
        if abs(len(first_B) - len(last_A)) == 0:
            ans -= 1
    return ans


if __name__ == "__main__":
    print(solve())