Problem:


Score : 400 points

Problem Statement

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

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

Constraints

1 \leq N \leq 10^{4}

2 \leq |s_i| \leq 10

s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

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

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4



Sample input:
3
ABCA
XBAZ
BAD


Sample output:
2

Submission status: Wrong Answer

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

import numpy as np

sys.setrecursionlimit(200000)


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


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


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


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


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


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


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

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

if num_end_with_A == num_start_with_B:
    print(num_inner_AB + num_end_with_A + num_both - 1)
else:
    print(num_inner_AB + min(num_end_with_A, num_start_with_B) + num_both)

Submission status: Wrong Answer

Solution code:
n = int(input())

ans_a, ans_b = 0, 0
for _ in range(n):
    s = input()
    ans_a += s.count('A')
    ans_b += s.count('B')
    
ans = min(ans_a, ans_b)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for i in range(n)]
ans = 0
l_b, r_a = 0, 0
flag = False
for i in range(n):
    if "AB" in s[i]:
        ans += 1
    if s[i][0] == "B":
        l_b += 1
    if s[i][-1] == "A":
        r_a += 1
print(min(l_b, r_a) + ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[]
for i in range(n):
  s.append(input())
cnt_a=0
cnt_b=0
for j in range(n):
  if s[j][0]=='B' and s[j][len(s[j])-1]=='A':
    cnt_a+=1
    cnt_b+=1
  elif s[j][0]=='B':
    cnt_b+=1
  elif s[j][len(s[j])-1]=='A':
    cnt_a+=1
cnt_2=0
for l in range(n):
  cnt_2+=s[l].count('AB')
if cnt_a==cnt_b:
  print((cnt_a+cnt_2)-1)
else:
  print(min(cnt_a,cnt_b)+cnt_2)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = 0
e = 0
se = 0
ans = 0
for i in range(n):
    stri = input()
    ans += stri.count('AB')
    if stri[0] == 'B' and stri[len(stri) - 1] == 'A':
        se += 1
    else:
        if stri[0] == 'B':
            s += 1
        if stri[len(stri)-1] == 'A':
            e += 1
while se < 0:
    if s < e:
        s += 1
    else:
        e += 1
    se -= 1
ans += min(s,e)
print(ans)

Submission status: Wrong Answer

Solution code:
N= int(input())

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for i in range(n)]
count = 0
count_a = 0
count_b = 0
count_ab = 0
for i in range(n):
    count += s[i].count("AB")
    if s[i][-1] == "A":
        count_a += 1
    if s[i][0] == "B":
        count_b += 1
    if s[i][0] == "B" and s[i][-1] == "A":
        count_ab += 1
if count_ab%2 == 0:
    print(count+min([count_a,count_b]))
else:
    print(count+min([count_a,count_b-1]))

Submission status: Wrong Answer

Solution code:
x = int(input())
a = 0
b = 0
c = 0
f = 0
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] == "B":
            f += 1
    for k in range(len(bun)-1):
        if bun[k] == "A":
            if bun[k+1] == "B":
                c += 1
print(a,b,c,f)



if f == a:
    if a > 0:
        a -= 1
if a < b:
    print(a+c)
else:
    print(b+c)

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
BA_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":
            BA_cnt += 1
    elif s[i][-1] == "A":
        A_cnt +=1

if A_cnt > 0:
    A_cnt += BA_cnt
    plus = min(min(A_cnt,B_cnt),N-1)
else:
    plus = BA_cnt - 1
cnt += plus

print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
b_start = 0
a_end = 0
both = 0
ok = 0

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

print(ok + min(b_start, a_end, max(b_start, a_end) - both))

Submission status: Wrong Answer

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

n = 0
bss = []
ass = []
css = []
for s in ss:
    p = 0
    while p != -1:
        p = s.find('AB', p)
        if p == -1:
            break
        n += 1
        p += 1

    if s[0] == 'B':
        bss.append(s)
    if s[-1] == 'A':
        ass.append(s)
    if s[0] != 'B' and s[-1] != 'A':
        css.append(s)

n += min(len(ass), len(bss))
print(n)

Submission status: Wrong Answer

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

count = 0
Acount = 0
Bcount = 0
BAcount = 0

for i in range(N):
    s = input()
    if 1 < len(s):
        if 'AB' in s:
            count += s.count('AB')
        if s[0] == 'B' and s[-1] == 'A':
            BAcount += 1
        elif s[0] == 'B':
            Bcount += 1
        elif s[-1] == 'A':
            Acount += 1
    else:
        if s == 'B':
            Bcount += 1
        if s == 'A':
            Acount += 1

count += min(Bcount, Acount)
BorA = abs(Bcount - Acount)

tmp = BorA + BAcount - 1
if 0 < tmp:
    count += tmp

print(count)

Submission status: Wrong Answer

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

end_A = 0
start_B = 0
AB_count = 0
both = 0

for i in range(n):
    if 'AB' in s[i]:
        AB_count += 1
    if s[i][0] == 'B':
        start_B += 1
    if s[i][len(s[i])-1] == 'A':
        end_A += 1
    if s[i][0] == 'B' and s[i][len(s[i])-1] == 'A':
        both += 1
start_B -= both
end_A -= both
# print(end_A, start_B, both, AB_count)
if end_A == 0 and start_B == 0:
    print(AB_count + both - 1)
elif end_A == 0:
    print(AB_count + both)
elif start_B == 0:
    print(AB_count + both)
else:
    print(AB_count + both + 1 + min(end_A-1, start_B-1))

Submission status: Wrong Answer

Solution code:
n = int(input())
list = []
cnt = 0
cnta = 0
cntb = 0
for i in range(n):
  list.append(input())
  if(list[i].find('AB') >= 0):
    cnt += 1
  elif(list[i][-1:] == "A"):
    cnta += 1
  elif(list[i][0:1] == "B"):
    cntb += 1
cntab = min(cnta, cntb)
ans = cnt + cntab
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()

ba, ab, a, b = 0, 0, 0, 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
    ab += s.count("AB")

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

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
ans = 0
count = [0, 0, 0]
for i in range(N):
    ans += S[i].count('AB')
    s = S[i][0] + S[i][-1]
    if s == 'BA':
        count[2] += 1
    elif s[0] == 'B':
        count[1] += 1
    elif s[1] == 'A':
        count[0] += 1
count = sorted(count[:2]) + [count[2]]
m = min(count)
count = [count[0] - m, count[1] - m, count[2] - m]
ans += m * 2
if count[0]:
    ans += count[0]
else:
    if count[1]:
        ans += count[2]
    else:
        ans += count[2] - 1
print(ans)

Submission status: Wrong Answer

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

    AB = 0
    B_ = 0
    _A = 0
    B_A = 0
    for ss in s:
        if 'AB' in ss:
            AB += 1
        if ss[0] == 'B':
            if ss[-1] == 'A':
                B_A += 1
            else:
                B_ += 1
        elif ss[-1] == 'A':
            _A += 1
    print(AB + max((B_A - 1), 0) + max(min(B_ + B_A, _A), min(B_, _A + B_A)))
    # print(AB, B_, _A, B_A)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

N = int(input())
S = [input() for _ in range(N)]
l, m, n = 0, 0, 0
ans = 0
for s in S:
    if s[0] == 'B' and s[-2] == 'A':
        n += 1
    elif s[0] == 'B':
        m += 1
    elif s[-2] == 'A':
        l += 1
    for i in range(len(s)-1):
        if s[i:i+2] == 'AB':
            ans += 1
ans += min(l, m)
if n > 0:
    ans += n - 1
if n > 0 and l != m:
    ans += 1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
cnt_AB = 0
end_A = 0
start_B = 0
for i in range(N):
    s = input()
    cnt_AB += s.count('AB')
    if s[-1] == 'A':
        end_A += 1
    if s[0] == 'B':
        start_B += 1
if end_A == start_B:
    print(cnt_AB + end_A - 1)
else:
    print(cnt_AB + min(end_A, start_B))

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
if len(a)==1:ans=a[0].count('AB')
ans=max(ans,0)
print(ans)

Submission status: Wrong Answer

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

base = sum(x.count('AB') for x in s)

same_pattern = a_n = b_n = 0
for x in s:
    b = x[0]
    a = x[len(x)-1]
    if b == 'B':
        b_n += 1
    if a == 'A':
        a_n += 1
    if a == 'A' and b == 'B':
        same_pattern += 1

if same_pattern == a_n:
    print(base + a_n - 1)
else:
    print(base + min(a_n, b_n))

Submission status: Wrong Answer

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

start = 0
end = 0
flg = False
c = 0
for x in s:
    c = c + len(x.split('AB')) - 1
    if x.startswith('B'):
        start += 1
        if x.endswith('A'):
            flg = True
    if x.endswith('A'):
        end += 1

if start == end:
    c += start
    if flg:
        c  -= 1
elif start > end:
    c += end
else:
    c += start
print(c)

Submission status: Wrong Answer

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

count = 0
aa = 0
bb = 0
f = False
flag=False
for a in A:
    if a[0]=='B':
        bb+=1
        f = True
    if a[-1]=='A':
        aa+=1
        if f:
            flag =True
            f=False
    count += a.count('AB')
if flag:
    aa -=1
count = count+min(aa,bb)
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
S = [input() for _ in range(n)]
cnt = 0
fb_cnt = 0
ba_cnt = 0
for s in S:
    idx = len(s) - 1
    for i in range(idx - 1):
        if s[i] == 'A' and s[i + 1] == 'B':
            cnt += 1
    if s[idx] == 'A':
        ba_cnt += 1
    if s[0] == 'B':
        fb_cnt += 1

min_cnt = min(fb_cnt, ba_cnt)
min_cnt = min_cnt if n > min_cnt else min_cnt - 1
print(cnt + min_cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for i in range(n)]
ans = 0
l_b, r_a = 0, 0
flag = False
for i in range(n):
    if "AB" in s[i]:
        ans += 1
    if s[i][0] == "B":
        l_b += 1
    if s[i][-1] == "A":
        r_a += 1
    if s[i][0] == "B" and s[i][-1] == "A":
        flag = True
if l_b == r_a and flag == True and l_b != 0:
    ans -= 1
print(min(l_b, r_a) + ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = ''
d= 0
e = {'A' : 0, 'B' : 0}
for i in range(n):
    S = input().strip()
    d += S.count('AB')
    if S[0] == 'A' or S[-1] =='A':
        e['A'] += 1
    if S[0] == 'B' or S[-1] == 'B':
        e['B']+=1

d+= min(e.values())
print(d)

Submission status: Wrong Answer

Solution code:
n=int(input())
ls=[0]*n
for i in range(n):
    ls[i]=input()
numab=0
sA=0
sB=0
sBA=0
for i in range(n):
    sb=""
    for j in ls[i]:
        if sb=="A":
            if j=="B":
                numab+=1
        sb=j
    if ls[i][0]=="B":
        if ls[i][-1]=="A":
            sBA+=1
        else:
            sB+=1
    if ls[i][-1]=="A" and ls[i][0]!="B":
        sA+=1
ans=0
if sA!=0 and sB!=0:
    ans=sBA+1
elif sA==0 and sB!=0:
    ans=sBA
elif sB==0 and sA!=0:
    ans=sBA
else:
    ans=sBA-1
ans+=numab
#print(numab,sB,sA,sBA)
print(ans if ans>0 else 0)

Submission status: Wrong Answer

Solution code:
n=int(input())

A_count=0
B_count=0
include_count=0

for i in range(n):
    moji=input()
    if 'AB' in moji:
        include_count+=1
    moji_line=list(moji)
    if moji_line[0]=='B':
        B_count+=1
    if moji_line[-1]=='A':
        A_count+=1
    # pair=[moji,moji_line[0],moji_line[-1]]
    # print(pair)

print(include_count+min(A_count,B_count))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []

counter = 0
alist = 0
blist = 0

for i in range(n):
    a.append(str(input()))

for i in a:
    counter += i.count('AB')
    if i[-1] == 'A':
        alist += 1
    if i[0] == 'B':
        blist += 1

if alist < blist:
    counter += alist
else:
    counter += blist

print(counter)

Submission status: Wrong Answer

Solution code:
# https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = s_inpl()   |	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = inpl()	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]

import math
import copy
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
from bisect import bisect_left, bisect_right
# import numpy as np

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

# 四方向: 右, 下, 左, 上
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]

def i_inpl(): return int(input())
def s_inpl(): return map(int,input().split())
def l_inpl(): return list(map(int, input().split()))
INF = float("inf")

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

ab_cnt = 0
f_a_cnt = 0
f_b_cnt = 0
l_a_cnt = 0
l_b_cnt = 0
for s_i in s:
    ab_cnt += s_i.count("AB")
    if s_i[0] == "A":
        f_a_cnt += 1
    elif s_i[0] == "B":
        f_b_cnt += 1
    if s_i[-1] == "A":
        l_a_cnt += 1
    elif s_i[-1] == "B":
        l_b_cnt += 1

# print(ab_cnt, f_a_cnt, f_b_cnt, l_a_cnt, l_b_cnt)
print(ab_cnt + min(f_b_cnt, l_a_cnt))

Submission status: Wrong Answer

Solution code:
N = int(raw_input())

inner_ab_count = 0
f_b_count = 0
l_a_count = 0
b_a_flag = False

for n in xrange(N):
    s = raw_input()
    inner_ab_count += s.count('AB')
    if s[0] == 'B':
        f_b_count += 1
        if s[-1] == 'A':
            b_a_flag = True
            l_a_count += 1
    elif s[-1] == 'A': l_a_count += 1

result = inner_ab_count + min([l_a_count, f_b_count])
if b_a_flag and min([l_a_count, f_b_count]) > 0 and l_a_count == f_b_count: result -= 1
print result

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

if num_end_with_A == num_start_with_B:
    print(num_inner_AB + num_both - 1)
else:
    print(num_inner_AB + min(num_end_with_A, num_start_with_B) + num_both)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
count_A_right = 0
count_B_left  = 0
AB_count = 0
same_count = 0

for _ in range (N):
    S = input()
    temp = 0
    if S[0] == 'B':
        count_B_left +=1
        temp +=1
    if S[-1] == 'A':
        count_A_right +=1
        temp +=1
    if temp ==2:
        same_count+=1
        count_A_right -=1
        count_B_left  -=1
    for i in range (len(S)-1):
        if S[i:i+2] == 'AB':
            AB_count +=1

AB_count += same_count-1#一つのBAに変更して加える
count_A_right +=1
count_B_left  +=1
if count_A_right==1 and count_B_left ==1:
    print(AB_count)
else:
    print(AB_count+min(count_A_right,count_B_left))

Submission status: Wrong Answer

Solution code:
#python 3.5.2

N = int(input())
s_list = [input() for i in range(N)]
cnt = sum([1 for s in s_list if "AB" in s])
a_end, b_start, a_b = 0, 0, 0
for s in s_list:
    if s[0] == "B" and s[-1] == "A":
        a_b += 1
    elif s[0] == "B":
        b_start += 1
    elif s[-1] == "A":
        a_end += 1
print(min(a_end, b_start)+a_b+cnt)

Submission status: Wrong Answer

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()

n = ni()
_anum = 0
b_num = 0
banum = 0
ct = 0
for i in range(n):
    st = input()
    ct += st.count('AB')
    if st.startswith('B'):
        if st.endswith('A'):
            banum += 1
        else:
            b_num += 1
    else:
        if st.endswith("A"):
            _anum += 1
if banum > 0:
    print(ct + min(_anum, b_num) + (banum - 1) + 1)
else:
    print(ct + min(_anum, b_num))

Submission status: Wrong Answer

Solution code:
N = int(input())

b = 0
a = 0
count = 0
check = -1
for i in range(N):
    s = input()
    count += len([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 check == -1:
        if s[0] != "B" and s[-1] !="A":
            check = 0

print(count + min([a, b, N - 1])+check)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from string import ascii_uppercase
import sys, bisect, math
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N = read_int()[0]
S = [read_str() for _ in range(N)]

def solve():
    _a = 0
    b_ = 0
    ba = 0
    in_ab = 0

    # search
    for i in range(N):
        if S[i][0] == 'B' and S[i][-1:] == 'A':
            ba += 1
        elif S[i][0] == 'B':
            b_ += 1
        elif S[i][-1:] == 'A':
            _a += 1
        for j in range(len(S[i]) - 1):
            if S[i][j: j + 2] == 'AB':
                in_ab += 1

    ans = 0
    ans += in_ab + ba - 1 + max(0, min(_a - 1, b_ - 1))

    if _a > 0: ans += 1
    if b_ > 0: ans += 1
    return ans

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

Submission status: Wrong Answer

Solution code:
#python 3.5.2

N = int(input())
s_list = [input() for i in range(N)]
cnt = sum([1 for s in s_list if "AB" in s])
a_end, b_start, a_b = 0, 0, 0
for s in s_list:
    if s[0] == "B" and s[-1] == "A":
        a_b += 1
    elif s[0] == "B":
        b_start += 1
    elif s[-1] == "A":
        a_end += 1
print(a_end, b_start, a_b)
print(min(a_end, b_start)+a_b+cnt)

Submission status: Wrong Answer

Solution code:
import numpy as np
import re

n=int(input())
string_list=[input() for i in range(n)]
#print(string_list)

check_sheet = np.zeros((len(string_list),4))
for u,string in enumerate(string_list):
  # "AB"が含まれる数のcount
  check_sheet[u][0] = len(re.findall("AB",string))
  # 頭に"A"がいる and 頭に"B"がいる
  if string[-1] == "A" and string[0] == "B":
    check_sheet[u][3] = 1
  elif string[-1] == "A":
    check_sheet[u][1] = 1
  elif string[0] == "B":
    check_sheet[u][2] = 1

# AB,-A,B-,B-A
sums=np.sum(check_sheet,axis=0)
#print(sums)
count = sums[0] + min(sums[1],sums[2])
if sums[1] != sums[2]:
  count += 1
if sums[3] != 0:
  count += (sums[3]-1)
print(int(count))

Submission status: Wrong Answer

Solution code:
N = int(input())

BA = 0
start_B = 0
end_A = 0

ans = 0
for _ in range(N):
    s = list(input())
    if s[0] == 'B' and s[-1] == 'A':
        BA += 1
    elif s[0] == 'B':
        start_B += 1
    elif s[-1] == 'A':
        end_A += 1
    for i in range(len(s)-1):
        if s[i] == 'A' and s[i+1] == 'B':
            ans += 1
ans += min(start_B, end_A)
if BA > 0:
    ans += BA - 1
if (start_B != end_A) and BA > 0:
    ans += 1
if ans == N:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
listA = list(input() for i in range(n))
a = 0
b = 0
c = 0
for j in range(n):
    a = a + listA[j].count("AB")
for k in range(n):
    if listA[k][0]=="B":
        b = b+1
    if listA[k][-1]=="A":
        c = c+1
print(a+min(b,c))

Submission status: Wrong Answer

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

ans = 0
A = 0
B = 0
BA = 0
for i in s:
    ans += i.count("AB")
    if i[0:1] != "B" and i[-1:] == "A":
        A += 1
    elif i[0:1] == "B" and i[-1:] != "A":
        B += 1
    elif i[0:1] == "B" and i[-1:] == "A":
        BA += 1

#print(ans)
#print(A,B,BA)

while 1:
    if BA == 0 or A == B:
        break
    if A >= B:
        B += 1
    else:
        A += 1
    BA -= 1
#print(A,B,BA)
print(ans + min(A,B) + BA)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[]
for i in range(n):
  s.append(input())
cnt_a=0
cnt_b=0
for j in range(n):
  if s[j][0]=='B' and s[j][len(s[j])-1]=='A':
    cnt_a+=1
    cnt_b+=1
  elif s[j][0]=='B':
    cnt_b+=1
  elif s[j][len(s[j])-1]=='A':
    cnt_a+=1
cnt_2=0
for l in range(n):
  cnt_2+=s[l].count('AB')
print(min(cnt_a,cnt_b)+cnt_2)

Submission status: Wrong Answer

Solution code:
n = int(input())
fully_ab = 0
last_a = 0
head_b = 0
head_last_both = 0

for i in range(n):
    s = input().rstrip()
    fully_ab +=s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        head_last_both += 1
    elif s[0] == "B":
        head_b += 1
    elif s[-1] == "A":
        last_a += 1
    
assert (last_a+head_b+head_last_both <= n)
bigger = max(last_a,head_b)
lower  = min(last_a,head_b)
amari = bigger - lower

connected = max(0,head_last_both - 1)

ans = fully_ab + lower + connected

if lower > 0 and head_last_both > 0:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

a = 0
b = 0
c = 0
co = 0
for i in range(n):
	l = input()
	co += l.count("AB")
	if l[0] == "B":
		b += 1
	if l[-1] == "A":
		a += 1
	if l[-1] == "A" and l[0] == "B":
		c += 1
if c != 0:
	if a != 0 and b != 0:
		print(co + c + 1 + min(a-1-c,b-1-c))
	elif a != 0:
		print(co + c)
	elif b != 0:
		print(co + c)
	else:
		print(co + c - 1)

else:
	print(co + min(a,b))

Submission status: Wrong Answer

Solution code:
N = int(input())
AB_number = 0
a_cnt = 0
b_cnt = 0
for _ in range(N):
    s_i = input()
    AB_number += s_i.count("AB")
    if s_i[0] == "B":
        b_cnt += 1
    if s_i[-1] == "A":
        a_cnt += 1

if a_cnt==b_cnt:
    print(AB_number+min(a_cnt,b_cnt)-1)
else:
    print(AB_number+min(a_cnt,b_cnt))

Submission status: Wrong Answer

Solution code:
N = int(input())

S = list()
count = 0
acount = 0
bcount = 0
for _ in range(N):
    st = input()
    count += st.count('AB')
    S.append(st)
    if st.startswith('B'):
        bcount += 1
    if st.endswith('A'):
        acount += 1

c = min(acount, bcount, N - 1)
print(count + c)

Submission status: Wrong Answer

Solution code:
def count(list):
    a,b,z=0,0,0
    for i in range(len(list)):
        if list[i][0]=='B':
            b+=1
        if list[i][-1]=='A':
            a+=1
        
    if a==b==len(list):
        return a-1
    else:
        
        return min(a,b)
def check(string):
    c=0
    for i in range(len(string)-1):
        if string[i]=='A' and string[i+1]=='B':
            c+=1
    return c
n=int(input())
m,l=0,[]
for i in range(n):
    s=input()
    m+=check(s)
    
    l.append(s)
m+=count(l)
 
print(m)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
ab = 0
a = 0
b = 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
b,a = sorted([a,b])

sa = (a-b)//2
if sa > ab:
    a -= ab
    b += ab
    ans += ab
    ans += min(a,b)
else:
    a -= sa
    b += sa
    ab -= sa
    ans += sa
    ans += min(a,b)
    if ab != 0:
        ans += ab-1
        ans += 1 if abs(a-b) else 0

print(ans)