Problem:


Score : 400 points

Problem Statement

Snuke has N strings. The i-th string is s_i.

Let us concatenate these strings into one string after arranging them in some order.
Find the maximum possible number of occurrences of AB in the resulting string.

Constraints

1 \leq N \leq 10^{4}

2 \leq |s_i| \leq 10

s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

For example, if we concatenate ABCA, BAD and XBAZ in this order, the resulting string ABCABADXBAZ has two occurrences of AB.

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4



Sample input:
3
ABCA
XBAZ
BAD


Sample output:
2

Submission status: Wrong Answer

Solution code:

def main():
    buf = input()
    N = int(buf)
    s = []
    for i in range(N):
        buf = input()
        s.append(buf)
    ab_count = 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':
                ab_count += 1
    A_count = 0
    B_count = 0
    AB_count = 0
    for i in range(N):
        if s[i][-1] == 'A' and s[i][0] == 'B':
            AB_count += 1
        elif s[i][-1] == 'A':
            A_count += 1
        elif s[i][0] == 'B':
            B_count += 1
    # B-AB-A
    BABA_dec = min(B_count, AB_count, A_count)
    B_count -= BABA_dec
    AB_count -= BABA_dec
    A_count -= BABA_dec
    ab_count += BABA_dec * 2
    # B-A
    BA_dec = min(B_count, A_count)
    B_count -= BA_dec
    A_count -= BA_dec
    ab_count += BA_dec
    # BA-BA
    ab_count += max(0, AB_count - 1)
    # A-BA or BA-B
    if AB_count > 0 and (A_count > 0 or B_count > 0):
        ab_count += 1
    print(ab_count)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

result = count_ab + min(count_top_b, count_last_a, n - 1)
print(result)

Submission status: Wrong Answer

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

counter = 0
begin_b = []
end_a = []
for i in range(n):
    counter += s[i].count('AB')
    if s[i][0] == 'B':
        begin_b.append(i)
    if s[i][-1] == 'A':
        end_a.append(i)


print(counter + min(len(begin_b), len(end_a)))

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")
if n == ca == cb:
    ca -= 1
print(cnt + min(ca, cb))

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":
        a += 1
    if s[0] == "B":
        b += 1

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

Submission status: Wrong Answer

Solution code:
N = int(input())
P = 0
K = 0
L = 0
ans = 0
for _ in range(N):
  s = input()
  if "AB" in s:
    ans += 1
  if s[0] == "B" and s[-1] == "A":
    P += 1
  elif s[0] == "B":
    K += 1
  elif s[-1] == "A":
    L += 1
if P >= 1:
  ans += (P - 1)
  ans += min(K, L)
  if K != L:
    ans += 1
else:
  ans += min(K, L)
print(ans)

Submission status: Wrong Answer

Solution code:
a = int(input())
s = 0
A=B=AB=0

d = {"A":0,"B":0}
for i in range(a):
    t = input()
    s+=t.count("AB")
    if t[0]=="B" and t[-1]=="A":AB+=1
    elif t[0]=="B":B+=1
    elif t[-1]=="A":A+=1
res = 0
if A==0 and B == 0:
    res = AB-1
elif A==1 and B == 0:
    res = AB
elif A==0 and B ==1:
    res = AB
else:
    res = AB+1 + min(A-1,B-1)
print(res+s)

Submission status: Wrong Answer

Solution code:
N = int(input())
s_list = []
for i in range(N):
    s_list.append(input())
AB_score = 0
back_A = []
front_B = []
for s in s_list:
    tmp = s[0]
    if tmp == "B":
        front_B.append(s_list.index(s))
    for i in range(1, len(s)):
        if tmp == "A" and s[i] == "B":
            AB_score += 1
        tmp = s[i]
    if tmp == "A":
        back_A.append(s_list.index(s))

AB_score += min(len(back_A), len(front_B))
# print(back_A, front_B)
print(AB_score)

Submission status: Wrong Answer

Solution code:
N = int(input())
ab_count = 0
heading_b_count = 0
trailing_a_count = 0
both_sides_count = 0
for _ in range(N):
    s = input()
    ab_count += s.count('AB')
    if s[0] == 'B':
        heading_b_count += 1
    if s[-1] == 'A':
        trailing_a_count += 1
    if s[0] == 'B' and s[-1] == 'A':
        both_sides_count += 1

ans = ab_count + min(heading_b_count, trailing_a_count)
if both_sides_count == trailing_a_count and both_sides_count == heading_b_count:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
Sa = 0
Sb = 0
Sab = 0
ans = 0
for i in range(N):
    t = input()
    if t[-1] == 'A' and t[0] == 'B': Sab += 1
    if t[-1] == 'A': Sa += 1
    elif t[0] == 'B': Sb += 1
    ans += t.count('AB')
if Sa < Sb: Sa += Sab
else: Sb += Sab
ans += min(Sa,Sb)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

ans = 0   # ABが含まれる最大数

cnt1 = 0  # BではじまりAでおわる
cnt2 = 0  # Aでおわる
cnt3 = 0  # Bで始まる
for _ in range(N):
    s = input().strip()
    ans += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A': cnt1 += 1
    elif s[-1] == 'A': cnt2 += 1
    elif s[0] == 'B':  cnt3 += 1

if cnt2 > 0:
    ans += 1
    cnt2 -= 1
# BではじまりAでおわる
ans += cnt1 - 1
# Bではじまる
if cnt3 > 0:
    ans += 1
    cnt3 -= 1
# Aでおわる/Bではじまる
ans += min(cnt2,cnt3)

print(ans)

Submission status: Wrong Answer

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

count_AB = 0
right_A = 0
left_B = 0
right_A_left_B = 0

for i in range(N):
    s = input()
    count_AB += s.count('AB')
    if s[0] == "B" and s[-1] == "A":
        right_A_left_B += 1
    elif s[0] == "B":
        left_B += 1
    elif s[-1] == "A":
        right_A += 1

if right_A_left_B > 0:
    count_AB += (right_A_left_B - 1)
    right_A_left_B = 1

min_AB = min(right_A, left_B)
count_AB += min_AB

# which one is 0
right_A = abs(right_A - min_AB)
left_B = abs(left_B - min_AB)

if right_A_left_B > 0 and (right_A + left_B) > 0:
    count_AB += 1

print(count_AB)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
a, b, = 0, 0
ans = 0
for s in S:
    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:
n = int(input())
cnt = 0
startb = 0
enda = 0
both = 0
S = []
for i in range(n):
    s = input()
    S.append(s)
    cnt += s.count('AB')
    if s[0] == 'B':
        startb += 1
    if s[-1] == 'A':
        enda += 1
    if s[0] == 'B' and s[-1] == 'A':
        both += 1
if both != n:
    print(cnt+min(startb,enda))
else:
    print(cnt+both-1)

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')
    b += 1 if s[0] == 'B' else 0
    a += 1 if s[-1] == 'A' else 0

print(ans + min(a,b))

Submission status: Wrong Answer

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

ct_a=0
ct_b=0
ct_ab=0

for i in range(N):
    if s[i][0]=='B':
        ct_b+=1
    if s[i][-1]=='A':
        ct_a+=1
    for j in range(len(s[i])-1):
        if s[i][j]=='A' and s[i][j+1]=='B':
            if len(s[i])!=2:
                ct_ab+=1
    
print(ct_ab+min(ct_a,ct_b))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = 0
b = 0
ab = 0

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

print(ab + min(a, b))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
ans = 0
a = 0
b = 0
ab = 0
for s in S:
    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] == "AB":
            ans += 1
if ab >= 1:
    ans += max(ab-1, 0)
    if a > b:
        b += 1
    else:
        a += 1
ans += min(a, b)
print(ans)

Submission status: Wrong Answer

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


count=0


end_A=0
start_B=0
both=0
for i in range(N):
	count+=S[i].count("AB")
	if(S[i][-1]=="A"):
		end_A+=1
	if(S[i][0]=="B"):
		start_B+=1
	if(not (S[i][-1]=="A" and (S[i][0]=="B"))):	
		both+=1

#print(end_A,start_B,both,count)
if(both==0):
#if(min(end_A,start_B)<=both):
	print(count+min(end_A,start_B)-1)
else:
	print(count+min(end_A,start_B))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
s = []
for i in range(N):
    s.append(input())
count = 0
bcount = 0
acount = 0
bacount = 0
for j in s:
    count += j.count("AB")
    if j[0] == "B" and j[-1] == "A":
        bacount += 1
    elif j[0] == "B":
        bcount += 1
    elif j[-1] == "A":
        acount += 1
count += min(bcount,acount) + bacount
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = []
temp_sum = 0
A_or_B_count = [0,0]
AB_count = 0
for i in range(N):
	aaa = input()
	temp_sum += aaa.count("AB")
	if aaa[0] == "B" and aaa[-1] == "A":
		AB_count += 1
	elif aaa[-1] == "A":
		A_or_B_count[0] += 1
	elif aaa[0] == "B":
		A_or_B_count[1] += 1
A_or_B_count.sort()
if A_or_B_count[0] + AB_count > A_or_B_count[1]:
	AB_count -= (A_or_B_count[1] - A_or_B_count[0])
	print(temp_sum + min(N-1,AB_count + A_or_B_count[1] - 1))
else:
	print(temp_sum + min(N - 1,A_or_B_count[0] + AB_count))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, s: "List[str]"):
    ABcount = 0
    Aend = 0
    Bstart = 0
    for i in range(N):
        if "AB" in s[i]:
            ABcount += 1
        if s[i][0] == "B":
            Bstart += 1
        if s[i][-1] == "A":
            Aend += 1
    print(ABcount + min(min(Bstart, Aend), N-1))

    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())
s = [input() for i in range(n)]
ss = ""
ans = 0
t1, t2, t3 = 0, 0, 0
for e in s:
  ans += e.count("AB")
  if e[0] == "B" and e[-1] == "A":
    t3 += 1
  elif e[0] == "B":
    t1 += 1
  elif e[-1] == "A":
    t2 += 1
ans += t3//2
if t3:
  t1 += 1
  t2 += 1
ans += min(t1, t2)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[input() for i in range(n)]
ct=0;cta=0;ctb=0;ctab=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' and l[i][len(l[i])-1]=='A':
    ctab+=1
    cta+=1
    ctb+=1
  elif l[i][len(l[i])-1]=='A':cta+=1
  elif l[i][0]=='B':ctb+=1
x=min(cta,ctb)

if ctab==cta and ctab==ctb:
  print(ct+x-1)
else:
  if x==n:x=n-1
  print(ct+x)

Submission status: Wrong Answer

Solution code:
n = int(input())
cnt = 0
s0b = 0
sla = 0
s0bsla = 0
for i in range(n):
    s = input()
    cnt += s.count("AB")
    if s[0] == "B" and s[-1] != "A":
        s0b += 1
    elif s[0] != "B" and s[-1] == "A":
        sla += 1
    elif s[0] == "B" and s[-1] == "A":
        s0bsla += 1
cnt += s0bsla - 1
if sla > 0:
    cnt += 1
    sla -= 1
if s0b > 0:
    cnt += 1
    s0b -= 1
cnt += min(s0b, sla)
print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
numca=0
numbc=0
numba=0
ans=0
for i in range(N):
    s=input()
    for j in range(len(s)):
        if j==0 and s[j]=="B":
            numbc+=1
        elif j==len(s)-1 and s[j]=="A":
            numca+=1
        elif j!=len(s)-1 and s[j]=="A" and s[j+1]=="B":
            ans+=1
    if s[0]=="B" and s[-1]=="A":
        numbc-=1
        numca-=1
        numba+=1
if numba==0:
    print(ans+min(numbc,numca))
    quit()
ans+=numba-1
ans+=min(numbc+1,numca+1)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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 e==int(n):e-=1
if all([c.startswith('B')and c.endswith('A')for c in s]):e-=1
print(sum([c.count('AB')for c in s])+e)

Submission status: Wrong Answer

Solution code:
N = int(input())

moji_list = []
A_count = 0
B_count = 0
AB_count = 0

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

if A_count >= B_count:
    print(AB_count+B_count)
else:
    print(AB_count+A_count)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


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


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, S):
    ans = 0
    pre_b = 0
    post_a = 0
    for s in S:
        if s[0] == 'B':
            pre_b += 1
        if s[-1] == 'A':
            post_a += 1
        ans += s.count('AB')
    ans += min(pre_b, post_a)
    return ans


def main():
    N = read_int()
    S = [read_str() for _ in range(N)]
    print(slv(N, S))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
a = 0
b = 0
ins = 0
data = ""
for i in range(n):
    data = ""
    t = input()
    k = t.split("AB")
    for p in k:
        data += p
    #print(data)
    if t[0] == "B":
        b+=1
    if t[-1] == "A":
        a+=1
    ins += int((len(t)-len(data))/2)
print(min(a, b)+ins)

Submission status: Wrong Answer

Solution code:
n = int(input())
lis = [input() for _ in range(n)]
ab_cnt = sum([l.count('AB') for l in lis])
a_cnt = sum([l[-1] == 'A' for l in lis])
b_cnt = sum([l[0] == 'B' for l in lis])
print(ab_cnt + min(a_cnt, b_cnt))

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")
if ca == cb:
    ca -= 1
print(cnt + min(ca, cb))

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 first_b == same:
    ans -=1
print(ans)

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}

        
    ab += min(len(startB), len(endA))
    if startB == endA: ab -= 1
    print(ab)
    return 0
  
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n = f()[0]
    ans = 0
    cnt_only_head_b = 0
    cnt_only_end_a = 0
    cnt_headb_enda = 0
    for i in range(n):
        s = input()
        slen = len(s)
        for i in range(slen-1):
            if s[i:i+2] == 'AB':
                ans += 1
            
        if s[0] == 'B' and s[-1]!='A':
            cnt_only_head_b += 1
        if s[-1] == 'A' and s[0]!='B':
            cnt_only_end_a += 1
        if s[0] == 'B' and s[-1] == 'A':
            cnt_headb_enda += 1

    

    minab = min(cnt_only_end_a, cnt_only_head_b)
    ans += minab
    cnt_only_end_a -= minab
    cnt_only_head_b -= minab


    if cnt_headb_enda:
        ans += cnt_headb_enda - 1

        if cnt_only_end_a or cnt_only_head_b:
            ans += 1

    print(ans)


    
    

solve()

Submission status: Wrong Answer

Solution code:
def solve():
    N = int(input())
    S = [input() for i in range(N)]
    ans = sum([s.count('AB') for s in S])
    a1 = len([1 for s in S if s[-1] == 'A'])
    b1 = len([1 for s in S if s[0] == 'B'])
    ab1 = len([1 for s in S if s[-1] == 'A' and s[0] == 'B'])

    if a1 > b1:
        return ans + b1
    elif a1 < b1:
        return ans + a1

    if a1 == ab1:
        return ans + ab1 - 1

    return ans + a1

print(solve())

Submission status: Wrong Answer

Solution code:
N = int(input())

count = 0
count_a = 0
count_b = 0
for i in range(N):
    s = input()
    for j in range(1,len(s)):
        if s[j] == 'B' and s[j-1] == 'A':
            count += 1
    if s[0] == 'B':
        count_b += 1
    if s[-1] == 'A':
        count_a += 1

ans = count + min(count_a, count_b)
print(ans)

Submission status: Wrong Answer

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

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

print(ab + min(a, b))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = 0
a = 0
b = 0
c = 0
for i in range(n):
    bo = input()
    if bo[0]=='B' and bo[-1]=='A':
        s+=1
    elif bo[0]=='B':
        b+=1
    elif bo[-1]=='A':
        a+=1
    
    for i in range(len(bo)-1):
        if bo[i]=='A' and bo[i+1]=='B':
            c+=1

c+=min(a,b)
k = min(a,b)
a = a-k
b = b-k
if a!=0:
    a = 1
if b!=0:
    b = 1

if s==0:
    a = 0
    b = 0
s = max(s-1,0)
print(c+s+a+b)

Submission status: Wrong Answer

Solution code:
N=input()
LL=[]
def find(s):
    count=0
    for i in xrange(len(s)-1):
        if s[i]+s[i+1]=='AB':
            count+=1
    return count        

ans=0
start_B=0
end_A=0
same_time=0
for i in xrange(N):
    s=raw_input()
    ans+=find(s)
    if s[-1]=='A':
        end_A+=1
    if s[0]=='B':
        start_B+=1   
    if s[0]=='B' and s[-1]=='A':
        same_time+=1

if end_A!=start_B:
    print(ans+min(end_A,start_B)-same_time+1+(same_time-1)//2)
else:
    print(ans+min(end_A,start_B)-same_time+(same_time)//2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

b = 0
a = 0
ba = 0

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

cnt2 = 0
if ba == b and ba==a:
    cnt2 = ba-1
else:
    cnt2 = min(a,b)

print(cnt+cnt2)

Submission status: Wrong Answer

Solution code:
n,*s=open(0)
a=b=c=d=0
for t in s:
    if t[-2]=='A'and t[0]=='B':
        d+=1
    elif t[-2]=='A':
        a+=1
    elif t[0]=='B':
        b+=1
    c+=t.count('AB')
if all(i==0for i in(a,b)):
    print(c+d-1)
    exit()
ans=min(a,b)+c
dif=abs(a-b)
ans+=min(dif,d)
d-=min(dif,d)
print(ans+d)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
s=[input() for i in range(N)]
ans=0
for st in s:
    ans+= st.count("AB")

a_cnt=0
b_cnt=0
ab_cnt=0
for i in range(N):
    if s[i][-1]=="A":
        a_cnt+=1
    if s[i][0]=="B":
        b_cnt+=1
    if s[i][-1]=="A" and s[i][0]=="B":
        ab_cnt+=1
ans+=min(a_cnt,b_cnt)
if ab_cnt==b_cnt==a_cnt:
    ans-=1

print(ans)

Submission status: Wrong Answer

Solution code:
#c
n = int(input())
stringsA = 0
stringsB = 0
both = 0
ans = 0
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

print(ans + min(stringsA,stringsB) + both)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

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

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

print(ans)

Submission status: Wrong Answer

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

result = 0
ABAB = 0
A = 0
B = 0

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

for S in s:
    AB = S.count("AB")
    result += AB
    if S[0] == "B" and S[-1] == "A":
        ABAB += 1
    elif S[0] == "B":
        B += 1
    elif S[-1] == "A":
        A += 1


if A >= B:
    onon = B
    amari = A - B
else:
    onon = A
    amari = B - A

if ABAB >= 1:
    onon = onon + ABAB - 1

if amari >= 1:
    onon = onon + 1

print(result + onon)

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_ended = 0
    A_begin = 0

    A_ended = 0
    B_begin = 0
    both = 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
        if s[0] == "B" and s[-1] == "A":
            both += 1
    
        
    
    mmA = min(A_ended, B_begin)
    
    # mmB = min(A_begin, B_ended)
    if B_begin == A_ended == both:
        mmA -= 1
    
    print(inner + mmA )
    

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
N = int(input())
cnt = 0
endA = 0
startB = 0
both = 0
for i in range(N):
	s = input()
	flgA = s[-1] == "A"
	flgB = s[0] == "B"
	if flgA and flgB:
		both += 1
	elif flgA:
		endA += 1
	elif flgB:
		startB += 1
	cnt += s.count("AB")
cnt += min(endA, startB)
rest = abs(endA - startB)
if both > 0:
	cnt += both - 1
	if rest > 0:
		cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
a, b, = 0, 0
ans = 0
for s in S:
    if 'AB' in s:
        ans += 1
    if s[-1] == 'A':
        a += 1
    if s[0] == 'B':
        b += 1
print(ans + min(a, 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
B_A = False
for s in s_list:
  ans += s.count("AB")
  if s[0] == "B":
    start_B += 1
  if s[-1] == "A":
    end_A += 1
  if s[0] == "B" and s[-1] == "A":
    B_A = True

ans += min(start_B, end_A)
if B_A:
  ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
# coding:utf-8

import sys
from collections import deque, Counter

INF = float('inf')
MOD = 10 ** 9 + 7
dy, dx = (0, -1, 0, 1), (1, 0, -1, 0)

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    n = II()
    S = [SI() for _ in range(n)]

    cnt = {"B_": 0, "_A": 0, "B_A": 0}
    res = 0
    for s in S:
        for i in range(len(s) - 1):
            if s[i: i + 2] == 'AB':
                res += 1

        if s[0] == 'B' and s[-1] == 'A':
            cnt["B_A"] += 1
        elif s[0] == 'B':
            cnt["B_"] += 1
        elif s[-1] == 'A':
            cnt["_A"] += 1

    # print(res, cnt)
    while cnt["B_A"] > 0:
        if cnt["B_"] < cnt["_A"]:
            cnt["B_"] += 1
        else:
            cnt["_A"] += 1
        cnt["B_A"] -= 1

    # print(res, cnt)

    print(res + min(cnt["B_"], cnt["_A"]))
    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
from collections import Counter

n = getN()
enda = 0
begb = 0
ab = 0
both = 0
for i in range(n):
    s = input()
    if s.startswith("B"):
        enda += 1
    if s.endswith("A"):
        begb += 1
    if s.startswith("B") and s.endswith("A"):
        both += 1
    for a, b in zip(s, s[1:]):
        if a == "A" and b == "B":
            ab += 1

ans = ab + min(enda,begb)
dec = 0
if min(enda,begb) == n:
    dec = 1
if max(enda, begb) == both:
    dec = 1
print(ans-dec)

Submission status: Wrong Answer

Solution code:
N = int(input())

right_A = 0

left_B = 0

#others = 0

cnt = 0

for i in range(N) :
    S = input()
    
    cnt += S.count("AB")
    
    if S[-1] == "A" :
        right_A += 1
    
    if S[0] == "B" :
        left_B += 1
    
ans = min(right_A, left_B) + cnt

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[input() for i in range(n)]
b_b=0
e_a=0
ba=0
ab=0
for i in range(n):
    for j in range(len(s[i])-1):
        if s[i][j:j+2]=='AB':
            ab+=1
    if s[i][0]=='B' and s[i][-1]=='A':
        ba+=1
        continue
    if s[i][0]=='B':
        b_b+=1
    if s[i][-1]=='A':
        e_a+=1
if ba>0:
    ab+=ba-1
    b_b+=1
    e_a+=1
print(ab+min(b_b,e_a))

Submission status: Wrong Answer

Solution code:
import sys

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

N = int(inp())
inAB = 0
sB = 0
eA = 0
for _ in range(N):
  s = inp()
  if 'AB' in s:
    inAB += 1
  if s[0] == 'B':
    sB += 1
  if s[-1] == 'A':
    eA += 1
print(inAB + min(sB, eA))

Submission status: Wrong Answer

Solution code:
import sys
from operator import itemgetter
from collections import deque
sys.setrecursionlimit(10*5)

ans=0
p,q,r=0,0,0
n=int(input())
s=[]
for i in range(n):
    a=input()
    st=a[0]
    ed=a[-1]
    ans+=a.count('AB')
    if st=='B' and ed=='A':
        p+=1
    elif ed=='A':
        q+=1
    elif st=='B':
        r+=1
if q>0 and r>0:
    ans+=p+1+min(q-1,r-1)
elif q==0:
    ans+=p-1 +(1 if r>0 else 0)
elif r==0:
    ans+=p-1 +(1 if q>0 else 0)
    
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
sab = 0
sa = 0
sb = 0

cnt = 0
for i in range(n):
    a = input()
    if a[0] == "B" and a[len(a)-1] == "A":
        sab += 1
    elif a[0] == "B":
        sb += 1
    elif a[len(a)-1] == "A":
        sa += 1
    cnt += a.count("AB")
t = abs(sa-sb)
if sab <= t:
    cnt += (min(sa,sb) + sab)
else:
    sab -= t
    cnt += (max(sa,sb) + sab // 2)

print(cnt)

Submission status: Wrong Answer

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

def count(s):
    p = 0
    result = 0
    while 1:
        q = s.find("AB", p)
        if q < 0:
            break
        p = q + 2
        result += 1
    return result

result = 0

la = 0
sb = 0

for s in S:
    r = count(s)
    # print("*", s, r)
    if s[-1] == "A":
        la += 1
    if s[0] == "B":
        sb += 1
    result += r

result += min(la, sb, N - 1)

print(result)

Submission status: Wrong Answer

Solution code:
n = int(input())
la = 0
fb = 0
c = 0
for _ in range(n):
    x = input()
    n = x.find('AB')
    if n != -1:
        c += 1
    if x[len(x)-1] == 'A':
        la += 1
    if x[0] == 'B':
        fb += 1

c += min(la,fb)
print(c)

Submission status: Wrong Answer

Solution code:
N = int(input())

Ss = []
count = 0
end_A = 0
start_B = 0
for i in range(0,N):
    s = input()
    count += s.count("AB")
    if s[0] == "B":
        start_B += 1
    if s[-1] == "A":
        end_A += 1
    Ss.append(s)

print(count + min(start_B,end_A))