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())

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" or s[-1] != "A":
            check = 0

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

Submission status: Wrong Answer

Solution code:
def countAB(S):
  ct=0
  flg = True
  pos=0
  while flg:
    idx = S.find('AB',pos)
    if idx!=-1:
      ct+=1
    else:
      flg = False
    pos = idx + 2
  return ct
 
N = int(input())
 
num_AB = 0
num_A = 0
num_B = 0
num_BA = 0
for n in range(N):
  S = input()
  num_AB += countAB(S)
  if S[len(S)-1]=='A':
    num_A += 1
  if S[0]=='B':
    num_B += 1
  if S[len(S)-1]=='A' and S[0]=='B': 
    num_BA += 1
    
num_AB_cmb = min(num_A,num_B)
if num_A == num_BA and num_B == num_BA:
  num_AB_cmb = num_BA-1
 
print("{0}".format(num_AB + num_AB_cmb))

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
for i in range (0,n):
  le=len(li[i])-1
  if li[i][0]=="B":
    sta=sta+1
  if li[i][le]=="A":
    las=las+1
if sta==las==n:
  print(t+n-1)
elif sta==n:
  print(t+las)
elif las==n:
  print(t+sta)
elif sta<las:
  print(t+sta)
elif sta>las:
  print(t+las)
else:
  print(t+sta)

Submission status: Wrong Answer

Solution code:
import re

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

counter = 0
B_begin=0
A_end =0
AB_in =0
B_begin_and_A_end = 0


for i in range(N):
    m = len(re.findall('AB',s[i]))
    AB_in += m

    chech_b = False
    chech_a = False

    if s[i][0]=='B':
        B_begin +=1
        chech_b = True

    if s[i][-1]=='A':
        A_end+=1
        chech_a = True

    if chech_a and chech_b:
        B_begin_and_A_end+=1

ans = 0 
mini = min(B_begin, A_end)

if B_begin == A_end ==B_begin_and_A_end:
    ans = AB_in + m-1

else:
    ans = AB_in + mini

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s_b = 0
s_a = 0
s_ab = 0
s_a_b = 0
for i in range(n):
  s = input()
  s_ab += s.count("AB")
  if s[0] == "B" and s[-1] == "A":
      s_a_b += 1
  elif s[0] == "B":
    s_b += 1
  elif s[-1] == "A":
    s_a += 1
ans = s_ab+s_a_b+min(s_b,s_a)
if s_a_b == n:
  ans -= 1
print(ans)

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 BA == N:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [0]*n

ans = 0
initb = 0
lasta = 0
add = 0

for i in range(n):
    s[i] = input()
    st = s[i]

    if st[0] == "B":
        initb += 1

    if st[len(st)-1] == "A":
        lasta += 1

    for j in range(len(st)):

        if st[j] == "A":

            if j == len(st) - 1:
                break
            if st[j+1] == "B":
                ans += 1

if initb <= lasta:
    add = initb
else:
    add = lasta

ans = ans + add

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

ans = 0
A_flag = 0
B_flag = 0
A_flag2 = 0
B_flag2 = 0

for i in range(N):
    S = input()
    ans += S.count('AB')

    if S[0] == 'B':
        if A_flag:
            ans += 1
            A_flag -= 1
        else:
            B_flag += 1

    if S[-1] == 'A':
        if B_flag:
            ans += 1
            B_flag -=1
        else:
            A_flag += 1

    if S[0] != 'B' and S[-1] == 'A':
        A_flag2 = 1

    if S[0] == 'B' and S[-1] != 'A':
        B_flag2 = 1

if A_flag*B_flag != 0:
    ans += min(A_flag,B_flag)

if A_flag+B_flag == 0:
    if A_flag2*B_flag2 == 0:
        ans -= 1
print(ans)

Submission status: Wrong Answer

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

for i in range(N):
    S = input()
    for j in range(len(S)-1):
        if S[j:j+2] == 'AB':
            c += 1
    if S[0] == 'B' and S[-1] == 'A':
        v += 2
        continue
    if S[0] == 'B':
        b += 1
    if S[-1] == 'A':
        a += 1

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

print(c + min(a, b))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

# coding: utf-8

# In[5]:

import sys
input = sys.stdin.readline
N = int(input())
S = [input() for i in range(N)]


# In[10]:

b_cnt = 0
a_cnt = 0
ab_cnt = 0
for i in range(N):
    if S[i][0]=='B'and S[i][-1]=='A':
        ab_cnt += 1
    elif S[i][0]=='B':
        b_cnt += 1
    elif S[i][-1]=='A':
        a_cnt += 1
#print(b_cnt, a_cnt)
#print(ab_cnt, a_cnt, b_cnt, '/'.join(S).count('AB') )
if abs(a_cnt-b_cnt) == 0:
    ab_cnt -= 1
#ab_cnt = ab_cnt-1 if ab_cnt!=0 else ab_cnt
print('/'.join(S).count('AB') + min(a_cnt, b_cnt, N-1) + ab_cnt)


# In[ ]:

Submission status: Wrong Answer

Solution code:
def main():
	N = int(input())
	S = [""]*N
	ans = 0
	a_end = 0
	b_begin = 0
	a_end_b_begin = 0
	for i in range(N):
		S[i] = input()
		if "AB" in S[i]:
			ans += S[i].count("AB")
		if S[i][0] == "B":
			if S[i][-1] == "A":
				a_end_b_begin += 1
				continue
			b_begin += 1
		if S[i][-1] == "A":
			a_end += 1
	ans += min(b_begin, a_end)
	if b_begin > 0 or a_end > 0:
		ans += a_end_b_begin
	else:
		ans += a_end_b_begin-1

	print(ans)


if __name__ == '__main__':
	main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/diverta2019/tasks/diverta2019_c

N = int(input())
s = []
for i in range(N):
    s.append(list(str(input())))

flag = False
count = 0
for i in range(len(s)):
    for j in range(len(s[i])):
        if s[i][j] == 'A':
            flag = True
        elif flag == True and s[i][j] == 'B':
            flag = False
            count += 1
        else:
            flag = False

#print(count)

startcnt = [0] * N
endcnt = [0] * N
for i in range(len(s)):
    if s[i][0] == 'B':
        startcnt[i] = 1
    if s[i][len(s[i])-1] == 'A':
        endcnt[i] = 1

cntpls = [0,0,0]
for i in range(N):
    if startcnt[i] + endcnt[i] == 1:
        if startcnt[i] == 1:
            cntpls[0]+= 1
        else:
            cntpls[1] += 1
    elif startcnt[i] + endcnt[i] == 2:
        cntpls[2] += 1

#print(cntpls)

count += cntpls[2] - 1
if cntpls[1] >= 1:
    count += 1
    cntpls[1] -= 1

if cntpls[0] >= 1:
    count += 1
    cntpls[0] -= 1

count += min(cntpls[0],cntpls[1])


print(count)

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)
	while('AB' in ss):
		ans += 1
		ss = ss.replace("AB", "", 1)
	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

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 = input()
N = int(N)

ss = [input() for i in range(N)]

ss_ab_count = sum([s.count('AB') for s in ss])

end_a = [1 if s[-1] == 'A' else 0 for s in ss]
start_b = [1 if s[0] == 'B' else 0 for s in ss]

for i in range(N):
    if end_a[i] == start_b[i] == 1:
        if sum(end_a) < sum(start_b):
            start_b[i] = 0
        else:
            end_a[i] = 0

start_b_count = sum(start_b)
end_a_count = sum(end_a)

if start_b_count < end_a_count:
    count = ss_ab_count + start_b_count
else:
    count = ss_ab_count + end_a_count

print(count)

Submission status: Wrong Answer

Solution code:
def solve():
    N = int(input())
    S = [input() for _ in range(N)]
    AB_count = 0
    # 元から含まれているABの計算
    head_B = 0
    tail_A = 0
    for si in S:
        # print(si.count('AB'))
        AB_count += si.count('AB')
        # print(list(si)[0], list(si)[-1])
        if list(si)[0] == 'B':
            if tail_A > 0:
                AB_count += 1
                tail_A -= 1
            else:
                head_B += 1
        if list(si)[-1] == 'A':
            if head_B > 0:
                AB_count += 1
                head_B -= 1
            else:
                tail_A += 1
    # 新たに作成できるABの計算
    # print(AB_count, head_B, tail_A)
    # print( tail_A == head_B == N)
    # AB_count += min(head_B, tail_A)
    # if tail_A == head_B == N:
        # 輪にはできないので1引く
        # AB_count -= 1
    print(AB_count)


if __name__ == '__main__':
    solve()

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":
        counta += 1
        countb += 1
    elif s[0] == "B":
        countb += 1
    elif s[-1] == "A":
        counta += 1

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

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



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

e=b-c

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

Submission status: Wrong Answer

Solution code:
N = int(input())
a, b, bxxa, ab = 0, 0, 0, 0

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

if a>b :
    print(ab+b)
elif a==b and bxxa==a:
    print(ab+a-1)
else: 
    print(ab+a)

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:
    if _anum == banum:
        print(ct + min(_anum, b_num) + (banum - 1))
    else:
        print(ct + min(_anum, b_num) + banum)
else:
    print(ct + min(_anum, b_num))

Submission status: Wrong Answer

Solution code:
n = int(input())

count = 0
bstart = 0
aend = 0
for _ in range(n):
    si = input()
    if si[0] == 'B':
        bstart += 1
    if si[-1] == 'A':
        aend += 1
    for i in range(len(si)-1):
        if si[i] == 'A' and si[i+1] == 'B':
            count += 1

count += min(bstart, aend, n-1)

print(count)

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
 
 
#print(ans,end_A,start_B,same_time)
final=ans+min(end_A-same_time,start_B-same_time)
if end_A!=start_B:
    print(final+same_time)
else:
    print(final+max(same_time-1,0))

Submission status: Wrong Answer

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

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N=INT()
S=['']*N
for i in range(N):
    S[i]=input()

enda=0
startb=0
both=0
cntab=0
for i in range(N):
    if S[i].startswith('B') and S[i].endswith('A'):
        both+=1
    elif S[i].startswith('B'):
        startb+=1
    elif S[i].endswith('A'):
        enda+=1
    cntab+=S[i].count('AB')

mnab=min(enda, startb)
cntab+=mnab
if both!=0:
    enda-=mnab
    startb-=mnab
    mxab=max(enda, startb)
    if mxab<=both:
        cntab+=mxab+(both-mxab)
    else:
        cntab+=both
print(cntab)

Submission status: Wrong Answer

Solution code:
# import random
#
# dist = ["#","."]
#
# f = open("in_sample.txt","w")
#
# f.write("1000 1000\n")
#
# for i in range(1000):
#     temp =[]
#     for j in range(1000):
#         temp.append(random.choice(dist))
#     f.writelines(temp)
#     f.write("\n")


n = int(input())
count = 0
enda = 0
startb = 0
both = 0
for i in range(n):
    s = input()
    flag = False
    for i in range(len(s)):
        if flag and s[i] == "B":
            count += 1
            flag = False
        else:
            flag = False
        if s[i] == "A":
            flag = True
    if s[0] == "B" and s[-1] == "A":
        both += 1
    elif s[0] == "B":
        startb += 1
    elif s[-1] == "A":
        enda += 1

if startb != enda:
    if both > 2:
        print(count + both-1 + min(enda,startb) + 1)
    else:
        print(count + min(enda, startb)+ both)

else:
    if both > 2:
        print(count + both - 1 + enda)
    else:
        print(count + enda)

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

s = c

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

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

Submission status: Wrong Answer

Solution code:
n=int(input())
c1=c2=c3=0
c=0
for i in range(n):
  s=input()
  c+=s.count('AB')

  if s[0]=='B' and s[-1]=='A':
    c3+=1
  elif s[0]=='B':
    c1+=1
  elif s[-1]=='A':
    c2+=1

c+=max(0,c3-1)
if c1<c2:
  c2-=c1
  c+=c1
  if c2>0 and c3>0:
    c+=1
elif c1==c2:
  c+=c1
else:
  c1-=c2
  c+=c2
  if c1>0 and c3>0:
    c+=1

print(c)

Submission status: Wrong Answer

Solution code:
N = int(input())

cnt = 0
list = [0,0,0]

for i in range(N):
    s = input()
    cnt += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        list[0] += 1
    elif s[-1] == 'A':
        list[1] += 1
    elif s[0] == 'B':
        list[2] += 1

min_ = min(list[0],list[1],list[2])

if min_ >= 1:
    cnt += 2*min_
    list[0] -= min_
    list[1] -= min_
    list[2] -= min_

if list[0] >= 2:
    cnt += 2*list[0]//2
    list[0] -= 2*(list[0]//2)

if list[1] == list[2]:
    cnt += list[1]
elif list[1] > list[2]:
    cnt += list[2] + min(list[0],list[1]-list[2])
elif list[1] < list[2]:
    cnt += list[1] + min(list[0],list[2]-list[1])

print(cnt)

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 = min(0,AB-1)
elif A==1 and B == 0:
    res = AB
elif A==0 and B ==1:
    res = AB
elif AB >0:
    res = AB+1 + min(A-1,B-1)
else:
    res = min(A,B)
print(res+s)

Submission status: Wrong Answer

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

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



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

Submission status: Wrong Answer

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

n = int(input())
AA = []
BA = []
BZ = []

AZ = []

count = 0
l = 0
AAc = 0
BAc = 0
BZc = 0
for i in range(n):
    t = input()
    count += t.count('AB')
    ss = list(t)
    if ss[0] == 'B' and ss[-1] == 'A':
        BA.append(1)
        BAc += 1
        l += 1
    elif ss[0] != 'B' and ss[-1] == 'A':
        AA.append(1)
        AAc += 1
        l += 1
    elif ss[0] == 'B' and ss[-1] != 'A':
        BZ.append(1)
        BZc += 1
        l += 1
if AAc != 0 and BA != 0:
    count += BAc
    AAc -= 1
    if BZc != 0:
        BZc -= 1
        count += 1
    if AAc != 0 and BZc !=0:
        count += min(AAc,BZc)
elif AAc != 0 and BAc != 0:
    count += min(BAc,BZc)
print(count)

Submission status: Wrong Answer

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

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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

N = int(inp())
inAB = 0
sB = 0
eA = 0
both = 0
for _ in range(N):
  s = inp()
  inAB += s.count('AB')
  if s[0] == 'B' and s[-1] == 'A':
    both += 1
    continue
  if s[0] == 'B':
    sB += 1
  if s[-1] == 'A':
    eA += 1

pair = min(eA, sB) if min(eA, sB) > 0 else -1
print(inAB + max(both + pair, 0))

Submission status: Wrong Answer

Solution code:
n = int(input())
S = [input() for i in range(n)]
cnt = 0
a = 0
b = 0
for i in range(n):
    cnt += S[i].count("AB")
    if S[i].startswith("B"):
        a += 1
    if S[i].endswith("A"):
        b += 1
if min(a, b) > n - 1:
    print(cnt + n - 1)
else:
    print(cnt + min(a, b))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ss = [input() for i in range(N)]

ss_ab_count = sum([s.count('AB') for s in ss])

end_a = [1 if s[-1] == 'A' else 0 for s in ss]
start_b = [1 if s[0] == 'B' else 0 for s in ss]

for i in range(N):
    if end_a[i] == start_b[i] == 1:
        if sum(end_a) < sum(start_b):
            start_b[i] = 0

start_b_count = sum(start_b)
end_a_count = sum(end_a)

if start_b_count < end_a_count:
    count = ss_ab_count + start_b_count
else:
    count = ss_ab_count + end_a_count

print(count)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
ss = [input() for _ in range(N)]

num_ab = 0
pres = []
posts = []
ss2 = []
for s in ss:
    while True:
        if len(s) > 1:
            if s[0:2] == 'AB':
                s = s[2:]
                num_ab += 1
            else:
                break
    while True:
        if len(s) > 1:
            if s[-2:] == 'AB':
                s = s[:-2]
                num_ab += 1
            else:
                break
#     print(s)
    if s[0] == 'A':
        pres.append(1)
    elif s[0] == 'B':
        pres.append(-1)
    else:
        pres.append(0)
    if s[-1] == 'A':
        posts.append(1)
    elif s[-1] == 'B':
        posts.append(-1)
    else:
        posts.append(0)
        

    while True:
        i = s.find('AB')
        if i >= 0:
            num_ab += 1
            s = s[i + 2:]
        else:
            break
pre_num = np.sum(np.array(pres) == -1) 
post_num = np.sum(np.array(posts) == 1)
num_ab += np.min([pre_num, post_num])

pre_num = np.sum(np.array(pres) == 1) 
post_num = np.sum(np.array(posts) == -1)
num_ab += np.min([pre_num, post_num])

        
print(num_ab)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
ab=0
a=[0]*n
b=[0]*n
for i in range(n):
  s=input()
  ab+=s.count("AB")
  if(s[0]=='B'):
    b[i]=1
  if(s[-1]=='A'):
    a[i]=1
c=0
for i in range(n):
  for j in range(n):
    if(i!=j and a[i]==1 and b[j]==1):
      a[i]=0
      b[i]=0
      c+=1
      break
print(ab+c)

Submission status: Wrong Answer

Solution code:
import re

N = int(input())
lst = []
bl = True

for i in range(N):
    s = input()
    n = len(re.findall(r'AB', s))
    m = len(re.findall(r'A$', s))
    l = len(re.findall(r'^B', s))
    lst.append((n, m, l))
    if m != l:
        bl = False


ans = sum([l[0] for l in lst]) + min(sum([l[1] for l in lst]), sum(l[2] for l in lst))
if bl:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for u in range(n)]
a = sum([u.count("AB") for u in s])
b = len([1 for u in s if u.endswith("A")])
c = len([1 for u in s if u.startswith("B")])
print(a+min(b,c))

Submission status: Wrong Answer

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

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

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

  c += s[i].count('AB')

c += min(min(a, b), n-1)

if ab == a and ab == b and ab < n:
  c -= 1

print(c)

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
    if ab-k>1 and (ab-k)%2:
        ans += 1
    if a>b and ab>a-b:
        ans += 1
else:
    k = min(ab,b-a)
    ans += a+k+(ab-k)//2
    if ab-k>1 and (ab-k)%2:
        ans +=1
    if ab>0 and ab>b-a:
        ans += 1
print(ans)

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
last = ""
while True:
    if len(a_list) != 0:
        if len(b_list) != 0:
            a = a_list.pop()
            b = b_list.pop()
            ans += 1
        elif len(both) != 0:
            a = a_list.pop()
            b = both.pop()
            ans += 1
        else:
            break
    elif len(both) != 0:
        if len(b_list) != 0:
            if last == "A":
                ans += 1
            a = both.pop()
            b = b_list.pop()
            ans += 1
        elif len(both) >= 2:
            if last == "A":
                ans += 1
            a = both.pop()
            b = both.pop()
            ans += 1
        else:
            break
    else:
        break
    ans += a.count("AB")
    ans += b.count("AB")
    last = b[-1]

if last == "A" and (len(b_list) != 0 or len(both) != 0):
    if len(b_list) != 0:
        ans += 1
        b = b_list.pop()
        ans += b.count("AB")
        last = b[-1]
    else:
        b = both.pop()
        ans += 1
        ans += b.count("AB")
        last = b[-1]

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:
    if last == "A":
        ans += 1
    a = both.pop()
    ans += a.count("AB")
    last = b[-1]

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

print(ans)

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
artificial_ans = min(start_B, end_A)
if BA > 0:
    artificial_ans += BA - 1
if (start_B != end_A) and BA > 0:
    artificial_ans += 1
if artificial_ans == N:
    artificial_ans -= 1
print(ans+artificial_ans)

Submission status: Wrong Answer

Solution code:
#C
N = int(input())
t = [''] * N
num = 0
for k in range(0, N):
    s = str(input())
    t[k] = s
    num += s.count('AB')

counts = []
for i in range(0, N):
    count = num
    for j in range(0, N):
        if i == j:
            pass
        if t[i][-1] == 'A' and t[j][0] == 'B':
            count += 1
    counts.append(count)
print(max(counts))

Submission status: Wrong Answer

Solution code:
n=int(input())
ss=[]
for _ in range(n):
    s=input()
    if 'A' in s or 'B' in s:
        ss.append(s)
c = sum(s.count('AB') for s in ss)
c += min(sum(s[0] == 'B' for s in ss), sum(s[-1] == 'A' for s in ss))
print(c)

Submission status: Wrong Answer

Solution code:
n=int(input())  #数値入力
listA=[] #appendのために宣言が必要
for i in range(n):
    listA.append(input())
# print(listA)

ABin = 0
countA = 0
countB = 0
for string in listA:
    if 'AB' in string:
        ABin += 1
    if string[-1] == 'A':
        countA += 1
    if string[0] == 'B':
        countB += 1
print(ABin + min(countA,countB))

Submission status: Wrong Answer

Solution code:

# coding: utf-8

# In[5]:

import sys
input = sys.stdin.readline
N = int(input())
S = [input() for i in range(N)]


# In[10]:

b_cnt = 0
a_cnt = 0
ab_cnt = 0
for i in range(N):
    if S[i][0]=='B'and S[i][-1]=='A':
        ab_cnt += 1
    elif S[i][0]=='B':
        b_cnt += 1
    elif S[i][-1]=='A':
        a_cnt += 1
#print(b_cnt, a_cnt)
#print(ab_cnt, a_cnt, b_cnt, '/'.join(S).count('AB') )
if abs(a_cnt-b_cnt) == 0:
    ab_cnt -= 0
#ab_cnt = ab_cnt-1 if ab_cnt!=0 else ab_cnt
print('/'.join(S).count('AB') + min(a_cnt, b_cnt, N-1) + ab_cnt)


# In[ ]:

Submission status: Wrong Answer

Solution code:
i = 1
n = input("")
num = int(n)
string = input("")
array = [string]
while i < num :
  strtemp = input("")
  array.append(strtemp)
  i+=1
front = 0
back = 0
is_a = 0
is_b = 0
is_ab = 0
for tmp in array :
  char = list(tmp)
  for tmp2 in char :
    if tmp2 == "A" :
      is_a = 1
      is_b = 0
    if is_a == 1 and tmp2 == "B" :
      is_ab += 1
      is_a = 0
    elif tmp2 == "B" :
      is_a = 0
      is_b = 1
      if char[0] == tmp2 :
        front += 1
  if is_a == 1 :
    back += 1
  is_a = 0
  is_b = 0
if front >= back :
  is_ab += back
elif back > front :
  is_ab += front
print(is_ab)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
a =[0 for i in range(n)]
b = [0 for i in range(n)]
c = [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 b[i] == 1 and a[i] == 1:
        c[i] = 1
        a[i] = 0
        b[i] = 0



A = sum(a)
B = sum(b)
C = sum(c)
if C >= 1:
    ans += C-1
    if A >= 1 and B >= 1:
        ans += 2 + min(A-1,B-1)
    else:
        ans += 1
else:
    ans += min(A,B)

print(ans)

Submission status: Wrong Answer

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

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" or s[-1] !="A":
            check = 0

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