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:
N = int(input())
s_ = [input() for _ in range(N)]

center = 0
s_aend = []
s_bstart = []
s_bsae = []
s_else = []
center = 0

for s in s_:
  if s.startswith("B") and s.endswith("A"):
    s_bsae.append(s)
  elif s.startswith("B"):
    s_bstart.append(s)
  elif s.endswith("A"):
    s_aend.append(s)
  else:
    center += s.count("AB")

s_max = ""
if len(s_bsae) > 0:
  s_max = ''.join(s_bsae)

if len(s_bstart) > 0:
  center += 1
  b_len = len(s_bstart)-1
else:
  b_len = len(s_bstart)
  
if len(s_aend) > 0:
  center += 1
  a_len = len(s_aend)-1
else:
  a_len = len(s_aend)
  
print(s_max.count("AB") + center + min(a_len,b_len))

Submission status: Wrong Answer

Solution code:
N = int(input())
s = [input() for i in range(N)]
count = 0
l = []
a = 0
b = 0
ab = 0
for i in s:
  count+=i.count("AB")
for i in s:
  if list(i)[0] == "B" and list(i)[-1] != "A":
    l.append(i)
    b += 1
  elif list(i)[-1] == "A" and list(i)[0] != "B":
    l.append(i)
    a += 1
  elif list(i)[-1] == "A" and list(i)[0] == "B":
    l.append(i)
    ab += 1
if a > b + ab:
  count += b + ab
elif b > a + ab:
  count += a + ab
else:
  count += min(a, b) + ab
print(count)

Submission status: Wrong Answer

Solution code:
import re

n = int(input())

strs = []

patterna = re.compile(r"A$")
patternb = re.compile(r"B")
patternab = re.compile(r"AB")

a = 0
b = 0
a_b = 0
ab = 0

for i in range(n):
    str = input()
    if 'AB' in str:
        ab += 1
    if str.startswith('B') and str.endswith('A'):
        a_b += 1
        continue
    if str.endswith('A'):
        a += 1
    if str.startswith('B'):
        b += 1

for i in range(a_b):
    if a <= b:
        a += 1
    else:
        b += 1

ans = ab + min(a, b)
if (a_b > 1):
    ans += 1

print(ans)

Submission status: Wrong Answer

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

default = sum(['AB' in i for i in l])

print(l)
#only末尾Aの数
a = sum([i[-1]=='A' and i[0]!='B' for i in l])
#only先頭Bの数
b = sum([i[0]=='B' and i[-1]!='A' for i in l])
#先頭Bかつ末尾Aの数
ab = sum([i[0]=='B' and i[-1]=='A' for i in l])

# print("a = ", str(a))
# print("b = ", str(b))
# print("ab = ", str(ab))

if a == b:
    num = a + math.floor(ab/2)
else:
    min = min(a, b)
    max = max(a, b)
    gap = abs(a-b)
    if ab <= gap:
        num = min + ab
    elif ab > gap:
        num = max + math.floor((ab - gap)/2)

print(num + default)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [input() for i in range(N)]
b = 0
c = 0
d = 0
e = 0
for j in range(N):
    for k in range(len(a[j]) - 1):
        if a[j][k] == "A" and a[j][k + 1] == "B":
            b += 1
    if a[j][len(a[j])- 1] == "A":
        c += 1
    if a[j][0] == "B":
        d += 1
    if a[j][len(a[j])-1] == "A" and a[j][0] == "B":
      e = 1
if c == 1 and d == 1 and e == 1:
  print(b)
elif c == d:
  f = min(c, d) - 1
  print(b + f)
else:
  f = min(c, d)
  print(b + f)

Submission status: Wrong Answer

Solution code:
N=int(input())
A_list=0
B_list=0
both=0
count=0
for i in range(0,N):
    txt=input()
    if "AB" in txt:
        count+=1
    
    if(txt[0]=="B" and txt[-1]=="A"):
        both+=1
    elif (txt[0]=="B"):
        B_list+=1
    elif(txt[-1]=="A"):
        A_list+=1

index=min(A_list,B_list)
count+=index
A_list-=index
B_list-=index
if(A_list==0):
    index=min(B_list,both)
    count+=index
    if (both>0):
        count+=(both-1)
elif(B_list==0):
    index=min(A_list,both)
    count+=index
    if (both>0):
        count+=(both-1)
print(count)

Submission status: Wrong Answer

Solution code:
import re
#N=int(input())
#listA=[input() for i in range(N)]
listA=["BABABA","ABAB"]
firstB=0
endA=0
feAB=0
AB=0
for i in listA:
    AB+=len(re.findall("AB",i))
    
    if i[0]=="B":
        firstB+=1
    if i[-1]=="A":
        endA+=1
        if i[0]=="B":
            feAB+=1

AB+=min([endA,firstB])
if feAB>0:
    AB-=1
print(AB)

Submission status: Wrong Answer

Solution code:
o=int(input())
a=[]
for i in range(o):
    a+=input().split()
s="A".join(a)
print(s.count("AB"))

Submission status: Wrong Answer

Solution code:
N = int(input())
s_list = [input() for _ in range(N)]
end_a_num = 0
start_b_num = 0
same_num = 0
ans = 0
for s in s_list:
    ab_num = s.count('AB')
    ans += ab_num
    if s[-1] == 'A':
        end_a_num+=1
    if s[0]=='B':
        start_b_num+=1
    if s[-1]=='A' and s[0]=='B':
        same_num+=1


ans+= min(end_a_num,start_b_num)
if end_a_num==start_b_num:
    ans-=1    

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
cnta = 0
cntb = 0
cntab = 0
for i in range(n):
    a = input()
    l = len(a)
    if a[0] == 'B':
        cntb += 1
    if a[l-1] == 'A':
        cnta += 1
    if 'AB' in a:
        cntab += 1
print(cntab + min(cnta,cntb))

Submission status: Wrong Answer

Solution code:
n=int(input())
numA=0
numB=0

def check(s):
    if not "AB" in s:
        return 0
    cnt=0
    for a,b in zip(s[:-1],s[1:]):
        if a=="A" and b=="B":
            cnt+=1
    return cnt

ans=0
for i in range(n):
    s=input()
    ans+=check(s)
    if s[0]=="B":
        numB+=1
    if s[-1]=="A":
        numA+=1
    
print(ans+min(min(numA,numB),n-1))

Submission status: Wrong Answer

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

N = int(raw_input())
S = list()

_count = 0
_A = 0
_B = 0
_BA = 0
for i in range(N):
    S.append(raw_input())

for s in S:
    if s[-1] == "A":
        _A += 1
    if s[0] == "B":
        _B += 1
    if s[-1] == "A" and s[0] == "B":
        _BA += 1
    for i in range(len(s)-1):
        if s[i:i+2] == "AB":
            _count += 1
    
_count += min(_A,_B)
if _A == _B == _BA:
    _count -= 1

print str(_count)

Submission status: Wrong Answer

Solution code:
import sys

N = int(sys.stdin.readline())

s = []

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

count = 0

for i in range(N):
    count += s[i].count('AB')

pattern_1 = []
pattern_2 = []
pattern_3 = []
for i in range(N):
    if s[i][:1] == 'B' and s[i][-1:] == 'A':
        pattern_3.append(i)
    elif s[i][:1] == 'B':
        pattern_2.append(i)
    elif s[i][-1:] == 'A':
        pattern_1.append(i)

len_1 = len(pattern_1)
len_2 = len(pattern_2)
len_3 = len(pattern_3)

dif = len_1 - len_2
if dif > 0:
    hoge = len_2 + len_3
    hoge2 = hoge - len_1
    if hoge2 > 0:
        temp = min(len_1, hoge) + hoge2 - 1
    else:
        temp = min(len_1, hoge)
else:
    hoge = len_1 + len_3
    hoge2 = hoge - len_2
    if hoge2 > 0:
        temp = min(len_2, hoge) + hoge2 - 1
    else:
        temp = min(len_2, hoge)

ans = count + temp

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

ar = 0
bl = 0
ans = 0
for i in range(N):
    s = input()
    ans += s.count('AB')
    if bl == 0 and ar == 0:
        if s[0] == 'B':
            bl += 1
        if s[-1] == 'A':
            ar += 1
    elif bl and ar == 0:
        if s[-1] == 'A':
            ans += 1
            bl -= 1
            if s[0] == 'B':
                bl += 1
    elif bl == 0 and ar:
        if s[0] == 'B':
            ans += 1
            ar -= 1
            if s[-1] == 'A':
                ar += 1
    else:
        if s[-1] == 'A' and s[0] == 'B':
            ans += 1
        elif s[-1] == 'A':
            ans += 1
            bl -= 1
        elif s[0] == 'B':
            ans += 1
            ar -= 1

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n =int(input())
c=0
ica = [0]*n
icb = [0]*n

for i in range(n):
    a = input().split()
    c += a[0].count('AB')
    if a[0][-1:] == 'A':
        ica[i] = 1
    if a[0][:1] == 'B':
        icb[i] = 1


cc = [1]*n
for i in range(n):
    if ica[i] == 1 and icb[i] == 0:
        cc[i] = 0
        break

e=0
s=0
o=0
for i in range(n):
    s=0
    if o == 0:
        for j in range(n):
            if ica[j] == 1 and icb[j] == 1 and cc[j] == 1:
                cc[j] = 0
                c+=1
                s=1
                break
    if s==0 and e==0:
        o=1
        for j in range(n):
            if icb[j] == 1 and cc[j] == 1:
                s=1
                e=1
                c+=1
                cc[j]=0
                break
    if s==0 and e==1:
        o=1
        for j in range(n):
            if ica[j] == 1 and cc[j] == 1:
                e=0
                s=1
                cc[j]=0
                break
    if s==0:
        break

print(c)

Submission status: Wrong Answer

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

c = 0
a = 0
b = 0
ab = 0

for i in range(0, n):
    for j in range(1, len(s[i])):
        if s[i][j-1:j+1] == "AB":
            c += 1
    if s[i][-1] == "A" and s[i][0] == "B":
        ab += 1
    else: 
        if s[i][0] == "B":
            b += 1
        if s[i][-1] == "A":
            a += 1

s = c
m = min(a, b)
s += m
a -= m
b -= m

if(ab > 0):
    s += ab - 1
    if(a > 0):
        s += 1
    if(b > 0):
        s += 1

print(s)

Submission status: Wrong Answer

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

counter = 0
alist = 0
blist = 0
ablist = 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 i[-1] == 'A' and i[0] == 'B':
        ablist += 1

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

if alist == n and blist == n:
    print(counter - 1)
else:
    print(counter)

Submission status: Wrong Answer

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

base_ab=0
last_a=0
first_b=0
for s in string:
    base_ab+=s.count("AB")
    if s[-1]=="A":
        last_a+=1
    if s[0]=="B":
        first_b+=1
if n==1:
    last_a==0
print(base_ab+min(last_a,first_b))

Submission status: Wrong Answer

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

ab = 0
ab_ = 0
a = 0
b = 0
for i in s:
  ab += i.count("AB")
  if i[0] == "B" and i[-1] == "A":
    ab_ += 1
  if i[0] == "B":
    b += 1
  if i[-1] == "A":
    a += 1
if ab_ == a and ab_ == b:
  print(ab+a-1)
else:
  print(ab+min([a,b]))

Submission status: Wrong Answer

Solution code:
from sys import stdin
import math
import itertools


def makeIntMatrix(lines):
    intMatrix = []
    for a in makeStringMatrix(lines):
        intMatrix.append([int(b) for b in a])
    return intMatrix
 
 
def makeStringMatrix(lines):
    stringMatrix = [line.rstrip() for line in lines]
    return stringMatrix
 
 
def makeInt(line):
    return int(line.rstrip())
 
 
def makeMultiInteger(line):
    return [int(x) for x in line.rstrip().split()]


def solve(input_string):
    N = makeInt(input_string[0])
    S = makeStringMatrix(input_string[1:])
    #print(S)
    C_S_has_ab = []
    for s_index, s in enumerate(S):
        C_S_has_ab.append(0)
        for i in range(len(s)-1):
            if s[i] + s[i+1] == 'AB':
                C_S_has_ab[s_index] += 1
    #print(C_S_has_ab)
    _sum = 0
    _sum += sum(C_S_has_ab) 
    c_head_has_b = 0
    c_tail_has_a = 0
    c_head_tail_has_a_and_b = 0
    for s in S:
        if s[0] == 'B':
            c_head_has_b += 1
        if s[-1] == 'A':
            c_tail_has_a += 1
        if s[0] == 'B' and s[-1] == 'A':
            c_head_tail_has_a_and_b += 1
    #print(c_head_has_b, c_tail_has_a, c_head_tail_has_a_and_b)
    c_need_a_and_b = max(c_head_has_b, c_tail_has_a)- min(c_head_has_b, c_tail_has_a) - c_head_tail_has_a_and_b
    #print(c_need_a_and_b)
    if c_need_a_and_b < 0:
        c_need_a_and_b = -c_need_a_and_b
    else:
        c_need_a_and_b = 0
    #print(c_need_a_and_b)
    if c_need_a_and_b != 0:
        count_combi_a_b = min(c_tail_has_a, c_tail_has_a) - c_need_a_and_b + 1
    else:
        count_combi_a_b = min(c_tail_has_a, c_tail_has_a)
    _sum += count_combi_a_b - c_need_a_and_b
    #print(_sum)
    return _sum


def main():
    input_lines = stdin.readlines()
    answer = solve(input_lines)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
S = []
a_end = 0
b_start = 0
both = 0
count = 0
for i in range(N):
    s = input()
    S.append(s)
    if "AB" in s:
        count += 1
    if s[0] == "B":
        b_start += 1
        if s[-1] == "A":
            both += 1
    if s[-1] == "A":
        a_end += 1
# print("count:{}, a_end:{}, b_start:{}, both:{}".format(count,a_end, b_start, both))
count += min(a_end, b_start)

print(count)

Submission status: Wrong Answer

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

cnt = 0
cntA = 0
cntB = 0
cntAB = 0

for s in S :
    for i in range(1, len(s)) :
        if s[i - 1] =='A' and s[i] == 'B':
            cnt += 1
    if s[0] == 'B' and s[len(s) - 1] == 'A':
        cntAB += 1
    elif s[0] == 'B' :
        cntB += 1
    elif s[len(s) - 1] == 'A' :
        cntA += 1

ans = min(cntA, cntB)
if max(cntA, cntB) == 0:
    ans = cntAB - 1
else :
    ans += cntAB

if ans == len(S) :
    ans -= 1

print(cnt + ans)

Submission status: Wrong Answer

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

abf = 0
af = 0
bf = 0
ans = 0
for i in range(N):
  s_i = slist[i]
  ans += s_i.count("AB")
  if (s_i[0] == "B") and (s_i[-1] == "A"):
    abf+=1
  elif s_i[0] == "B":
    bf+=1
  elif s_i[-1] == "A":
    af+=1

t = abf
if (abf > af):
  ans += af
  t -= af
if (abf > bf):
  ans += bf
  t -= bf
ans += t // 2  
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for i in range(N)]
count = 0
con = []
Alis = []
Blis = []
for s in S:
    if 'AB' in s:
        count += 1
for i in range(N):
    if S[i][-1:] == 'A' and S[i][0] == 'B':
        con.append(S[i])
    elif S[i][-1:] == 'A':
        Alis.append(S[i])
    elif S[i][0] == 'B':
        Blis.append(S[i])
if (len(Alis)+len(Blis)+len(con))==len(con):
    count += len(con)-1
else:
    count += min(len(con)+len(Alis),len(con)+len(Blis))
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

for _ in range(N):
    s = input()
    flag = False
    for i in range(len(s)):
        if (flag == True) and (s[i] == "B"):
            ans += 1
            flag = False
        elif (flag == False) and (s[i] == "A"):
            flag = True
        elif (flag == True):
            flag = False
    if (s[0] == "B" and s[-1] == "A"):
        c += 1
    elif (s[0] == "B"):
        b += 1
    elif (s[-1] == "A"):
        a += 1
if c > 0:
    ans += c - 1
    c = 1
    if (a > 0 or b > 0):
        ans += 1
print(ans + min(a, b))

Submission status: Wrong Answer

Solution code:
o=int(input())
a=[]
for i in range(o):
    a+=input().split()
s="A".join(a)
print(s.count("AB"),s)

Submission status: Wrong Answer

Solution code:
N = int(input())
res = 0
both = 0
head = 0
last = 0
for i in range(N):
  inp = input()
  if 'AB' in inp:
    res += 1
  if 'B'==inp[0] and 'A'==inp[-1]:
    both+=1
  elif 'B'==inp[0]:
    head+=1
  elif 'A'==inp[-1]:
    last += 1

res += max(0,both -1)
if head > 0 and last >0:
  res += 2 + min(head-1,last-1)
elif head>0:
  res += 1 + min(head-1,last)
elif last>0:
  res += 1 + min(head,last-1)
print(res)

Submission status: Wrong Answer

Solution code:
n=int(input())
li=[]
t=0
for i in range (0,n):
  m=input()
  t=t+m.count("AB")
  li.append(list(m))
las=0
sta=0
q=0
for i in range (0,n):
  p=0
  le=len(li[i])-1
  if li[i][0]=="B":
    sta=sta+1
    p=p+1
  if li[i][le]=="A":
    las=las+1
    p=p+1
  if p==2:
    q=q+1
if sta==las==q:
  print(t+q-1)
elif sta<las:
  print(t+sta)
elif sta>las:
  print(t+las)
else:
  print(t+sta)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import stdin
input=[]
input1=stdin.readline().rstrip()
N=int(input1[0])
for n in range(N):
  input.append(stdin.readline().rstrip())
#print(input)

output=0
count_A=0
count_B=0
count_AB=0
for i in range(len(input)):
    if "AB" in input[i]:
        output+=input[i].count("AB")
    if "A" in input[i][len(input[i])-1]:
        count_A+=1
        #continue
    if "B" in input[i][0]:
        count_B+=1
    if "A" in input[i][len(input[i])-1] and "B" in input[i][0]:
        count_AB+=1

if count_AB==count_A and count_AB==count_B:
    print(output+count_AB-1)
else:
    if count_A<count_B:
        print(output+count_A)
    else:
        print(output+count_B)

Submission status: Wrong Answer

Solution code:
n = int(input())

s = []

ans = 0

both = 0
a_a = 0
b_a = 0

for i in range(n):
	ss = input()
	#print(ss)
	if(ss[0] == "B" and ss[len(ss)-1] == "A"):
		both += 1
	elif(ss[0] == "B"):
		b_a += 1
	elif(ss[len(ss)-1] == "A"):
		a_a += 1
	while('AB' in ss):
		ans += 1
		ss = ss.replace("AB", "", 1)

if(both > 1):
	ans += both -1
	both = 1

if(both >0 and a_a > 0 and b_a > 0):
	ans += 2
	both = 0
	a_a -= 1
	b_a -= 1
elif(both >0 and a_a > 0):
	both = 0
	a_a -= 1
	ans += 1
elif(both >0 and b_a > 0):
	both = 0
	b_a -= 1
	ans += 1

ans += min(a_a, b_a)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

S = []
for _ in range(N):
    s = input()
    S.append(s)
    
sub_strs = S[:]

count_ab = 0
for s in sub_strs:
    count_ab += s.count('AB')
    
count_ba = 0
count_xa = 0
count_bx = 0

for s in sub_strs:
    if s[0] == 'B' and s[-1] == 'A':
        count_ba += 1
    if s[0] != 'B' and s[-1] == 'A':
        count_xa += 1
    if s[0] == 'B' and s[-1] != 'A':
        count_bx += 1

ans = 0
ans += count_ab

ans += count_ba - 1

if count_xa >= 1:
    count_xa -= 1
    ans += 1
    
if count_bx >= 1:
    count_bx -= 1
    ans += 1
    
ans += min(count_xa, count_bx)

print(ans)

Submission status: Wrong Answer

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

res = 0
counta = 0
countb = 0
countab = 0
for s in S:
    res += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        countab += 1
    elif s[0] == "B":
        countb += 1
    elif s[-1] == "A":
        counta += 1

if min(counta, countab) + min(countb, countab) < countab:
    res += countab - 1
    if counta > 0:
        res += 1
        counta -= 1
    if countb > 0:
        res += 1
        countb -= 1
else:
    res += min(counta, countab)
    res += min(countb, countab)

    used = min(max(0, countab - counta), max(0, countab - countb))
    res += min(0, used)
    counta += max(0, countab - counta - used)
    countb += max(0, countab - countb - used)

    counta = max(0, counta - min(counta, countab))
    countb = max(0, countb - min(countb, countab))

res += min(counta, countb)
print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())

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

if N == 1:
    moji = input()
    print(moji.count('AB'))

else:
    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 AB_count == 0 and A_count == 0:
        print(0)

    elif A_count == B_count and A_count != 0:
        print(AB_count+B_count-1)

    elif A_count > B_count:
        print(AB_count+B_count)
    else:
        print(AB_count+A_count)

Submission status: Wrong Answer

Solution code:
N=int(input())

lst = []
for i in range(N):
    lst.append(input())
    
new_lst = [e for e in lst if 'A' in e or 'B' in e]

ABcount = [s for s in new_lst if 'AB' in s]

Bstart =[s for s in new_lst if s.startswith('B')]
Aend =[s for s in new_lst if s.endswith('A')]

BS = len(Bstart)
AE = len(Aend)

p = len(ABcount) + min(BS, AE)
print(p)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = 0
b = 0
c = 0
d = 0

for i in range(N):
    S = str(input())
    s = len(S)
    for i in range(s - 1):
        if S[i] == "A" and S[i + 1] == "B":
            d += 1
    if S[0] == "B" and S[s - 1] == "A":
        a += 1
    elif S[0] == "B":
        b += 1
    elif S[s - 1] == "A":
        c += 1

if b == 0 or c == 0:
    if a != 0:
        print(a + d)
    else:
        print(d)
elif b == c:
    print(a + b + d)
else:

    b = max(b, c)
    c = min(b, c)

    e = b - c

    if e >= a:
        print(a + c + d)
    else:
        if a!=0:
            print(a - 1 + c + d)
        else:
            print(c+d)

Submission status: Wrong Answer

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

print(s)

c = 0
a = 0
b = 0
ab = 0

for i in range(0, n):
    for j in range(1, len(s[i])):
        if s[i][j-1:j+1] == "AB":
            c += 1
    if s[i][-1] == "A" and s[i][0] == "B":
        ab += 1
    else: 
        if s[i][0] == "B":
            b += 1
        if s[i][-1] == "A":
            a += 1

s = c
m = min(a, b)
s += m
a -= m
b -= m

if(ab > 0):
    s += ab - 1
    if(a > 0):
        s += 1
    if(b > 0):
        s += 1

print(s)

Submission status: Wrong Answer

Solution code:
N = int(input())

ans = 0
dic = {"AB": 0, "A": 0, "B": 0}
for i in range(N):
    S = list(str(input()))
    pre = S[0]
    for j in range(1, len(S)):
        if (S[j-1] + S[j]) == "AB":
            ans += 1
    if S[-1] == "A" and S[0] == "B":
        dic["AB"] += 1
    elif S[-1] == "A":
        dic["A"] += 1
    elif S[0] == "B":
        dic["B"] += 1

# make pairs
ans += min(dic["A"], dic["B"])
ans += max(int(dic["AB"] - 1), 0)

# remain
remain = abs(dic["A"] - dic["B"])
if remain > 0:
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = 0
B = 0
ans = 0

S = list()

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

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

if A > B:
    ans += B
else:
    ans += A

print(ans)

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


p = min(l, m) + n
m -= min(l, m)
l -= min(l, m)
if p > 0:
    ans += p
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
cnt = 0
ca = 0
cb = 0
cab = 0

for i in range(n):
    s = input()
    cnt += s.count('AB')
    if s[-1] == 'A':
        ca += 1
    if s[0] == 'B':        
        cb += 1
    if s[-1] == 'A' and s[0] == 'B':
        cab += 1

m1 = min(ca, cb)
m2 = max(ca, cb)
if m2 == cab:
    m1 -= 1
print(cnt+m1)

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),6))
for u,string in enumerate(string_list):
  # そもそもA,B単発の可能性
  if len(string) == 1:
    if string[0] == "A":
      check_sheet[u][4] = 1
    elif string[0] == "B":
      check_sheet[u][5] = 1
  else:
    # "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,A,B
# どんどん組みを作ったやつは引いていった方があとで計算しやすい
sums=np.sum(check_sheet,axis=0)
#print(sums)
_c = min(sums[1],sums[2])
c = min(sums[4],sums[5])
counts = sums[0] + _c + c
sums[1:3] -= _c
sums[4:] -= c
#print(sums)
if sums[3] != 0:
  counts += (sums[3]-1)
  ## B-Aに、必ず1つはつけられる！が、残ってる組み合わせは知っておきたい...
  ## -A,Bの組み合わせ
  if sums[1] > 0 and sums[5] > 0:
    # B-Aの両端にまずつける
    counts += 2
    # その上で、残ってる数のウチ作れる分だけ作って追加
    counts += min(sums[1]-1,sums[5]-1)
  ## B-,Aの組み合わせでも同様
  elif sums[2] > 0 and sums[4] > 0:
    # B-Aの両端にまずつける
    counts += 2
    # その上で、残ってる数のウチ作れる分だけ作って追加
    counts += min(sums[2]-1,sums[4]-1)
  ## 全部0とか出なければ、+1
  elif np.sum(sums[1:]) > sums[3] : 
    counts += 1
else:
  # 組み合わせが適切であれば追加できる
  if sums[1] > 0 and sums[5] > 0:
    counts += min(sums[1],sums[5])
  elif sums[2] > 0 and sums[4] > 0:
    counts += min(sums[2],sums[4])
print(int(counts))

Submission status: Wrong Answer

Solution code:
n = int(input())
a,b,ab,pure = 0,0,0,0
for i in range(n):
    s = input()
    pure += s.count("AB")
    if s.startswith("B"): b += 1
    if s.endswith("A"): a += 1
    if s.startswith("B") and s.endswith("A"): ab += 1

ans = pure + min(a,b)
bias = ab-1 - abs(a-b)
if bias>0: ans -= bias
print(ans)

Submission status: Wrong Answer

Solution code:
import re

N = int(input())
string_list = [input() for i in range(N)]
count = 0
A_count = 0
B_count = 0
for i in string_list:
    count += len(re.findall('AB', i))
    if i[-1] == 'A':
        A_count += 1
    if i[0] == 'B':
        B_count += 1
if A_count < B_count or A_count == B_count:
    count += A_count
elif A_count > B_count:
    count += B_count
print(count)

Submission status: Wrong Answer

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

count_AB = 0
lefter_B = 0
righter_A = 0
only_lefter_B = 0
only_righter_A = 0
for i in range(N):
    count_AB += S[i].count('AB')
    if S[i][0] == 'B':
        lefter_B += 1
    if S[i][len(S[i])-1] == 'A':
        righter_A += 1
    if (S[i][0] == 'B')and(S[i][len(S[i])-1] != 'A'):
        only_lefter_B += 1
    if (S[i][0] != 'B')and(S[i][len(S[i])-1] == 'A'):
        only_righter_A += 1
        
if (only_lefter_B==0) and (only_righter_A==0):
    Appearing_AB = min(lefter_B,righter_A) - 1
else:
    Appearing_AB = min(lefter_B,righter_A)


print(count_AB + Appearing_AB)

Submission status: Wrong Answer

Solution code:
N = int(input())
AB_number = 0
ab_cnt =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" and s_i[-1] == "A":
        ab_cnt += 1
    elif s_i[-1] == "A":
        a_cnt += 1
    elif s_i[0] == "B":
        b_cnt += 1

if ab_cnt== 0:
    print(AB_number+min(a_cnt,b_cnt))
elif a_cnt != b_cnt:
    print(AB_number+(ab_cnt-1)+1+min(a_cnt,b_cnt))
else:
    print(AB_number+(ab_cnt-1)+min(a_cnt,b_cnt))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in range(n)]
counter = 0
startWithB = 0
endWithA = 0
both = 0


for i in range(n):
    counter += s[i].count("AB")
    if s[i][0] == "B":
        if s[i][-1] == "A":
            both += 1
        else:
            startWithB += 1
    elif s[i][-1] == "A":
        endWithA += 1

if abs(startWithB - endWithA) > both:
    print(min(startWithB + both, endWithA + both)+counter)
else:
    print((startWithB + both + endWithA+both)//2+counter)

Submission status: Wrong Answer

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

ab = 0
ab_ = 0
a = 0
b = 0
for i in s:
  ab += i.count("AB")
  if i[0] == "B" and i[1] == "A":
    ab_ += 1
  if i[0] == "B":
    b += 1
  if i[-1] == "A":
    a += 1
if ab_ == a and ab_ == b:
  print(ab+a-1)
else:
  print(ab+min([a,b]))

Submission status: Wrong Answer

Solution code:
import re

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

tmp = 0

print(s[1],s[1][-1:],s[1][0:1])

B_A = 0
B_any = 0
any_A = 0

for i in range(N):
    tmp = tmp + s[i].count("AB")
    if s[i][0:1] == "B":
        if s[i][-1:] == "A":
            B_A = B_A + 1
        else:
            B_any = B_any + 1
    elif s[i][-1:] == "A":
        any_A = any_A + 1

add = 0

add1 = min(B_any,any_A)

if B_A % 2 == 0:
    add = int(B_A / 2)
else:
    if B_any == any_A:
        add = int((B_A-1) / 2)
    else:
        add = int((B_A-1) / 2) + 1

print(tmp+add+add1)

Submission status: Wrong Answer

Solution code:
import re
n = int(input())
a = 0
b = 0
res = 0
ab = 0
for i in range(n):
    string = input()
    res += len( re.findall('AB', string))
    if string[0] == "B":
        b += 1
        if string[-1] == "A":
            ab += 1
    if string[-1] == "A":
        a += 1

if ab == a == b:
    res += (ab - 1)
else:
    res += min(a, b)
print(res)

Submission status: Wrong Answer

Solution code:
n = int(input())

g1 = []
g2 = []
g3 = []

cnt = 0

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

if a == b:
    cnt += a
    if c > 0:
        cnt += c - 1
elif a > b:
    cnt += b
    cnt += c
elif b > a:
    cnt += a
    cnt += c

print(cnt)

Submission status: Wrong Answer

Solution code:
import re

N = int(input())

startB = 0
stopA = 0
BtoA = 0
AB = 0

for i in range(N):
    s = input()
    if s.startswith("B") and s.endswith("A"):
        BtoA += 1
    elif s.startswith("B"):
        startB += 1
    elif s.endswith("A"):
        stopA += 1
    
    AB += len(re.findall("AB", s))

if abs(startB - stopA) > 0:
    ans = AB + min(startB, stopA) + BtoA
else:
    ans = AB + min(startB, stopA) + BtoA - 1
print(ans)

Submission status: Wrong Answer

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

count = 0
A_count = 0
B_count =0
# 文字列の中にある"AB"の個数(下限)
for s in str_list:
    if "AB" in s:
        count += 1
    # 末尾がAの文字列の個数
    if s[-1] == "A":
        A_count += 1
    if s[0] == "B":
        B_count += 1

set_num = min([A_count, B_count])

print(count + set_num)

Submission status: Wrong Answer

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

ans = 0
num_a = 0
num_b = 0
num_ba = 0
num_ab = 0
for i in s:
    if(i[-1]=='A' and i[0]=='B'):
        num_ba += 1
    elif(i[-1]=='A'):
        num_a+=1
    elif(i[0]=='B'):
        num_b+=1
    last = ''
    for k in i:
        if(k=='A'):
            last = 'A'
        elif(last == 'A' and k=='B'):
            num_ab += 1
            last = ''
        else:
            last = ''

ans += num_ab

num_conn = min(num_a,num_b)
if(num_a!=num_b and num_ba!=0):
    ans+=num_ba+num_conn
elif(num_ba==0):
    ans+=num_conn
else:
    ans+=num_ba+num_conn
print(ans)