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 solve(N, Ss):
    a = 0
    b = 0
    both = 0
    ans = 0
    for s in Ss:
        ans += s.count("AB")
        if s[-1] == "A" and s[0] == "B":
            both += 1
        else:
            if s[-1] == "A":
                a += 1
            if s[0] == "B":
                b += 1
    if both > 0:
        ans += both - 1
    if both > 0:
        if a > b and a > 0:
            ans += 1
            a -= 1
        if b > a and b > 0:
            ans += 1
            b -= 1

    a = min(N, a)
    b = min(N, b)
    ans += min(a, b)
    return ans


if __name__ == "__main__":
    N = int(input())
    Ss = [input() for _ in range(N)]
    print(solve(N, Ss))

Submission status: Wrong Answer

Solution code:
N = int(input())
Ss=[input() for s in range(N)]
count = len([s for s in Ss if "AB" in s])
headcount = len([s for s in Ss if s[0]=="B"])
outcount = len([s for s in Ss if s[-1]=="A"])
print(count+min(headcount,outcount))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for i in range(N)]
ab = x_a = b_x = b_a = 0
for s in S:
    ab += s.count('AB')
    if s[-1]=='A' and s[0]=='B':
        b_a += 1
    elif s[-1]=='A':
        x_a += 1
    elif s[0]=='B':
        b_x += 1

ans = ab
if b_a:
    ans += b_a - 1
    x_a += 1
    b_x += 1
ans += min(x_a, b_x)
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
N = int(input())

middle = 0
countB = 0
countA = 0

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

sum = min(countB, countA)
if sum == N:
    sum = sum -1

print(middle+sum)

Submission status: Wrong Answer

Solution code:
# diverta 2019 Programming Contest
# https://atcoder.jp/contests/diverta2019
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N = i2n()
    S = ii2ss(N)
    c0b = 0
    c1a = 0
    c0b1a = 0
    c = 0
    for s in S:
        for i, t in enumerate(s[:-1]):
            if t == 'A':
                if s[i+1] == 'B':
                    c += 1
        if s[0] == 'B' and s[-1] == 'A':
            c0b1a += 1
        elif s[0] == 'B':
            c0b += 1
        elif s[-1] == 'A':
            c1a += 1
    print(c + c0b1a + min(c0b, c1a))

main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

b = 0
a = 0

cnt = 0
for x in s:
    if x[0] == "B":
        b+=1
    if x[-1] == "A":
        a+=1
    cnt += x.count("AB")

print(cnt+min(n-1,min(a,b)))

Submission status: Wrong Answer

Solution code:
ans = 0
first_b = 0
last_a = 0
same = 0

N = int(input())

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":
        same +=1

ans += min(first_b, last_a)
if first_b == last_a and same:
    ans -=1
print(ans)

Submission status: Wrong Answer

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


if max(A,B) - min(A,B) <= AB:
    additional = min(A,B) + AB
    print(ans + min(additional, N-1))
else:
    AB -= max(A,B) - min(A,B)
    additional = max(A,B) + AB//2
    print(ans + min(additional, N-1))

Submission status: Wrong Answer

Solution code:
# N = int(input())
# A = [int(x) for x in input().split()]
# from collections import defaultdict
# dd = defaultdict(lambda: 0)

N = int(input())

S = []
bStart = 0
aEnd = 0

abSum = 0
for i in range(N):
    s = input()
    abSum += s.count('AB')
    if s[0] == 'B':
        bStart += 1
    if s[-1] == 'A':
        aEnd += 1

print(abSum + min(bStart,aEnd))

Submission status: Wrong Answer

Solution code:
N = int(input())
ss = list(input() for _ in range(N))
ans = 0
a_end = 0
b_start = 0
both = 0
for si in ss:
    ans += si.count("AB")
    if si[0]=="B" and si[-1]=="A":
        both += 1
    elif si[0]=="B":
        b_start += 1
    elif si[-1]=="A":
        a_end += 1

if both > 0:
    ans += both - 1
    if max(a_end, b_start)>0:
        if max(a_end, b_start)==a_end:
            a_end -= 1
            ans += 1
        else:
            b_start -= 1
            ans += 1
ans += min(a_end, b_start)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
s_list=[]
count_list=[]
first=[]
last=[]
for i in range(n):
    s=input()
    length=len(s)
    first.append(s[0])
    last.append(s[length-1])
    count_list.append(s.count("AB"))
    s_list.append(s)
print(sum(count_list)+min(first.count("B"),last.count("A")))

Submission status: Wrong Answer

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


def solve(N: int, S: "List[str]"):
    ret = 0
    a = 0
    b = 0
    for s in S:
        i = 0
        for i in range(len(s) - 1):
            if s[i:i+2] == 'AB':
                ret += 1
        if s[0] == 'B':
            a += 1
        if s[-1] == 'A':
            b += 1
    ret += min(a, b)
    print(ret)
    return


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())
a=[input() for i in range(n)]
count=0
ca=0
cb=0
cc=0
for i in a:
    count+=i.count('AB')
    if i[0]=='B':cb+=1
    if i[-1]=='A':ca+=1
    if i[0]=='B' and i[-1]=='A':cc+=1
ans=count+min(ca,cb)
if max(ca,cb)==cc:ans-=1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

N=int(input())
S=[input().strip() for i in range(N)]

ANS=0
for s in S:
    ANS+=s.count("AB")


_A=0
B_=0
BA=0

for s in S:
    if s[0]=="B" and s[-1]=="A":
        BA+=1
    elif s[0]=="B":
        B_+=1
    elif s[-1]=="A":
        _A+=1


#print(ANS,_A,B_,BA)

if _A!=0 or B_!=0:
    print(ANS+BA+min(_A,B_))

else:
    print(ANS+BA-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

n = int(input())

strs = [ input() for _ in range(n) ]

ab = sum(map(lambda s:s.count("AB"), strs))
# print(ab)

last_a = sum(map(lambda s:1 if s.endswith("A") else 0, strs))

first_b = sum(map(lambda s:1 if s.startswith("B") else 0, strs))

print(ab + min(last_a, first_b))

Submission status: Wrong Answer

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

ans = 0
start_B = 0
end_A = 0
for s in s_list:
  ans += s.count("AB")
  if s[0] == "B":
    start_B += 1
  if s[-1] == "A":
    end_A += 1

ans += min(start_B, end_A)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
ab = 0
frontb = 0
backa = 0
count = 0
for i in range(n):
    s = input()
    for j in range(len(s)-1):
        if s[j] == 'A' and s[j+1] == 'B':
            count += 1
    if s[0] == 'B':frontb += 1
    if s[-1] == 'A':backa += 1
if frontb + backa == n*2:
    count += backa-1
else:
    count += min(backa,frontb)
print(count)

Submission status: Wrong Answer

Solution code:
import sys, math, collections, heapq, itertools
F = sys.stdin
def single_input(): return F.readline().strip("\n")
def line_input(): return F.readline().strip("\n").split()
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while a % b > 0:
        a, b = b, a % b
    return b
  
def solve():
    N = int(single_input())
    ab = 0
    startB, endA = set(), set()
    for i in range(N):
        s = str(single_input())
        for j in range(len(s) - 1):
            if s[j:j+2] == "AB": ab += 1
        if s[0] == "B": startB |= {i}
        if s[-1] == "A": endA |= {i}

    single = False
    for item in endA:
        if item not in startB:
            single = True
        
    ab += min(len(startB), len(endA))
    if not single and len(startB) == len(endA): ab -= 1
    print(ab)
    return 0
  
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

n = int(input())
t = [0]
for i in range(n):
    t.append(list(input()))
del t[0]

a = 0
b = 0
ab = 0
for i in range(n):
    temp = "".join(t[i])
    ab += temp.count("AB")
    if t[i][0] == "B":
        b += 1
    if t[i][-1] == "A":
        a += 1

temp = min(a,b,n-1)

print(ab + temp)

Submission status: Wrong Answer

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

a = 0 #うしろがA
b = 0 #先頭がB
for i in range(n):
    s = list(input())
    for k in range(len(s)-1):
        if s[k] == "A" and s[k+1] == "B":
            counter += 1
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1

print(counter + min(a,b))

Submission status: Wrong Answer

Solution code:
#C
N = int(input())
cnt = 0
headB, tailA = 0, 0
for _ in range(N):
    si = input()
    cnt += si.count('AB')
    if si[0] == 'B': headB += 1
    if si[-1] == 'A': tailA += 1
headB = min(headB, N-1)
tailA = min(tailA, N-1)
cnt += min(headB, tailA)
print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())

a=0
b=0
ab=0

for i in range(n):
    line=input()
    if line[0]=='B':
        b=b+1
    if line[len(line)-1]=='A':
        a=a+1
    for j in range(len(line)-1):
        if line[j]=='A' and line[j+1]=='B':
            ab=ab+1

if b>a:
    print(ab+a)
else:
    print(ab+b)

Submission status: Wrong Answer

Solution code:
s=[input() for _ in range(int(input()))]
print(sum(r.count("AB") for r in s)+min(sum(1 for r in s if r.startswith("B")), sum(1 for r in s if r.endswith("A"))))

Submission status: Wrong Answer

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



if a < b:
    res += a
    b -= a
    res += ab
else:
    res += b
    res += ab
print(res)

Submission status: Wrong Answer

Solution code:
n = int(input())

countA = 0
countB = 0
countAB = 0
for i in range(n):
    s = input()

    findAB = 0
    while(findAB != -1):
        findAB = s.find("AB", findAB)
        if(findAB != -1):
            countAB += 1
            findAB += 1

    if(s[0]=="B"):
        countB += 1
    if(s[len(s)-1]=="A"):
        countA += 1

ans = min(countA, countB) + countAB
print(ans)

Submission status: Wrong Answer

Solution code:


def main():
    n = input()
    a = b = s = 0
    for _ in xrange(n):
        st = raw_input()
        s += st.count('AB')
        if st.endswith('A'):
            a += 1
        if st.startswith('B'):
            b += 1
    if a == n:
        a -= 1
    if b == n:
        b -= 1
    print s + min(a, b)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    ends_A = 0
    starts_B = 0
    include_AB = 0
    for i in range(N):
        s = input()
        if s[-1] == "A":
            ends_A += 1
        if s[0] == "B":
            starts_B += 1
        aflag = 0
        for c in s:
            if aflag and c == "B":
                include_AB += 1
            if c == "A":
                aflag = True
            else:
                aflag = False
    print(min(ends_A, starts_B) + include_AB)
            
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())

b = 0
a = 0
count = 0
for i in range(N):
    s = input()
    count += sum([1 for j in range(len(s)) if s[j:j+2]=="AB"])
    if s[-1] == "A":
        print(s)
        a += 1
    if s[0] == "B":
        b += 1

if a == 0:
    a = N-1
if b == 0:
    b = N-1
#print(a, b)
print(count+min([a, b, N-1]))

Submission status: Wrong Answer

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

enda = 0
startb = 0
btoa = 0

ans = 0

for i in s:
    if i[0] == 'B' and i[-1] == 'A':
        btoa += 1
    elif i[-1] =='A':
        enda += 1
    elif i[0] == 'B':
        startb += 1
    ans += i.count('AB')

if enda < startb:
    enda, startb = startb, enda
ans += startb
enda -= startb
ans += max(0, btoa-1)
if enda and btoa:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
# from sys import exit
N = int(input())
S = [str(input()) for _ in range(N)]

Astart = set()
Bstart = set()
Aend = set()
Bend = set()
ABcon = set()
for s in S:
    if(s[0] == "A"):
        Astart.add(s)
    elif(s[0] == "B"):
        Bstart.add(s)
    if(s[-1] == "A"):
        Aend.add(s)
    elif(s[-1] == "B"):
        Bend.add(s)
    if("AB" in s):
        ABcon.add(s)

canAB0 = min(len(Astart), len(Bend))
canAB1 = min(len(Bstart), len(Aend))
print(canAB0 + canAB1 + len(ABcon))

Submission status: Wrong Answer

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

ans = 0
for ss in sss:
    ans += ss.count('AB')

num_A = numB_ = numBA = 0
for ss in sss:
    is_A = ss.endswith('A')
    isB_ = ss.startswith('B')
    if is_A:
        num_A += 1
    if isB_:
        numB_ += 1
    if is_A and isB_:
        numBA += 1

ans += min(num_A, numB_)
if num_A == numBA:
    ans -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[]
ctr=0
ctr_a=0
ctr_b=0
for i in range(n):
    s.append(str(input()))
    if "AB" in s[i]:
        ctr+=s[i].count("AB")
    if s[i][-1]=="A":
        ctr_a+=1
    if s[i][0]=="B":
        ctr_b+=1
    
print(ctr+min(ctr_a,ctr_b))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for i in range(n)]
ans = 0
for i in range(n):
    for j in range(len(s[i])-1):
        if s[i][j] == "A" and s[i][j+1] == "B":
            ans += 1

cnt_A = 0
cnt_B = 0
cnt_BA = 0
for i in range(n):
    if s[i][0] == "B" and s[i][-1] == "A":
        cnt_BA += 1
    elif s[i][0] == "B":
        cnt_B += 1
    elif s[i][-1] == "A":
        cnt_A += 1 
ans += max(cnt_BA - 1, 0)
ans += min(cnt_B, cnt_A)

if cnt_BA > 0 and cnt_B != cnt_A:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = []
for _ in range(N):
    S.append(input())
ans = 0
a = list(filter(lambda x: "AB" not in x, S))
ans += len(S)-len(a)
b = list(filter(lambda x: x[-1]=="A", S))
c = list(filter(lambda x: x[0]=="B", S))
ans += min(len(b),len(c))
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

a_end = 0
b_start = 0
ab = 0
for n in range(N):
    s = input()
    ab = ab + s.count('AB')
    if s[-1] == 'A':
        a_end = a_end + 1
    if s[0] == 'B':
        b_start = b_start + 1

print(ab + min(a_end, b_start, N - 1))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
N = int(input())
S = [input() for i in range(N)]

ac = 0
bc = 0
abc = 0
for s in S:
    if s[-1] == 'A':
        ac += 1
    if s[0] == 'B':
        bc += 1
    abc += s.count('AB')
print(abc + min(ac, bc))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import collections as c
ip = lambda : map(int, input().split())

#################
N = int(input())
count_ab = 0
clis = [0,0,0] # [f,b,f and b]

for i in range(N):
    s = input()
    if s[0] == 'B' and s[-1] == 'A':
        clis[2] += 1
    elif s[0] == 'B':
        clis[0] += 1
    elif s[-1] == 'A':
        clis[1] += 1
    for i in range(len(s)-1):
        if s[i:i+2] == 'AB':
            count_ab+=1
if clis[0] == 0 and clis[1] == 0:
    count_ab -= 1

#print(count_ab,clis)
print(min(clis[0:2]) + clis[2] + count_ab)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in range(n)]
start_b = len([i for i in s if i[0] == "B"])
end_a = len([i for i in s if i[-1] == "A"])
include_ab = sum([i.count("AB") for i in s])
print(include_ab + min(start_b,end_a))

Submission status: Wrong Answer

Solution code:
n = int(input())
st = [input() for _ in range(n)]
count = 0
for s in st:
    count += s.count('AB')
first = 0
last = 0
both = 0
for s in st:
    if s[0] == 'B' and s[-1] == 'A':
        both += 1
    elif s[0] == 'B':
        first += 1
    elif s[-1] == 'A':
        last += 1
print(count + min(first, last) + both)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[input() for _ in range(n)]
cnt_ab=0
cnt_a=0
cnt_b=0
for i in range(n):
    cnt_ab += s[i].count('AB')
    if s[i][-1] == 'A':
        cnt_a += 1
    if s[i][0] == 'B':
        cnt_b += 1
cnt_ab += min(cnt_a,cnt_b)
if min(cnt_a,cnt_b) == n:
    cnt_ab -= 1
print(cnt_ab)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[input() for i in range(n)]
ct=0;cta=0;ctb=0
for i in range(len(l)):
  for j in range(len(l[i])-1):
    if l[i][j]=='A' and l[i][j+1]=='B':ct+=1
  if l[i][0]=='B':ctb+=1
  if l[i][len(l[i])-1]=='A':cta+=1
x=min(cta,ctb)
if x==10:x=9
print(ct+x)

Submission status: Wrong Answer

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

ab_cnt = 0
a_saigo = 0
b_atama = 0
for s in s_list:
    ab_cnt += s.count("AB")
    if s[0] =="B":
        b_atama += 1
    if s[-1] == "A":
        a_saigo += 1
result = ab_cnt + min(b_atama,a_saigo)
print(result)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env pypy3

import math, itertools, operator
from math import *
from math import factorial as fact
from itertools import permutations, combinations
from functools import reduce
from statistics import mean, median, median_low, median_high, mode
# Before Python 3.5
from fractions import gcd

#import numpy
#import scipy, scipy.special
#from sympy import isprime, nextprime
#from sympy.ntheory import factorint

inf = float('inf')

def lcm(a, b):
    return a * b // gcd(a,b)

def perm(a, b):
    import scipy.special
    return scipy.special.perm(a, b, exact = True)

def comb(a, b):
    import scipy.special
    return scipy.special.comb(a, b, exact = True)

def dfact(n):
    import scipy.special
    return scipy.special.factorial2(n, exact = True)

def prod(l):
    return reduce(operator.mul, l)

def input_ints(inp = input, sep = ' ', conv = int):
    return map(conv, inp().split(sep = sep))

################################################################################


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

n_AB = 0
n_endswithA = 0
n_startswithB = 0
for i in range(N):
    n_AB += si[i].count('AB')
    if si[i].endswith('A'):
        n_endswithA += 1
    if si[i].startswith('B'):
        n_startswithB += 1

print(n_AB + min(n_endswithA, n_startswithB))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in range(n):
    s = input()
    q = len(s)
    for j in range(q):
        if j == 0 and s[j] == 'B':
            b[i] = 1
        elif j == q-1 and s[j] == 'A':
            a[i] = 1
        if j >0:
            if s[j] == 'B' and s[j-1] == 'A':
                ans += 1

print(ans + min(sum(a),sum(b)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 0

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


a = 0
b = 0
for i in range(n):
    if s[i][0] == 'B':
        a += 1
    if s[i][-1] == 'A':
        b += 1

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

Submission status: Wrong Answer

Solution code:
N=int(input())
end_with_A=0
start_with_B=0
count=0
for i in range(N):
    s=input()
    count+=s.count("AB")
    if s[-1]=="A":
        end_with_A+=1
    if s[0]=="B":
        start_with_B+=1

print(count+min(end_with_A,start_with_B))

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[-1]=='A':
        a+=1
    if s[0]=='B':
        b+=1
print(ans+min(a,b))

Submission status: Wrong Answer

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

    inner = 0
    for s in S:
        inner += s.count("AB")
    
    B_begin = 0
    B_ended = 0
    A_begin = 0
    A_ended = 0
    for s in S:
        if s[0] == "B": B_begin += 1
        if s[-1] == "B": B_ended += 1
        if s[0] ==  "A": A_begin += 1
        if s[-1] == "A": A_ended += 1
        
    
    mmA = min(A_ended, B_begin)
    mmB = min(A_begin, B_ended)
    
    print(inner + mmA + mmB)
    

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
n = int(input())
sl = [input() for _ in range(n)]
sb,la,cnt,both = 0,0,0,0
for s in sl:
  cnt += s.count("AB")
  if s[0] == "B":
    sb += 1
  if s[-1] == "A":
    la += 1
  if s[0] + s[-1] == "BA":
    both += 1
if sb == both or la == n:
  cnt -= 1
print(cnt + min(la,sb))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
fir=0
sec=0
same=0
ret = 0
for i in range(n):
    s = input()
    if 'AB' in s:
        ret+=1
    if s[0]=='B':
        fir+=1
    if s[-1]=='A':
        sec+=1
    if s[0]=='B' and s[-1]=='A':
        same+=1
print(ret+min(fir,sec)-same)

Submission status: Wrong Answer

Solution code:
n,*s=map(str,open(0).read().split())
b=sum([c.startswith('B')for c in s])
a=sum([c.endswith('A')for c in s])
e=a if b>=a else b
if int(n)==e:e-=1
print(sum([c.count('AB')for c in s])+e)

Submission status: Wrong Answer

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

if max(A,B) - min(A,B) <= AB:
    print(ans + min(A,B) + AB)
else:
    AB -= max(A,B) - min(A,B)
    print(ans + max(A,B) + AB//2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())

a = 0
b = 0
ans = 0
c = 0

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq as hq
import itertools as its
import operator as op


# globals
inf = float('inf')
N = 0


def set_inputs():
    global N
    return


def main():
    setrecursionlimit(100000)
    set_inputs()
    # ----------MAIN----------
    n = get_int()
    s = get_data(n, [str])
    ans = 0
    a = 0
    b = 0
    ba = 0
    for si in s:
        if si[-1] == 'A':
            a += 1
        if si[0] == 'B':
            b += 1
        if si[-1] == 'A' and si[0] == 'B':
            ba += 1
        for c1, c2 in zip(si, si[1:]):
            if c1 == 'A' and c2 == 'B':
                ans += 1
    if ba != a or ba != b:
        ans += min(a, b)
    else:
        ans += ba - 1
    print(ans)
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types, sep=None):
    if len(types) == 1:
        return [types[0](input().strip()) for _ in range(n)]
    return list(zip(*(
        [t(x) for t, x in zip(types, input().strip().split(sep=sep))]
        for _ in range(n)
    )))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
cnt_a=0
cnt_b=0
cnt_ab=0
for i in range(n):
  s=input()
  if s[0]=='B':
    cnt_b+=1
  if s[-1]=='A':
    cnt_a+=1
  for i in range(len(s)-1):
    if s[i:i+2]=='AB':
      cnt_ab+=1
print(cnt_ab+min(cnt_a,cnt_b))

Submission status: Wrong Answer

Solution code:
n = int(input())

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

print(ans + min(k, j))

Submission status: Wrong Answer

Solution code:
n=int(input())
fir=0
sec=0
same=0
ret = 0
for i in range(n):
    s = input()
    if 'AB' in s:
        ret+=1
    if s[0]=='B':
        fir+=1
    if s[-1]=='A':
        sec+=1
    if s[0]=='B' and s[-1]=='B':
        same+=1
print(ret+min(fir,sec)-same)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())
s = 0
t = 0
u = 0
A = []
for i in range(N):
    m = input()
    A.append(m)
    u += m.count("AB")
    if m[0] == "B":
        s += 1
    if m[-1] == "A":
        t += 1
u += min(s, t)
print(u)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
Alist = [word for word in S if word[-1] == 'A']
Blist = [word for word in S if word[0] == 'B']
Slist = [word for word in S if 'AB' in word]
C = min(len(Alist),len(Blist))
print(len(Slist)+min(C,N-1))

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
s = [ns() for _ in range(n)]

ans = 0

x = y = z = 0

for si in s:
    # 内部のABカウント
    ans += si.count('AB')

    # 右端A かつ 左端B ... Z
    if si[:1] == 'B' and si[-1:] == 'A':
        z += 1

    # 右端A ... X
    elif si[-1:] == 'A':
        x += 1

    # 左端B ... Y
    elif si[:1] == 'B':
        y += 1

if max(x,y) - min(x,y) >= z:
    ans += min(x,y) + z

else:
    ans += max(x,y) + z//2

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
slist = [input().strip() for _ in range(n)]

ea, sb = 0, 0
ans = 0
for s in slist:
  for prevs, _s in zip(s, s[1:]):
    if prevs + _s == 'AB':
      ans += 1
  if s[0] == 'B':
    sb += 1
  if s[-1] == 'A':
    ea += 1

if ea == sb == len(slist):
  print(ans + ea - 1)
else:
  print(ans + min(sb, ea))

Submission status: Wrong Answer

Solution code:
n=int(input())
cnt=0
cnta=0
cntb=0
for i in range(n):
    s=input()
    if 'AB' in s:
        cnt+=1
    if s[-1]=="A":
        cnta+=1
    if s[0]=="B":
        cntb+=1
#print(cnt,cnta,cntb)
print(cnt+min(cnta,cntb))

Submission status: Wrong Answer

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