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: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
import re
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    s = SR(n)
    a = 0
    b = 0
    ab = 0
    ans = 0
    for si in s:
        if si[0] == "B" and si[-1] == "A":
            ab += 1
        elif si[0] == "B":
            b += 1
        elif si[-1] == "A":
            a += 1
        ans += si.count("AB")
    if ab:
        ans += ab - 1
        ans += (a > 0) + (b > 0)
        a -= a > 0
        b -= b > 0
    ans += min(a, b)
    print(ans)

    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys


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


def main():
    ans = 0
    A = []
    B = []
    N = int(input())

    for _ in range(N):
        s = input()
        ans += s.count("AB")
        if s[0] == "B":
            B.append(1)
        else:
            B.append(0)
        if s[-1] == "A":
            A.append(1)
        else:
            A.append(0)
    if A == B:
        if sum(A) > 0:
            ans += sum(A) - 1
    else:
        ans += min(sum(A), sum(B))
    return ans


if __name__ == "__main__":
    ans = main()
    print(ans)

Submission status: Accepted

Solution code:
## coding: UTF-8

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

#print(N)
#print(l)

#中にあるABの数を数える
def count_inAB(s):
    counter = 0
    for i in range(len(s)-1):
        if(s[i] == 'A' and s[i+1] == 'B'):
            counter += 1
    return counter
'''
print(count_inAB('ABCA'))
print(count_inAB('ABCAB'))
print(count_inAB('ABABAB'))
print(count_inAB('ACBA'))
'''
#末尾にあるAの数を数える
lastA = []
for i in range(N):
    if(l[i][-1] == 'A'):
        lastA.append(i)
#print('lastA:{}'.format(lastA))

#先頭にあるBの数を数える
firstB = []
for i in range(N):
    if(l[i][0] == 'B'):
        firstB.append(i)
#print('firstB:{}'.format(firstB))






ans = 0
for i in range(N):
    ans += count_inAB(l[i])
    #print(ans)
#print('insert:{}'.format(ans))


connect = min(len(lastA), len(firstB))
if(lastA == firstB):
    connect -= 1
if(lastA == [] and firstB == []):
    connect += 1
#print('connect:{}'.format(connect))


ans += connect

#rint('ans:{}'.format(ans))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ret = 0
cnt_ab = 0
cnt_a = 0
cnt_b = 0

for i in range(n):
    s = input()
    ret += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        cnt_ab += 1
    elif s[0] == 'B':
        cnt_b += 1
    elif s[-1] == 'A':
        cnt_a += 1

if cnt_ab == 0:
    ret += min(cnt_a, cnt_b)
else:
    if cnt_a + cnt_b > 0:
        ret += cnt_ab + min(cnt_a, cnt_b)
    else:
        ret += (cnt_ab - 1)


print(ret)

Submission status: Accepted

Solution code:
n = int(input())
naibu = 0
head = 0
tail = 0
both = 0

for _ in range(n):
    s = str(input())
    naibu += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        both += 1
    elif s[0] == 'B':
        head += 1
    elif s[-1] == 'A':
        tail += 1

ans = min(head, tail) + both + naibu
if head == 0 and tail == 0:
    ans = max(both-1, 0) + naibu
print(ans)

Submission status: Accepted

Solution code:
import re
n=int(input())
ans=0
a,b,ba=0,0,0
for i in range(n):
  s=input()
  ans+=len(re.sub("[A-Z]+","",s.replace("AB","s")))
  if s[0]=="B" and s[-1]=="A":ba+=1
  elif s[0]=="B":b+=1
  elif s[-1]=="A":a+=1
ans+=max(ba-1,0)
if ba:
  if a:ans+=1;a-=1
  if b:ans+=1;b-=1
print(ans+min(a,b))

Submission status: Accepted

Solution code:
N = int(input())

a, b, ba = 0, 0, 0
ret = 0
for _ in range(N):
    s = input()

    ret += s.count('AB')

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

if ba:
    ret += ba - 1
    if a:
        ret += 1
        a -= 1
    if b:
        ret += 1
        b -= 1
ret += min(a, b)

print(ret)

# ...A
# B...
# B...A
# ... -> 無視

Submission status: Accepted

Solution code:
N = int(input())

from collections import defaultdict
d = defaultdict(int)
cnt = 0
for i in range(N):
  s = input().replace("AB","#")
  cnt += s.count("#")
  l = 1 if s[0]  == "B" else 0
  r = 1 if s[-1] == "A" else 0
  d[(l,r)] += 1

if d[(1,0)]>0  and d[(0,1)]>1 :
  print(cnt+d[(1,1)] + min(d[(0,1)],d[(1,0)]))
elif d[(1,0)] >0 or d[(0,1)]>0:
  print(cnt+d[(1,1)] + min(d[(0,1)],d[(1,0)]))
else:
  print(cnt+max(0,d[(1,1)]-1))

Submission status: Accepted

Solution code:
N = int(input())

count_ab = 0
count_start_b = 0
count_finish_a = 0
count_start_b_finish_a = 0
for i in range(N):
    s = input()
    count_ab += s.count('AB')
    # print(s, s[0], s[-1])
    if s[0] == 'B' and s[-1] == 'A':
        count_start_b_finish_a += 1
    elif s[0] == 'B':
        count_start_b += 1
    elif s[-1] == 'A':
        count_finish_a += 1

# print(count_start_b_finish_a, count_finish_a, count_start_b)

if not count_start_b_finish_a:
    print(count_ab + min(count_finish_a, count_start_b))
elif max(count_finish_a, count_start_b) > 0 and count_start_b_finish_a:
    print(count_ab + count_start_b_finish_a + min(count_finish_a, count_start_b))
elif count_start_b_finish_a:
    print(count_ab + count_start_b_finish_a - 1)

Submission status: Accepted

Solution code:
n = int(input())

ans = 0

BA = 0
XA = 0
BX = 0

for i in range(n):
    s = input()
    ans += s.count('AB')

    if s[0] == 'B' and s[-1] == 'A':
        BA += 1
    elif s[-1] == 'A':
        XA += 1
    elif s[0] == 'B':
        BX += 1

if BA > 0:
    ans += BA-1
    BA = 1
    if BX > 0:
        ans += 1
        BA = 0
    else:
        BX = 1
        BA = 0

ans += min(XA, BX)

print(ans)

Submission status: Accepted

Solution code:
import sys

n, *s = sys.stdin.readlines()
a = 0
b = 0
ba = 0
ans = 0
for i in s:
    if i[0] == 'B' and i[-2] == 'A':
        ba += 1
    elif i[0] == 'B':
        b += 1
    elif i[-2] == 'A':
        a += 1
    ans += i.count('AB')
if a == 0 and b == 0 and ba > 0:
    ba -= 1
ans += ba + min(a, b)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = 0
B = 0
AB = 0
ans = 0
for _ in range(N):
    s = list(input())
    if s[0] == "B" and s[-1] == "A":
        AB += 1
    elif s[0] == "B":
        B += 1
    elif s[-1] == "A":
        A += 1
    for i in range(len(s) - 1):
        if s[i:i + 2] == list("AB"):
            ans += 1
if A == 0 and B == 0 and AB > 0:
    ans -= 1
print(ans + AB + min(A, B))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())

a, b, c, d = 0, 0, 0, 0
for _ in range(N):
    s = input()
    if s.startswith('B') and s.endswith('A'):
        a += 1
    elif s.startswith('B'):
        b += 1
    elif s.endswith('A'):
        c += 1

    d += s.count('AB')

# print(a, b, c, d)

if a == 0:
    print(d + min(b, c))

elif b == 0 and c == 0:
    print(d + a -1 )

else:
    print(d + a + min(b, c))

Submission status: Accepted

Solution code:
n = int(input())
count = 0

a, b, ab = 0, 0, 0
for _ in range(n):
    s = input()
    if s[-1] == "A" and s[0] == "B":
        ab += 1
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1


    for i in range(len(s)-1):
        if s[i:i+2] == "AB":
            count += 1

count += min(a, b)
if max(a, b) == ab and ab != 0:
    count -= 1

print(count)

Submission status: Accepted

Solution code:
def main():
    words = int(input())
    first_b = [""]
    last_a = [""]
    others = []
    ab = []
    for _ in range(words):
        s = input()
        if s[0] == "B" and s[-1] == "A":
            ab.append(s)
        elif s[-1] == "A":
            last_a.append(s)
        elif s[0] == "B":
            first_b.append(s)
        else:
            others.append(s)
    connect = []
    for i in range(max(len(last_a), len(first_b))):
        if i < len(last_a) - 1:
            connect.append(last_a[i])
        if i < len(first_b) - 1:
            connect.append(first_b[i])
    word = last_a[-1] + "".join(ab) + first_b[-1] + "".join(connect) + "".join(others)
    print(word.count("AB"))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
s = [input() for _ in range(n)]
tail_a = 0
head_b = 0
both_have = 0
ans = 0
for i in range(n):
    if s[i][0] == "B": head_b += 1
    if s[i][-1] =="A": tail_a += 1
    if s[i][0] == "B" and s[i][-1] =="A":
        both_have += 1
    ans += s[i].count("AB")
ans += min(tail_a,head_b)
if tail_a == head_b and tail_a == both_have:
    if both_have > 0:
        ans -= 1
print(ans)

Submission status: Accepted

Solution code:
import bisect
# from collections import Counter, defaultdict, deque, namedtuple
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
    n = int(input())
    b_count = 0
    a_count = 0
    b_and_a_count = 0
    ans = 0

    for i in range(n):
        s = sys.stdin.readline().rstrip("\n")
        len_s = len(s)
        for j in range(len_s-1):
            if s[j] == "A":
                if s[j+1] == "B":
                    ans += 1
        if s[0] == "B" and s[-1] == "A":
            b_and_a_count += 1
        elif s[0] == "B":
            b_count += 1
        elif s[-1] == "A":
            a_count += 1

    if b_and_a_count > 0:
        ans += (b_and_a_count-1)
        if a_count > 0:
            ans += 1
            a_count -= 1
        if b_count > 0:
            ans += 1
            b_count -= 1

    ans += min(a_count, b_count)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
numA = 0
numB = 0
cnt = 0
sm = 0

for i in range(N):
    mm = input()
    if mm[0]=="B":
        numB += 1
    if mm[-1]=="A":
        numA += 1
    if mm[0]=="B" and mm[-1]=="A":
        sm += 1

    for i in range(len(mm)):
        if mm[i:i+2]=="AB":
            cnt += 1

if sm == numA and sm==numB and sm>0:
    numA -= 1
    numB -= 1
mm2 = min(numA,numB,N-1)
print(cnt+mm2)

Submission status: Accepted

Solution code:
n=int(input())
ans=0
a=0
b=0
flg=1
for _ in range(n):
  s=input()
  for i in range(len(s)-1):
    if s[i:i+2]=="AB":
      ans+=1
  if s[0]=="B":
    b+=1
    if s[-1]!="A":
      flg=0
  if s[-1]=="A":
    a+=1
    if s[0]!="B":
      flg=0
if flg:
  ans+=max(0,min(a,b)-1)
else:
  ans+=min(a,b)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

S = []

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


# internal ab count
internal_ab = 0
for s in S:
    for i in range(1,len(s)):
        if s[i-1] == 'A' and s[i] == 'B':
            internal_ab += 1

# edge
a_pool = 0
b_pool = 0
ab_pool = 0
edge_ab = 0
for s in S:
    if s[0] == 'B' and s[-1] == 'A':
        ab_pool += 1 
    elif s[0] == 'B':
        b_pool += 1
    elif s[-1] == 'A':
        a_pool += 1

if ab_pool == 0:
    edge_ab = min(a_pool, b_pool)
elif a_pool + b_pool == 0:
    edge_ab = ab_pool - 1
else:
    edge_ab = ab_pool + min(a_pool, b_pool)
    
print(internal_ab+edge_ab)

Submission status: Accepted

Solution code:
N=int(input())
S=[input() for i in range(N)]
BXA=0;XA=0;BX=0;ans=0;
for s in S:
  ans+=s.count("AB")
  if(s[0]=='B' and s[-1]=='A'):
    BXA+=1
  elif(s[0]=='B'):
    BX+=1
  elif(s[-1]=='A'):
    XA+=1
#print(ans,min([XA,BX]))
if min([XA,BX])>0:
  ans+=min([XA,BX])+BXA
elif max([XA,BX])>0:
  ans+=BXA
else:
  ans+=max([BXA-1,0])
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=[input() for i in range(N)]
BA=0
B=0
A=0
cnt=0
for i in S:
  if i[0]=='B'and i[-1]=='A':
    BA+=1
  elif i[0]=='B':
    B+=1
  elif i[-1]=='A':
    A+=1
  for j in range(len(i)-1):
    if i[j]=='A' and i[j+1]=='B':
      cnt+=1
if BA==0:
  print(cnt+min(A,B))
else:
  ans=BA-1
  if B>0:
    ans+=1
    B-=1
  if A>0:
    ans+=1
    A-=1
  print(cnt+min(A,B)+ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
cntAB = 0
cntA = 0
cntB = 0
BcntA = 0
for i in range(N):
  s = input()
  if s[0] == "B" and s[-1] == "A":
    BcntA += 1
  elif s[0] == "B" and s[-1] != "A":
    cntB += 1
  elif s[0] != "B" and s[-1] == "A":
    cntA += 1
  for j in range(1,len(s)):
    if s[j-1] == "A" and s[j] == "B":
      cntAB += 1
if BcntA!=0:
  if cntA+cntB>0:
    print(cntAB+min(cntA,cntB)+BcntA)
  else:
    print(cntAB+BcntA-1)
else:  
  print(cntAB+min(cntA,cntB))

Submission status: Accepted

Solution code:
N = int(input())
ans = 0
da =0
db = 0
dba = 0
for i in range(N):
    s = input()
    k = s.count('AB')
    ans += k
    if s[0] == 'B' and s[-1] == 'A':
        dba +=1
        continue
    if s[0] == 'B':
        db +=1
    if s[-1] == 'A':
        da +=1
if dba != 0:
    ans += dba -1
    if da >= 1:
        ans +=1
        da -=1
    if db >=1:
        ans +=1
        db -=1
    ans += min(da,db)
    print(ans)
    exit()
if dba == 0:
    ans += min(da,db)
    print(ans)
    exit()

Submission status: Accepted

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

ans,acnt,bcnt,abcnt=0,0,0,0
for s in S:
  ans+=s.count('AB')
  if s[-1]=='A' and s[0]=='B': abcnt+=1
  elif s[-1]=='A': acnt+=1
  elif s[0]=='B': bcnt+=1
if abcnt:
  ans+=abcnt-1
  if acnt:
    ans+=1
    acnt-=1
  if bcnt:
    ans+=1
    bcnt-=1
ans+=min(acnt,bcnt)
print(ans)

Submission status: Accepted

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

def main():
    N = int(input())
    AB = [0,0,0]
    ans = 0
    for _ in range(N):
        s = input()
        l = len(s)
        if s[0] == "B" and s[-1] == "A":
            AB[2] += 1
        else:
            if s[0] == "B":
                AB[1] += 1
            if s[-1] == "A":
                AB[0] += 1
        for i in range(l-1):
            if s[i] == "A" and s[i+1] == "B":
                ans += 1

    if (AB[0]+AB[1] == 0 and AB[2] != 0):
        print(ans+AB[2]-1)
    else:
        print(ans+min(AB[0],AB[1])+AB[2])
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N=int(input())
S=[input() for _ in range(N)]
AB=sum(s.count("AB") for s in S)
EA=sum(s.endswith("A") for s in S)
SB=sum(s.startswith("B") for s in S)
EASB=sum(s.endswith("A") and s.startswith("B") for s in S)
K=min(EA,SB)-(EA==SB==EASB!=0)
print(AB+K)

Submission status: Accepted

Solution code:
N = int(input())

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

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

Submission status: Accepted

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

if BA==0:
    print(count+min(A,B))
else:
    ans=count+BA-1
    if A>=1 and B>=1:
        print(ans+2+min(A,B)-1)
    elif A>=1 or B>=1:
        print(ans+1)
    else:
        print(ans)

Submission status: Accepted

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

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

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

if cnt_start_B_last_A == 0:
    print(cnt_AB + min(cnt_start_B, cnt_last_A))
else:
    if cnt_start_B + cnt_last_A > 0:
        print(cnt_AB + cnt_start_B_last_A + min(cnt_start_B, cnt_last_A))
    else:
        print(cnt_AB + cnt_start_B_last_A - 1)

Submission status: Accepted

Solution code:
N=int(input())
a,b,ba=0,0,0
ret = 0
for i in range(N):
  st = input()
  ret+=st.count('AB')
  if st[0]=='B' and st[-1]=='A':
    ba+=1
  elif st[0]=='B':
    b+=1
  elif st[-1]=='A':
    a+=1
if ba>0:
  ret+=ba-1
  if a>0 and b>0:
    ret+=2
    ret+=min(a-1,b-1)
  elif a==0 and b==0:
    ret+=0
  else:
    ret+=1
else:
  ret+=min(a,b)
print(ret)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
ab,xa,bx,bxa = 0,0,0,0
for i in range(n):
  s = input()
  if s[-1] == 'A' and s[0] == 'B':
    bxa += 1
  if s[-1] == 'A':
    xa += 1
  if s[0] == 'B':
    bx += 1
  for j in range(len(s)-1):
    if s[j]+s[j+1] == 'AB':
      ab += 1
if bxa == xa == bx:
  print(ab+bxa-bool(bxa))
else:
  print(ab+min(xa,bx))

Submission status: Accepted

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

Submission status: Accepted

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

count = 0
no = 0
end_A = 0
start_B = 0
AB = 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][0] == "B":
    start_B+=1
  elif S[i][-1] == "A":
    end_A+=1
  else:
    no+=1
if end_A >= start_B:
  count +=start_B
  start_B = 0
  end_A -= start_B
else:
  count += end_A
  end_A = 0
  start_B-=end_A

count += max(0,AB-1)

if AB >= 1 and (end_A>0 or start_B>0):
  count += 1
print(count)

Submission status: Accepted

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

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

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

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

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

ans+=min(a_en,b_st)
a_en -= min(a_en,b_st)
b_st -= min(a_en,b_st)

if b_a == 0:
    print(ans)
    exit()
ans+=b_a-1
if a_en != 0 or b_st!= 0:
    print(ans+1)
else:
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

ans, ba, a, b = 0, 0, 0, 0

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

if a > 0 and b > 0 and ba > 0:
    ans += ba + 1 + min(a-1, b-1)
elif a == 0 and b == 0:
    ans += max(ba-1, 0)
elif (a > 0 or b > 0) and ba > 0:
    ans += ba
elif ba == 0:
    ans += min(a, b)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [input() for i in range(N)]
    ans = 0
    ba = 0
    b = 0
    a = 0
    for s in A:
        ans += s.count("AB")
        if s[0] == "B" and s[-1] == "A":
            ba += 1
        elif s[0] == "B":
            b += 1
        elif s[-1] == "A":
            a += 1
    ans += (ba - 1 if ba > 1 else 0)
    if ba > 0 and a > 0 and b > 0:
        b += 1
        a += 1
        ans += min(a, b)
    elif ba > 0 and a == 0 and b == 0:
        pass
    elif ba > 0 and a > 0:
        ans += 1
    elif ba > 0 and b > 0:
        ans += 1
    elif ba == 0:
        ans += min(a, b)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
import re
import math
from copy import deepcopy
import fractions
import random
from heapq import heappop,heappush
import time
import sys
readline = sys.stdin.readline
#import numpy as np
mod=int(10**9+7)
inf=int(10**20)
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def fac_list(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
def get_primes(n,type="number"):
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=2):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def join(A,c=" "):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
#main
n=int(input())
C=[0,0,0]
S=[input() for _ in range(n)]
for a in S:
    if a[0]=="B" and a[-1]=="A":
        C[0]+=1
    elif a[0]=="B":
       C[1]+=1
    elif a[-1]=="A":
        C[2]+=1
count=0
if C[0]==0:
    count+=(min(C[1],C[2]))
elif C[0]>0 and C[1]==0 and C[2]==0:
    count+=C[0]-1
else:
    count+=C[0]+min(C[1],C[2])
for a in S:
    count+=a.count("AB")
print(count)

Submission status: Accepted

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

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

ans += max(0, ba - 1)
if ba:
    if a:
        ans += 1
        a -= 1
    if b:
        ans += 1
        b -= 1

ans += min(a, b)

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = [input() for _ in range(N)]
counter1 = 0
counter2 = 0
counter3 = 0
for s in S:
    if s[-1] == "A":
        if s[0] == "B":
            counter1 += 1
        else:
            counter2 += 1
    elif s[0] == "B":
        counter3 += 1
ans = 0
if counter1 == 0:
    ans += min(counter2, counter3)
else:
    if counter2 + counter3 > 0:
        ans += (counter1 + min(counter2, counter3))
    else:
        ans += (counter1 - 1)

for s in S:
    ans += s.count("AB")
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

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

Submission status: Accepted

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

finA = 0
topB = 0
BtoA = 0
cnt = 0
for l in lst:
    cnt += l.count('AB')
    if l[0]=='B' and l[-1]=='A':
        BtoA += 1
    else:
        if l[0]=='B':
            topB += 1
        elif l[-1]=='A':
            finA += 1

if BtoA==0:
    cnt += min(finA, topB)
else:
    if finA==0 and topB==0:
        cnt += BtoA - 1
    else:
        cnt += BtoA + min(finA, topB)
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())

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

if c == 0:
    print(ans + min(a,b))

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

Submission status: Accepted

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

Submission status: Accepted

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

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

ans=0
if(ba>1):
    ans+=ba-1

if(ba>0):
    if(last_a>0):
        ans+=1
        last_a-=1
    if(first_b>0):
        ans+=1
        first_b-=1
ans+=min(last_a,first_b)
ans+=ab_num
print(ans)