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():
    from sys import stdin, stdout

    def read():
        return stdin.readline().rstrip('\n')

    def read_array(sep=None, maxsplit=-1):
        return read().split(sep, maxsplit)

    def read_int():
        return int(read())

    def read_int_array(sep=None, maxsplit=-1):
        return [int(a) for a in read_array(sep, maxsplit)]

    def write(*args, **kwargs):
        sep = kwargs.get('sep', ' ')
        end = kwargs.get('end', '\n')
        stdout.write(sep.join(str(a) for a in args) + end)

    def write_array(array, **kwargs):
        sep = kwargs.get('sep', ' ')
        end = kwargs.get('end', '\n')
        stdout.write(sep.join(str(a) for a in array) + end)

    n = read_int()
    b_starts = 0
    a_ends = 0
    both = 0
    count = 0

    for i in range(n):
        word = read()
        if word[0] == 'B':
            b_starts += 1
        if word[-1] == 'A':
            a_ends += 1
        if word[0] == 'B' and word[-1] == 'A':
            both += 1
        for i in range(1, len(word)):
            if word[i] == 'B' and word[i-1] == 'A':
                count += 1
    if a_ends > both and b_starts > both:
        count += both + 1
        a_ends -= 1
        b_starts -= 1
    elif a_ends > both:
        count += both
        a_ends -= 1
    elif b_starts > both:
        count += both
        b_starts -= 1
    else:
        count += both - 1
    a_ends -= both
    b_starts -= both
    count += min(a_ends, b_starts)

    write(count)

main()

Submission status: Wrong Answer

Solution code:
N = int(input())

strings = []
for _ in range(N):
    strings.append(input())

cnt = 0
cnt_A = cnt_B = 0
flag = False
for s in strings:
    for c1, c2 in zip(s, s[1:]):
        if c1 == 'A' and c2 == 'B':
            cnt += 1

    if s[0] == 'B':
        cnt_B += 1
        if s[-1] != 'A':
            flag = True
    if s[-1] == 'A':
        cnt_A += 1
        if s[0] != 'B':
            flag = True

cnt += min(cnt_A, cnt_B)
print(cnt if flag else cnt - 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

N = int(input())
acount = 0
bcount = 0
ab = 0
ans = 0
for i in range(N):
    s = input()
    ans += s.count("AB")
    if s[-1] == "A" and s[0] == "B":
        ab += 1

    if s[-1] == "A":
        acount += 1
    if s[0] == "B":
        bcount += 1
add = min (acount,bcount)
if ab == acount and ab == bcount:
    add -= 1
if ans + add < 0:
    print(0)
else:
    print(ans + add)

Submission status: Wrong Answer

Solution code:
n = int(input())
bb = 0
af = 0
ab = 0
p = 1
for i in range(n):
  s = input()
  l = len(s)
  if s[0] == "B":
    bb += 1
  if s[l - 1] == "A":
    af += 1
  if s[0] == "B" and s[l - 1] != "A":
    p = 0
  if s[0] != "B" and s[l - 1] == "A":
    p = 0
  for j in range(l - 1):
    if s[j] == "A":
      if s[j + 1] == "B":
        ab += 1
if af == bb:
  ans = ab + af - p
else:
  ans = ab + min(af, bb)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

s = []
count = 0
A = 0
B = 0
AB = 0
for i in range(N):
    s.append(input())
    count += s[i].count('AB')
    if s[i][0] == 'B':
        B += 1
    if s[i][-1] == 'A':
        A += 1
    if s[i][0] == 'B' and s[i][-1] =='A':
        AB += 1
print(count + (AB - 1 if A == B and A == AB else min(A,B)))

Submission status: Wrong Answer

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

ans += n_BA - 1
if n_A >= 1:
    n_A -= 1
    ans += 1
if n_B >= 1:
    n_B -= 1
    ans += 1
ans += min(n_A, n_B)
print(ans)

Submission status: Wrong Answer

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

ans = 0
for i in s:
	ans += i.count("AB")

# print(ans)

A = 0
B = 0
AB = 0

for i in s:
	if i[0] == 'B' and i[-1] == 'A':
		AB += 1
	elif i[0] == 'B':
		B += 1
	elif i[-1] == 'A':
		A += 1


if AB > 1:
	ans += AB-1
	AB = 1

# print(A,B,AB)
pair = min(A,B)
ans += pair
# print(ans)
rest = abs(A-B)

if AB < rest:
	ans += AB
else:
	ans += rest + (AB-rest)//2

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
total_cand = 0
tail_a = 0
head_b = 0

for i in range(N):
  str = input()
  ab_inside_str = str.count('AB')
  total_cand = total_cand + ab_inside_str
  if (str[-1:] == "A"):
    tail_a = tail_a + 1
  if (str[0] == "B"):
    head_b = head_b + 1


total_cand = total_cand + min(tail_a, head_b)
print(total_cand)

Submission status: Wrong Answer

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

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

if min(sum(a),sum(b)) == n:
    ans -= 1


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

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

if a==b and a != 0:
    print(counter + min(a,b)-1)
else:
    print(counter + min(a,b))

Submission status: Wrong Answer

Solution code:
n=int(input())
count=0
bx=0
xa=0
ba=0
for i in range(n):
    s=input()
    for j in range(0,len(s)-2):
        if s[j:j+2]=='AB':
            count=count+1
    if s[0]=='B' and s[len(s)-1]=='A':
        ba=ba+1
    if s[0]=='B' and not s[len(s)-1]=='A':
        bx=bx+1
    if not s[0]=='B' and s[len(s)-1]=='A':
        xa=xa+1
if bx*xa==0:
    if ba>=2:
        count=count+ba-1
else:
    count=count+ba+min(xa,bx)
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())

a_end = 0
b_start = 0
ab = 0
zz = 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
    if s[-1] != 'A' and s[0] != 'B':
        zz = zz + 1

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
N=int(input())
ans=0
a,b=0,0
tmp=0
for i in range(N):
    s=input()
    if s[0]=="B": b+=1
    if s[-1]=="A": a+=1
    if a!=b: tmp=1
    ans+=s.count("AB")

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

Submission status: Wrong Answer

Solution code:
x = int(input())
a = 0
b = 0
c = 0
f = 1
bun = []
for p in range(x):
    bun = input()
    if bun[0] == "B":
        b +=1
    if bun[len(bun)-1] == "A":
        a += 1
        if bun[0] == "A":
            f = 0
    for k in range(len(bun)-1):
        if bun[k] == "A":
            if bun[k+1] == "B":
                c += 1
if a == x:
    a = a-1
if a==0:
    if b==0:
        f = 0

if a < b:
    print(a+c-f)
else:
    print(b+c-f)

Submission status: Wrong Answer

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

ab_count = 0
b_start = 0
a_end = 0

for s in s_list:
    if s[0] == 'B':
        b_start = b_start + 1
    if s[-1] == 'A':
        a_end = a_end + 1
    ab_count = ab_count + s.count('AB')

print(ab_count + min(min(a_end, b_start), n - 1))

Submission status: Wrong Answer

Solution code:
n=int(input())
cnt=0
cnta=0
cntb=0
cntba=0
for i in range(n):
    s=input()
    for i in range(1,len(s)):
        if s[i]=='B' and s[i-1]=='A':
            cnt+=1
    if s[-1]=='A' and s[0]=='B':
        cntba+=1
    elif s[-1]=="A":
        cnta+=1
    elif s[0]=="B":
        cntb+=1
print(cnt,cnta,cntb,cntba)
if cntb==0 and cntba==0:
    print(cnt)
elif cntb==0 and cntba!=0:
    print(cnt+min(cntba+cntb,cntba+cnta-1))
else:
    print(cnt+min(cntb+cntba,cnta+cntba))

Submission status: Wrong Answer

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

N = int(input())
s_li = []
for _ in range(N):
    s_li.append(input().strip())

last_is_A = 0
first_is_B = 0
AB_counter = 0
for i in s_li:
    if i[-1] == 'A': last_is_A += 1
    if i[0] == 'B': first_is_B += 1
    AB_counter += i.count('AB')

print(AB_counter + min(last_is_A, first_is_B, N-1))

Submission status: Wrong Answer

Solution code:
import re

N = int(input())
s = [input() for _ in range(N)]

num_end_with_A = 0
num_start_with_B = 0
num_both = 0
num_inner_AB = 0

for ss in s:
    res = re.findall('AB', ss)
    if res:
        num_inner_AB += len(res)
    if ss[0] == 'B' and ss[-1] == 'A':
        num_both += 1
    elif ss[0] == 'B':
        num_start_with_B += 1
    elif ss[-1] == 'A':
        num_end_with_A += 1

print(num_inner_AB + min(num_end_with_A, num_start_with_B) + num_both)

Submission status: Wrong Answer

Solution code:
n = int(input())
ary = []
ans = 0
final_a = 0
start_b = 0
ab = 0
for i in range(n):
  s = input()
  ary.append(s)
  if s[0] == "B" and s[-1] == "A":
    ab += 1
  else:
    if s[0] == "B":
      start_b += 1
    if s[-1] == "A":
      final_a += 1
  while True:
    idx = s.find("AB")
    if idx == -1:
      break
    else:
      s = s[idx+2:]
      ans += 1

ans += ab - 1
if start_b > 0:
  ans += 1
  start_b -= 1
if final_a > 0:
  ans += 1
  final_a -= 1

if start_b >= final_a:
  ans += final_a
else:
  ans += start_b
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = [input() for _ in range(N)]
ctrAB = 0
lastA = 0
firstB = 0
lAfB = 0
for s in L:
    if s[0] == 'B' and s[-1] == 'A':
        lAfB += 1
    if s[0] == 'B':
        firstB += 1
    if s[-1] == 'A':
        lastA += 1
    ctrAB += s.count('AB')
if lAfB == lastA and lAfB == firstB:
    print(ctrAB + lAfB-1)
else:
    print(ctrAB + min(lastA,firstB))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


ans = 0
A = B = BA = 0
for s in S:
    for i in range(len(s) - 1):
        if s[i] + s[i + 1] == 'AB':
            ans += 1

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


if A:
    ans += BA
    A -= 1
    if B:
        ans += 1
        B -= 1
    ans += min(A, B)
else:
    ans += (BA - 1)
    if B:
        ans += 1

print(ans)

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 = 0
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 += 1

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

Submission status: Wrong Answer

Solution code:
n=int(input())
f=0
b=0
c=0
d=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
    if s[0]=="B" and s[len(s)-1]=="A":
        d+=1
print(c+min(f,b)-d)

Submission status: Wrong Answer

Solution code:
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    ans = 0
    last_a = 0
    first_b = 0
    for _ in range(N):
        s = str(input().strip())
        for i in range(len(s)-1):
            if s[i:i+2] == 'AB':
                ans += 1

        if s[0] == 'B' and last_a > 0:
            ans += 1
            last_a -= 1
            s = 'b' + s

        if s[-1] == 'A' and first_b > 0:
            ans += 1
            first_b -= 1
            s = s + 'a'

        if s[0] == 'B':
            first_b += 1

        if s[-1] == 'A':
            last_a += 1

    return ans


if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

mod = 10**9+7

N = int(input())
s = [0 for i in range(N)]
cnt = 0
chance = False
A_cnt = 0
B_cnt = 0
for i in range(N):
    s[i] = str(input())
    cnt += s[i].count("AB")
    if s[i][0] == "B":
        B_cnt += 1
    if s[i][-1] == "A":
        A_cnt += 1
cnt += min(A_cnt,B_cnt)

print(cnt)

Submission status: Wrong Answer

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

ans = 0
start_b = 0
end_a = 0
ab = 0

for i in range(n):
    for j in range(len(S) - 1):
        if S[i][j:j+2] == 'AB':
            ans += 1

    if S[i][0] == 'B' and S[i][-1] == 'A':
        ab += 1
        continue 
    if S[i][0] == 'B':
        start_b += 1
    if S[i][-1] == 'A':
        end_a += 1
    
ans += min(start_b, end_a)
if ab > 0:
    ans += ab - 1
    ans += min(1, abs(start_b - end_a))

print(ans)

Submission status: Wrong Answer

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

a = 0
b = 0
a_b = 0

for s in S:
    if s[-1] == "A" and s[0] == "B":
        a_b += 1
    elif s[-1] == "A":
        a += 1
    elif s[0] == "B":
        b += 1

ans = 1 + a_b - 1 + 1 + (a-1+b-1)//2
for line in S:
    for i in range(len(line)-1):
        if (line[i],line[i+1]) == ("A","B"):
            ans += 1
print(ans)

Submission status: Wrong Answer

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

count = 0
for i in range(n):
    for j in range(len(s[i])-1):
        if s[i][j:j+2] == "AB":
            count += 1
a_end = 0
b_start = 0
both = 0
for i in range(n):
    if s[i][0] == "B":
        if s[i][-1] == "A":
            both += 1
        else:
            b_start += 1
    elif s[i][-1] == "A":
        a_end += 1
if a_end == 0 and b_start == 0:
    count += (both-1)
else:
    count += (both + min(a_end, b_start))
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = [0] * N
for i in range(N):
	s[i] = input()
ans = 0
for j in range(N):
	ans += s[j].count("AB")
a, b, ab = 0, 0, 0
for k in range(N):
	if s[k][-1] == "A":
		a += 1
	if s[k][0] == "B":
		b += 1
		if s[k][-1] == "A":
			ab += 1
if a == b and b == ab:
	ans += ab - 1
else:
	ans += min(a, b)
print(ans)

Submission status: Wrong Answer

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

print(s_list)
ab_count = 0
b_start = 0
a_end = 0

for s in s_list:
    if s[0] == 'B':
        b_start = b_start + 1
    if s[-1] == 'A':
        a_end = a_end + 1
    ab_count = ab_count + s.count('AB')

print(ab_count + min(min(a_end, b_start), n - 1))

Submission status: Wrong Answer

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

cnt = 0
enda = 0
startb = 0
for s in strs:
    cnt += s.count("AB")
    enda += 1 if (s[-1]=="A") else 0
    startb += 1 if (s[0]=="B") else 0

print(cnt + min(enda, startb))

Submission status: Wrong Answer

Solution code:
N = int(input())
ABc = 0
Ac = 0
Bc = 0
j = 0
for i in range(N):
	s = input()
	ABc += s.count("AB")
	if s[0] == "B":
		Bc += 1
	if s[-1] == "A":
		Ac += 1
	if s[0] == "B" and s[-1] == "A":
		j += 1

if Ac == Bc and Bc == j:
	print(ABc + Ac - 1)
else:
	print(ABc + min(Ac, Bc))

Submission status: Wrong Answer

Solution code:
n = int(input())
ab_num = 0
a_num = 0
b_num = 0
double = 0
for _ in range(n):
    s = input()
    ab_num += s.count("AB")
    if s[0] == "B":
        b_num += 1
    if s[-1] == "A":
        a_num += 1
    if (s[0] == "B") & (s[-1] == "A"):
        double += 1
ans = ab_num + min(a_num, b_num)
if (a_num == 1) & (b_num == 1) & (double == 1):
    ans = ab_num
print(ans)

Submission status: Wrong Answer

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

if N == 1:
    print(S[0].count("AB"))
    quit()

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())
cnt=0
a=0
b=0
c=0
for i in range(N):
    S=input()
    if S[-1]=="A" and S[0]=="B":
        c+=1
    elif S[-1]=="A":
        a+=1
    elif S[0]=="B":
        b+=1
    cnt+=S.count("AB")
if a==0 and b==0:
    cnt+=c-1
else:
    cnt+=min(a,b)+c
print(cnt)

Submission status: Wrong Answer

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

k = sum(t.count('AB') for t in s)
ab = sum(t[-1] == 'A' and t[0] == 'B' for t in s)
a = sum(t[-1] == 'A' and t[0] != 'B' for t in s)
b = sum(t[-1] != 'A' and t[0] == 'B' for t in s)
d = abs(a - b)
d2 = max(0, ab - d)
ans = k
f = a > 0 or ab > 0
if a > 0 and ab > 0:
    ans += 1
    ab -= 1
    a -= 1
ans += ab
if f and b > 0:
    ans += 1
    b -= 1
ans += min(a, b)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[input() for i in range(n)]
cnt1=0
cnt2=0
cnt3=0
cnt4=0
for si in s:
    if si[0]=='B' and si[-1]=='A':
        cnt1+=1
    elif si[0]=='B':
        cnt2+=1
    elif si[-1]=='A':
        cnt3+=1
    for i in range(len(si)-1):
        if si[i]=='A' and si[i+1]=='B':
            cnt4+=1
if cnt2>=1:
    if cnt3>=1:
        print(cnt4+cnt1+min(cnt2, cnt3))
    else:
        print(cnt4+cnt1)
else:
    if cnt3>=1:
        print(cnt4+cnt1)
    else:
        print(cnt4-1)

Submission status: Wrong Answer

Solution code:

N = int(input())
acount = 0
bcount = 0
ab = 0
ans = 0
for i in range(N):
    s = input()
    ans += s.count("AB")
    if s[-1] == "A" and s[0] == "B":
        ab += 1

    if s[-1] == "A":
        acount += 1
    if s[0] == "B":
        bcount += 1
add = min (acount,bcount)
if ab == acount and ab == bcount:
    add -= 1
print(ans + add)

Submission status: Wrong Answer

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

n = 0
n += sum(si.count("AB") for si in s)
fA = [si[-1] == 'A' for si in s]
sB = [si[0] == 'B' for si in s]
print(n + min(sum(fA), sum(sB)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
count = 0
sta_a, sta_b, en_a, en_b = 0, 0, 0, 0
n = int(input())
for _ in range(n):
  l = list(input())
  for i in range(len(l)-1):
    if l[i] == 'A':
      if l[i+1] == 'B':
        count += 1
  if l[0] == 'A':
    sta_a += 1
  elif l[0] == 'B':
    sta_b += 1
  if l[-1] == 'A':
    en_a += 1
  elif l[-1] == 'B':
    en_b += 1

c1 = min(sta_a, en_b)
c2 = min(sta_b, en_a)
li = [sta_a, sta_b, en_a, en_b]
count += c1 + c2
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())

ans = 0
b_ = 0
_a = 0
b_a = 0

for _ in range(N):
    s = input()
    ans += s.count('AB')
    if s[0]=='B' and s[-1]=='A':
        b_a += 1
    elif s[0]=='B':
        b_ += 1
    elif s[-1]=='A':
        _a += 1

if b_a > 0:
    ans += b_a - 1
    if 0 < _a >= b_:
        ans += 1
        _a -= 1
    elif 0 < b_ >= _a:
        ans += 1
        b_ -= 1
ans += min(_a, b_)
    


print(ans)

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


count+=min(start_with_B,end_with_A,N-1)

print(count)

Submission status: Wrong Answer

Solution code:
words = lambda t : list(map(t, input().split()))
n = int(input())

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

ab += ba-1
ba = 1
if a > 0:
    ab += 1
    ba = 0
elif b > 0:
    ab += 1
    ba = 0
print(ab + min(a,b))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# /usr/bin/python
# -*- coding: utf-8 -*-
import sys

N = int(input())
ret = 0
pat = [0,0,0,0]
## pat:=[--, B-, -A, BA]

for _ in range(N):
  s = str(input())
  ret += s.count("AB")
  p = 0
  if s[0]=='B':
    p += 1
  if s[-1]=='A':
    p += 2
  pat[p] += 1

if pat[3]>0: ret += pat[3]-1
ret += min(pat[1],pat[2])
if abs(pat[1]-pat[2])>0 and pat[3]>0:
  ret += 1
print(ret)

Submission status: Wrong Answer

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

a_list = []
both = []
b_list = []
remain = []

for i, s in enumerate(s_list):
    if s[0] == "B" and s[-1] == "A":
        both.append(s)

    elif s[-1] == "A":
        a_list.append(s)

    elif s[0] == "B":
        b_list.append(s)

    else:
        remain.append(s)
ans = 0
while True:
    if len(a_list) != 0:
        a = a_list.pop()
        if len(b_list) != 0:
            b = b_list.pop()
        elif len(both) != 0:
            b = both.pop()
        else:
            break
    elif len(both) != 0:
        a = both.pop()
        if len(b_list) != 0:
            b = b_list.pop()
        elif len(both) != 0:
            b = both.pop()
        else:
            break
    else:
        break
    ans += 1
    ans += a.count("AB")
    ans += b.count("AB")

while len(a_list) != 0:
    a = a_list.pop()
    ans += a.count("AB")


while len(b_list) != 0:
    a = b_list.pop()
    ans += a.count("AB")

while len(both) != 0:
    a = both.pop()
    ans += a.count("AB")

while len(remain) != 0:
    a = remain.pop()
    ans += a.count("AB")

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 20 20:47:47 2019

@author: Owner
"""

# -*- coding: utf-8 -*-
"""
Created on Sat Apr 13 09:53:12 2019

@author: Owner
"""
import numpy as np
import sys
import collections
import scipy.misc
import math
from operator import itemgetter
import itertools
import copy
import bisect

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default


"""
N, X = map(int, input().split())

x = list(map(int, input().split()))

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())

# 多次元配列の宣言（あとでintにすること。）（タプルにすること。）
dp = np.zeros((N+1, 4,4,4))
    
all(nstr.count(c) for c in '753')

# 複数配列を並び替え
ABT = zip(A, B, totAB)
result = 0
# itemgetterには何番目の配列をキーにしたいか渡します
sorted(ABT,key=itemgetter(2))
A, B, totAB = zip(*ABT)
A.sort(reverse=True)

# 2進数のbit判定
(x >> i) & 1

# dp最小化問題
dp = [np.inf]*N
for n in range(N):
    if n == 0:
        dp[n] = 0
    else:
        for k in range(1,K+1):
            if n-k >= 0:
                dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k]))
            else:
                break
# 累積和
add = 1 # 問題によって決まる
res = 0
sums = [0]*(len(nums)+1)
for i in range(len(nums)):
    sums[i+1] = sums[i] + nums[i]
for i in range(0, len(nums), 2):
    left = i
    right = min(i+add, len(nums))
    tmp = sums[right] - sums[left]
    res = max(tmp, res)

#２分探索
li, ri = bisect.bisect_left(p_ac, l[i]-1), bisect.bisect_right(p_ac, r[i]-1)    

#ソート関数
org_list = [3, 1, 4, 5, 2]
new_list = sorted(org_list)
print(org_list)
print(new_list)
# [3, 1, 4, 5, 2]
# [1, 2, 3, 4, 5]

"""
N = int(input())

s = [""]*N

cnt  = 0
cnt_A= 0
cnt_B= 0
cnt_BA=0
for n in range(N):
    s[n] = input()
    cnt += s[n].count("AB")
    if s[n][0] == "B" and s[n][-1] == "A":
        cnt_BA+=1
    elif s[n][0]=="B":
        cnt_B +=1
    elif s[n][-1]=="A":
        cnt_A +=1
    else:
        pass
    
            

def main(): 
    #print(s, cnt)
    print(cnt + min(cnt_BA+cnt_B, cnt_BA+cnt_A))
                
                
                
            
            
                

    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x = int(input())
a = 0
b = 0
c = 0
f = 1
bun = []
for p in range(x):
    bun = input()
    if bun[0] == "B":
        b +=1
    if bun[len(bun)-1] == "A":
        a += 1
        if bun[0] == "A":
            f = 0
    for k in range(len(bun)-1):
        if bun[k] == "A":
            if bun[k+1] == "B":
                c += 1
    
if a == x:
    a = a-1
if a < b:
    print(a+c-f)
else:
    print(b+c-f)

Submission status: Wrong Answer

Solution code:
N = int(input())
counter = 0
end_a = 0
start_b = 0
for n in range(N):
    s = input()
    for i in range(len(s) - 1):
        if s[i:i + 2] == 'AB':
            counter += 1
    if s[-1] == 'A':
        end_a += 1
    if s[0] == 'B':
        start_b += 1

print(counter + min([end_a, start_b]))

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    S = [input() for _ in range(N)]
    ans = 0
    bxa = 0
    bx = 0
    xa = 0
    for s in S:
        ans += s.count("AB")
        if s.startswith("B") and s.endswith("A"):
            bxa += 1
        elif s.startswith("B"):
            bx += 1
        elif s.endswith("A"):
            xa += 1

    ans += min(bx, xa)
    ans += bxa - (1 if max(bx, xa) == 0 else 0)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

count = 0
Acount = 0
Bcount = 0

for i in range(N):
    s = input()
    if 'AB' in s:
        count += 1
    if s[0] == 'B':
        Bcount += 1
    if s[-1] == 'A':
        Acount += 1

count += min(Bcount, Acount)
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = [input() for i in range(N)]
abc = sum([e.count("AB") for e in s]) # 各文字列のAB出現回数
bstart, aend = 0, 0
AllB_A = True
for e in s:
    if e[0] == "B":
        bstart += 1
    if e[-1] == "A":
        aend += 1
    if (e[0] == "B" or e[-1] == "A") and (e[0] != "B" or e[-1] != "A"):
        AllB_A = False

ans = 0
if bstart == aend:
    if AllB_A:
        ans = abc + bstart - 1
    else:
        ans = abc + bstart
else:
    ans = abc + min(bstart, aend)

print(ans)

Submission status: Wrong Answer

Solution code:
N =int(input())
co = {0:0,1:0,2:0,3:0}
ans = 0
foo =0
for _ in range(N):
    i = input()
    foo = 0
    if i[0] =="B":
        foo += 1
    if i[-1] =="A":
        foo += 2
    co[foo] += 1
    ans += i.count("AB")
ans += co[3] + min(co[1],co[2])
print(ans)

Submission status: Wrong Answer

Solution code:


def read_input():
    n = int(input())
    slist = []
    for i in range(n):
        slist.append(input())

    return n, slist

def submit():
    n, slist = read_input()

    ab_in = 0
    x_a = 0
    b_x = 0
    b_a = 0

    for s in slist:
        if 'AB' in s:
            ab_in += 1

        if s[0] == 'B' and s[-1] == 'A':
            b_a += 1
        elif s[0] == 'B':
            b_x += 1
        elif s[-1] == 'A':
            x_a += 1

    pure_match = min(b_x, x_a)
    rest = max(b_x, x_a) - pure_match

    if b_a <= rest:
        print(ab_in + pure_match + b_a)
    else:
        print(ab_in + pure_match + rest + (b_a - rest)//2 + 1)


if __name__ == '__main__':
    submit()

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

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for i in range(N)]
#print(S)
a_cnt = 0
b_cnt = 0
ab_cnt = 0
b_a_cnt = 0
for i,s in enumerate(S):
    ab_cnt += s.count("AB")
    if s[-1] == "A":
        a_cnt += 1
    if s[0] == "B":
        b_cnt += 1
    if s[0] == "B" and s[-1] == "A":
        b_a_cnt += 1
if a_cnt == b_cnt == b_a_cnt:
    ab_cnt += min(a_cnt, b_cnt) - 1
else:
    ab_cnt += min(a_cnt, b_cnt)

print(ab_cnt)