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: Runtime Error

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

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

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

print(max(c,cc))

Submission status: Runtime Error

Solution code:
from numpy.core.defchararray import endswith
N = int(input())
count = 0
flag = "A"
words = []
ketugo = ""
for i in range(N):
    temp = input().rstrip()
    words.append(temp)

words.sort(key=str,reverse=True)

while(words != []):
    for idx,i in enumerate(words):
        if i.endswith('A') == True:
            ketugo += i
            words.pop(idx)
        else:
            pass

        if ketugo.endswith('A') == True and i.startswith('B') == True:
            ketugo += i
            words.pop(idx)


    a_count = 0
    b_count = 0

    for i in words:
        if i.endswith('A') == True:
            a_count += 1
        elif i.startswith('B') == True:
            b_count += 1

    if a_count > 0 and b_count > 0:
        pass
    else:
        for idx,i in enumerate(words):
            ketugo += i
            words.pop(idx)


count = ketugo.count('AB')

print(count)

Submission status: Runtime Error

Solution code:
v = [int(d) for d in input().split()]
f = []

for e in range(v[0]):
    f.append(input())
re = 0
for d in f:
    re += d.count('AB')
dd = [list(s) for s in f]

print(dd)

b = 0
a = 0
coo= 0

for q in dd:
    if q[0] == 'B':
        b += 1
    if q[-1] == 'A':
        a += 1
        if q[0] == 'B':
            coo += 1

if a > b:
    ss = b
elif a < b:
    ss = a
elif a == b:
    ss == a

if ss == coo:
    if ss == b:
        if ss == a:
            if ss != 0:
                ss -= 1
re = re + ss
print(re)

Submission status: Runtime Error

Solution code:
n = int(input())

s = [input() for i in range(n)]
count = 0
count_A = 0
count_B = 0
count_A_B = 0
for i in range(n):
    if "A" in s[i][-1] and "B" not in s[i][0]:
        count_A += 1
    if "B" in s[i][0] and "A" not in s[i][-1]:
        count_B += 1
    if "A" in s[i][-1] and "B" in s[i][0]:
        count_A_B += 1
    if "AB" in s[i]:
        count += 1
if max(count_A, count_B) != 0:
    print(min(count_A, count_B) + count + count_A_B)
else:
    print(cout + count_A_B - 1)

Submission status: Runtime Error

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 and e == 0:
  print(b + c)
else if c == d and e == c:
  print(b + c - 1)
else:
  f = min(c, d)
  print(b + f)

Submission status: Runtime Error

Solution code:
N= int(input())
s = [input() for _ in range(N)]
cnt = 0
s_new = None
begin_B = []
end_A = []
AandB = []
other = []
for i in range(N):
  if s[i][0] == 'B' and s[i][-1] == 'A':
    AandB.append(i)
  elif s[i][0] == 'B':
    begin_B.append(i)
  elif s[i][-1] == 'A':
    end_A.append(i)
  else:
    other.append(i)
index = 0
if other:
  s_new = s[other.pop(-1)]
while True:
  #print('begin_B',begin_B,'end_A',end_A,'other','AandB',AandB,other,s_new,index,cnt)
  if index + 1 == len(s_new):
    if not begin_B and not end_A and not other and not AandB:
      break
    elif not begin_B and not AandB:
      if end_A:
        for i in end_A:
          s_new = s_new + s[i]
          end_A = []
      if other:
        for i in other:
          s_new = s_new + s[i]
          other = []
    else:
      if s_new[index] == 'A':
        if AandB:
          s_new = s_new + s[AandB.pop(0)]
          cnt += 1
        elif begin_B:
          s_new = s_new + s[begin_B.pop(0)]
          cnt += 1
      elif end_A:
        s_new = s_new + s[end_A.pop(0)]
      elif other:
        s_new = s_new + s[other.pop(0)]
      elif AandB:
        s_new = s_new + s[AandB.pop(0)]
        cnt+=1
      elif begin_B:
        s_new = s_new + s[begin_B.pop(0)]
        cnt += 1
  else:
    if s_new[index] == 'A' and s_new[index+1] == 'B':
      cnt += 1
  index += 1
print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
s = []
for _ in range(N):
    s.append(input())
import copy
S = copy.deepcopy(s)
both = []
A_tail = []
B_head = []
other = []
for text in S:
    if text[0] == "B" and text[-1] == "A":
        both.append(text)
    elif text[0] == "B":
        B_head.append(text)
    elif text[-1] == "A":
        A_tail.append(text)
    else:
        other.append(text)
concat = []
for both_text in both:
    concat.extend(both_text)
a_idx = []
for idx,A_tail_text in enumerate(A_tail):
    if len(B_head) >= 1:
        concat.extend(A_tail_text)
        concat.extend(B_head[0])
        B_head.pop(0)
    else:
        a_idx.append(idx)
a_rest = []
for idx in a_idx:
    a_rest.append(A_tail[i])
cnt = 0
for i in range(len(concat)-1):
    if concat[i:i+2] == ["A","B"]:
        cnt += 1
for oth in other:
    for i in range(len(oth)-1):
        if oth[i:i+2] == "AB":
            cnt += 1
for oth in a_rest:
    for i in range(len(oth)-1):
        if oth[i:i+2] == "AB":
            cnt += 1
for oth in B_head:
    for i in range(len(oth)-1):
        if oth[i:i+2] == "AB":
            cnt += 1

Submission status: Runtime Error

Solution code:
import copy
n=int(input())
s=[]

for i in range(n):
    s.append(input())
w=s.copy()
mojiretu=''
mojiretuab=''

for i in range(n):
    if s[i][0]=='B':
        mojiretuab=mojiretuab+s[i]
    else:
        if s[i][-1]=='A':
            if mojiretuab[0]=='B':
                mojiretuab=s[i]+mojiretuab
            else:
                mojiretuab=mojiretuab+s[i]
        else:
            mojiretu=mojiretu+s[i]
m=mojiretuab+mojiretu                
count=0
for i in range(len(m)-1):
    if m[i]=='A':
        if m[i+1]=='B':
            count+=1
print(count)

Submission status: Runtime Error

Solution code:
a = int(input())
b = [input() for i in range(a)]
c = []
d = []
f = []
e = 0
p =[]
x ='0'
for j in range(a):
    if b[j][0] == 'B':
        c.append(b[j])
    elif b[j][-1] == 'A':
        d.append(b[j])
    else:
        p.append(b[j])
while True:
    if not (len(c) == 0) & (len(d) == 0):
        if e%2 == 0:
            f.append(d[0])
            d.remove(d[0])
            e += 1
        else:
            f.append(c[0])
            c.remove(c[0])
            e += 1
    else:
        break
f = f + p
for h in range(len(f)):
    x += f[h]
print(x.count('AB'))

Submission status: Runtime Error

Solution code:
N = int(input())
s =[]
count=0
Acount=0
Bcount=0
BAcount=0
for i in range(N):
    S = input()
    #s.append(S)
    ##AB inside
    for j in range(len(S)-1):
        if S[j] == 'A' and S[j+1]=='B':
            count+=1
    if S[0]=='B' and S[-1]=='A':
        BAcount+=1
    elif S[0]=='B':
        Bcount+=1
    elif S[-1]=='A':
        Acount+=1
#BAを先にくっつける
if BAcount >0:
    count+=BAcount-1
    BAcount=1
    ba = [BAcount,Bcount,Acount]
else:
    BAcount=0
    ba = [BAcount,Bcount,Acount]
#0が含まれているとき
ba = [i for i in ba if not i == 0]
minba =min(ba)
print(ba)
print(count)
if BAcount == minba and len(ba)==3:
    count+=minba*2
    for i in range(3):
        ba[i] = ba[i]-minba
    ba = [i for i in ba if not i == 0]
    print(ba)
    print(count)
    if len(ba) == 2:
        count+= min(ba)
else:
    if len(ba) == 2:
        count+= min(ba)
print(count)

Submission status: Runtime Error

Solution code:
#coding: utf-8
N = int(input())
ans = 0
l_B = 0
r_A = 0
lbra = 0
for _ in range(N):
	tmp = input()
	ans += tmp.count("AB")
	if tmp[0] == "B":
		if tmp[-1] == "A":
			lbra += 1
		else:
			l_B += 1
	elif tmp[-1] == "A":
		r_A += 1
l.sort()
print(ans + lbra + min(l_B, r_A))

Submission status: Runtime Error

Solution code:
import sys

N = int(input())

A = []

for n in range(N):
    chars = np.array(list(input()))
    A.append(chars)

A_cnt = 0
B_cnt = 0
AsBe = 0 # A start, B end
AB_cnt = 0

for n in range(N):
    if A[n][0] == "B":
        B_cnt += 1

    if A[n][-1] == "A":
        A_cnt += 1

    if A[n][-1] == "A" and A[n][0] == "B":
        AsBe += 1
        A_cnt -= 1
        B_cnt -= 1

    for i in range(len(A[n])-1):
        if A[n][i] == "A":
            if A[n][i+1] == "B":
                AB_cnt += 1

if A_cnt > 0:
    a = 1
    A_cnt -= 1
else:
    a = 0

if B_cnt > 0:
    b = 1
    B_cnt -= 1
else:
    b = 0

print(AB_cnt + (AsBe-1) + a + b + min(A_cnt,B_cnt))

Submission status: Runtime Error

Solution code:
N = int(input())
s = []
counter = 0
counter_AB = 0
counter_A = 0
counter_B = 0

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

for n in range(N):
    tmp = s[n]
    counter += tmp.count("AB")
    
    if tmp[0] == "B" and tmp[len(tmp)-1] == "A":
        counter_AB += 1

    elif tmp[0] == "B":
        counter_B += 1
    
    elif tmp[len(tmp)-1] == "A":
        counter_A += 1

counter_sort = [counter_A, counter_B]
counter_sort.sort(reverse = False)

if abs(counter_A - counter_B) >= counter_AB:
    print(counter + counter_sort[0] + counter_AB)

else:
    amari_counter_AB = counter_AB - abs(counter_A - counter_B)
    print(counter + counter_sort[2] + int(amari_counter_AB/2))

Submission status: Runtime Error

Solution code:
str_dict = [0, 0, 0] 
val1 = 0
for i in range(int(input())):
  line = input()
  val1 += line.count('AB')
  if line[0] == 'B' and line[-1] == 'A':
    str_dict[2] += 1
  elif line[0] == 'B':
    str_dict[0] += 1
  elif line[-1] == 'A':
    str_dict[1] += 1
val2 = min(str_dict[0], str_dict[1])
tmp = max(str_dict[0]-val2, str_dict[1]-val2)
val3 = min(tmp, str_dict[2])
val4 = str_dict[2]-val3
if val4 % 2 == 1:
  val4 -= 1
print(val1+val2+val3+val4

Submission status: Runtime Error

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
ans=0
ba=0
bx=0
xa=0
for i in range(n):
  s = stdin.readline().rstrip()
  for j in range(len(s)-1):
    if s[j] == 'A' and s[j+1] == 'B':
      ans +=1
  if( s[0] == 'B' and s[-1] == 'A' ):
    ba +=1
  elif s[0] == 'B':
    bx +=1
  elif s[-1] == 'A':
    xa +=1

ans += min(xa,bx)
if ba>0:
  ans += (ba-1)
  if and xa!=0 and xa!=bx:
    ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
S = []
for i in range(N):
    S.append(str(input()))
    
start_b = 0
end_a = 0
ab_count = 0
for i in S:
    if i[0]=="B":
        start_b = start_b + 1
    if i[-1]=="A":
        end_a = end_a + 1
    if i[0]=="B" and i[-1]=="A":
        both = both + 1
    ab_count = ab_count + i.count("AB")

additional = min(start_b, end_a)
if additional == len(S):
    additional = additional  - 1
elif start_b == both and end_a == both:
    additional = additional  - 1
ans = ab_count + additional
print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
lasta=0
headb=0
spec=0
cnt=0
for _ in range(N):
    s=str(input())
    for i in range(len(s)-1):
        if s[i:i+2]=="AB":
            cnt+=1
    if s[0]=="B" and s[-1]=="A":
        spec+=1
    elif s[-1]=="A":
        lasta+=1
    elif s[0]=="B":
        headb+=1
if abs(headb-lasta)>=spec:
    print(min(headb,lasta)+spec+cnt)
    
elif lasta=0 and headb=0:
    print(spec-1+cnt)
else:
    print(spec-abs(headb-lasta)+max(headb,lasta)+cnt)

Submission status: Runtime Error

Solution code:
N=int(input())
A,B=0
for i in range(N):
  X=input()
  if X[0]==The "B":
    B+=1
    if X[-1]=="A":
      A+=1
      Y=X.count("AB")
Max=min(A,B)
count=Max+Y
print(count)

Submission status: Runtime Error

Solution code:
N=int(input())
A,B=0
for i in range(N):
  X=input()
  if X[0]==The "B":
    B+=1
    if X[-1]=="A":
      A+=1
      Y=X.count("AB")
Max=max(A,B)
count=Max+Y
print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
count = 0
topB = 0
bottomA = 0
for i in range(N):
    s = str(input())
    count += s.count("AB")
    if s[0] == "B":
        topB += 1
    if s[-1] == "A":
        bottomA += 1
if topB =! bottomA:
    print(count + min(topB,bottomA))
else:
    print(count + min(topB,bottomA)-1)

Submission status: Runtime Error

Solution code:
n = int(input())
first_b_num = 0
last_a_num = 0
ab_num = 0

for _ in range(n):
    s_list = list(input())
    for index,s in enumerate(s_list):
        if(index == 0):
            if(s == 'B'):
                first_b_num += 1
        else:
            if(s_list[index-1]+s == 'AB'):
                ab_num += 1
    if(s_list[index] == 'A'):
        last_a_num += 1

if(first_b_num == last_a_num):
    print(ab_num + first_b_num - 1):
else:
    print(ab_num + min(first_b_num,last_a_num))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())
s =[]
count=0
Acount=0
Bcount=0
BAcount=0
for i in range(N):
    S = input()
    #s.append(S)
    ##AB inside
    for j in range(len(S)-1):
        if S[j] == 'A' and S[j+1]=='B':
            count+=1
    if S[0]=='B' and S[-1]=='A':
        BAcount+=1
    elif S[0]=='B':
        Bcount+=1
    elif S[-1]=='A':
        Acount+=1
#BAを先にくっつける
if BAcount >0:
    count+=BAcount-1
    ba = [1,Bcount,Acount]
else:
    ba = [0,Bcount,Acount]
#0が含まれているとき
ba = [i for i in ba if not i == 0]
minba =min(ba)
if BAcount == minba and len(ba)==3:
    count+=minba*2
    for i in range(3):
        ba[i] = ba[i]-minba
    ba = [i for i in ba if not i == 0]
    if len(ba) == 2:
        count+= min(ba)
else:
    if len(ba) == 2:
        count+= min(ba)
print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
for i in range(N):
    print(data[i])
count = 0
for i in range(N):
    if 'AB' in data[i]:
        count += 1
        
B_first = 0
A_end = 0
for i in range(N):
    if 'B' in data[i][0]:
        B_first += 1
        
for i in range(N):
    if 'A' in data[i][len(data[i])-1]:
        A_end += 1    
        
print(count+min(B_first, A_end))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

for i in range(N):
    s = input()
    v.append(s)
    
ab = 0
sbea = 0
sb = 0
ea = 0

for i in range(N):
    s = v[i]
    
    for j in range(N-1):
        if s[j] == 'A' and s[j+1] == 'B':
            ab += 1
            
    if s[0] == 'B' and s[len(s) - 1] == 'A':
        sbea += 1
    elif s[0] == 'B':
        sb += 1
    else:
        ea += 1
        
ans = ab
ans += min(sb, ea)
if sb == ea:
    ans += max(sbea-1, 0)
else:
    ans += sbea
    
print(ans)

Submission status: Runtime Error

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

from itertools import permutations as perm

p = perm(S,N)
count_list = []
for s in p:
    if "AB" in s:
        start = s.find("AB")
        end = s.rfind("AB")
        #print(s)
        join = "".join(s)
        count_list.append(join.count("AB",start,end+2))
print(max(count_list))

Submission status: Runtime Error

Solution code:
#d=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
#import numpy as np
#n=int(input())
#li=input().split()
#import fractions
#lis=list(map(int,input().split()))

n=int(input())
S=[]
ans=0
firstB,lastA,bothAB=0,0,0

for i in range(n):
    S.append(input())
    flag=0
    for j in range(len(S[-1])-1):
        if S[-1][j:j+2]=="AB":
            ans+=1
    if S[-1][0]=="B":
        firstB+=1
        flag+=1
    if S[-1][-1]=="A":
        lastA+=1
        flag+=1
    if flag==2:
        bothAB+=1
        firstB-=1
        lastA-=1

if firstB==lastA:
    if first==0 and bothAB!=0:
        ans+=bothAB-1
    else:
        ans+=(bothAB+firstB)
else:
    ans+=min(firstB,lastA)+bothAB

print(ans)

Submission status: Runtime Error

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

count = 0
a_count = 0
b_count = 0
for i in range(len(s_list)):
    count += s_list[i].count("AB")
    if s_list[i][-1] == "A":
        a_count += 1
    if s_list[i][0] == "B":
        b_count += 1

if a_count  0 or b_count == 0:
    print(count)
elif a_count == len(s_list) and b_count == len(s_list) :
    print(count + a_count - 1)
elif a_count > b_count :
    print(count + b_count)
else :
    print(count + a_count)

Submission status: Runtime Error

Solution code:
n = int(input())
s = 0
count = 0

for x in range(n):
    str = input()
    s += str

for a in s:
    if a == "A":
        ken = "A"
    else:ken = 0
    if ken == "A" and a == "B":
        hikaku = ken + a
    if hikaku == "AB":
        count += 1
print(count)

Submission status: Runtime Error

Solution code:
ans=0
flaga=0

for i in range(N):
    a=A[i].count("A")
    b=A[i].count("B")
    x=min(a,b)
    ans+=x
    if b-x>0 and flaga==1:
        ans+=1
        
    if a-x>0:
        flaga=1
    else:
        flaga=0
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
strings = [input() for _ in range(n)]
ta_hb = list(filter(lambda s: s[-1] == "A" and s[0] == "B", strings))
for s in ta_hb:
    strings.remove(s)
tail_a = list(filter(lambda s: s[-1] == "A", strings))
for s in tail_a:
    strings.remove(s)
head_b = list(filter(lambda s: s[0] == "B", strings))
for s in head_b:
    strings.remove(s)
result = ""
if 0 < len(tail_a) and 0 < len(head_b):
    result = tail_a.pop(0) + "".join(ta_hb) + head_b.pop(0)
elif 0 < len(tail_a):
    result = tail_a.pop(0) + "".join(ta_hb)
elif 0 < len(head_b):
    result = join(ta_hb) + head_b.pop(0)
ab = []
for a,b in zip(tail_a, head_b):
    ab.append(a+b)
    tail_a.remove(a)
    head_b.remove(b)
result = result + "".join(ab) + "".join(strings) + "".join(tail_a) + "".join(head_b)
print(result.count("AB"))

Submission status: Runtime Error

Solution code:
n=input()
s1,s2=input()      
nm=0
nm1=0
cnt=s1+s2
for i in cnt:
      if i=='A' or i=='B':
              nm+=1
              nm1=max(nm1,nm)
      else:
              nm=0
print(nm1)

Submission status: Runtime Error

Solution code:
#問題C
N = int(input())
s = []

naka=0
a_kazu=0
b_kazu=0
b_a_kazu=0
for i in range(N):
    a = str(input())
    naka+=a.count("AB")
    if (a[0]=="B" and a[-1]=="A"):
        b_a_kazu+=1
    elif a[0]=="B":
        b_kazu+=1
    elif a[-1]=="A":
        a_kazu+=1
if b_a_kazu==0:
    print(naka+min(a_kazu, b_kazu))
else:
    if a_kazu==0 and b_kazu==0:
        print(naka+(b_a_kazu-1))
    elif a_lazu==0 or b_kazu==0:
        print(naka+(b_a_kazu))
    else:
        print(naka+(b_a_kazu-1)+min(a_kazu+1, b_kazu+1))

Submission status: Runtime Error

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
if max(a,b) == 0　and ba <= 1:
    print(ab)
elif max(a,b) == 0:
    print(ab+ba-1)
else:
    print(min(a,b)+ab+ba)

Submission status: Runtime Error

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

from itertools import permutations as perm

p = perm(S,N)
count_list = []
for s in p:
    if "AB" in s:
        #print(s)
        join = "".join(s)
        count_list.append(join.count("AB"))
print(max(count_list))

Submission status: Runtime Error

Solution code:

v = [int(d) for d in input().split()]
f = []

for e in range(v[0]):
    f.append(input())
re = 0
for d in f:
    re += d.count('AB')
dd = [list(s) for s in f]


b = 0
a = 0
coo= 0

for q in dd:
    if q[0] == 'B':
        b += 1
    if q[-1] == 'A':
        a += 1
        if q[0] == 'B':
            coo += 1

if a > b:
    ss = b
elif a < b:
    ss = a
elif a == b:
    ss == a

if ss == coo:
    if ss == b:
        if ss == a:
            if ss != 0:
                ss -= 1
re = re + ss
print(re)

Submission status: Runtime Error

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

from itertools import permutations as perm

p = perm(S,N)
count_list = []
for s in p:
    if s in "AB":
        #print(s)
        join = "".join(s)
        count_list.append(join.count("AB"))
print(max(count_list))

Submission status: Runtime Error

Solution code:
# N < 10000
N = int(input())

# len(s) < 10

count = 0
a, b, ba = 0, 0, 0
for i in range(N):
    s = input().rstrip()
    count += s.count("AB")

    if s[-1] == 'A':
        if s[0] == 'B':
            ba += 1
        else:
            a += 1
    elif s[0] == 'B':
        b += 1

#e = int(input())
#print("*A=",a, ", B*=",b, "B*A=",ba, ",AB=", count, ",expected=", e)

if ba > 1:
    count += ba - 1
    ba = 1

if ba == 1:
    c0 = 1 + min(a, b)
count+=max(min(a+ba,b),min(a,b+ba), c0)
print(count)

Submission status: Runtime Error

Solution code:
def f(a,b,ba):
  if a>0 and b>0:
    return 1 + f(a-1,b-1,ba)
  elif a>0 and ba>0:
    return 1 + f(a-1,b+1,ba-1)
  elif ba>0 and b>0:
    return 1 + f(a,b-1,ba-1)

  return 0

N=int(input())
a=[]
b=[]

stA=0
stB=0
stBA=0

ans=0
for z in range(N):
  a.append(input())

for z in a:
  buf=z.count("AB")
  b.append((z[0],z[-1],buf))
  ans+=buf

for z in b:
  if z[0]=="B" and z[1]=="A":
    stBA+=1
  elif z[0]=="B":
    stB+=1
  elif z[1]=="A":
    stA+=1

ans=ans+f(stA,stB,stBA)
print(ans)

Submission status: Runtime Error

Solution code:

v = [int(d) for d in input().split()]
f = []

for e in range(v[0]):
    f.append(input())
re = 0
for d in f:
    re += d.count('AB')
dd = [list(s) for s in f]


b = 0
a = 0
coo= 0

for q in dd:
    if q[0] == 'B':
        b += 1
    if q[-1] == 'A':
        a += 1
        if q[0] == 'B':
            coo += 1

if a > b:
    ss = b
elif a < b:
    ss = a
elif a == b:
    ss == a

if ss == coo:
    if ss == b:
        if ss == a:

            re -= 1
re = re + ss
print(re)

Submission status: Runtime Error

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

for i in range(N):
  str = input()
  ab_inside_str = str.count('AB')

  inside_str = inside_str + ab_inside_str
  if (str[-1] == 'A'):
    tail_a = tail_a + 1
  if (str[0] == 'B'):
    head_b = head_b + 1
  if(str[-1] == 'A' && str[0] == 'B'):
    except_Dcount = except_Dcount + 1

total_cand = inside_str + min(tail_a, head_b) - except_Dcount
print(total_cand)

Submission status: Runtime Error

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
if max(a,b) == 0　and ba <= 1:
    print(ab)
elif max(a,b) == 0:
    print((ab+ba-1))
else:
    print(min(a,b)+ab+ba)

Submission status: Runtime Error

Solution code:
n=int(input())
s=list(input() for _ in range(n))
m,i,f=0,0,0

for i in range(n):
    m+=s[i].count("AB")
    if s[i][0]=="B":
        i+=1
    if s[i][-1]=="A":
        f+=1
print(m+min(i,f))

Submission status: Runtime Error

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

cnt = 0
b = 0
a = 0
ab = 0
for x in S:
    cnt += x.count('AB')
    if x[0] == 'B':
        b += 1
    if x[-1] == 'A':
        a += 1
    if x[0] == 'B' and x[-1] == 'A':
        ab += 1

if a+b >= 2:
    if a == ab and b == ab:
        a = 1
        b = 1
        ab -= 2
    if a > b:
        cnt += a//b + ab
    else:
        cnt += b//a + ab

print(cnt)

Submission status: Runtime Error

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
if max(a,b) == 0　and ba <= 1:
    print(ab)
elif max(a,b) == 0
    print((ab+ba-1))
else:
    print(min(a,b)+ab+ba)

Submission status: Runtime Error

Solution code:
N = input()
cnt = 0
top_b = 0
bottom_a = 0

for i in range(N):
    s = input()
    if "AB" in s:
        cnt += 1
    if s[0] == "B":
        top_b += 1
    if s[-1] == "A":
        bottom_a += 1

if top_b >= bottom_a:
    cnt += bottom_a
else:
    cnt += top_b
print(cnt)

Submission status: Runtime Error

Solution code:
n=input()
while True:
       for j in range(n):
              j=input()
       
nm=0
nm1=0
cnt=j
for i in cnt:
      if i=='A' or i=='B':
              nm+=1
              nm1=max(nm1,nm)
      else:
              nm=0
print(nm1)

Submission status: Runtime Error

Solution code:
n = int(input())
strings = [input() for _ in range(n)]
ta_hb = list(filter(lambda s: s[-1] == "A" and s[0] == "B", strings))
for s in ta_hb:
    strings.remove(s)
tail_a = list(filter(lambda s: s[-1] == "A", strings))
for s in tail_a:
    strings.remove(s)
head_b = list(filter(lambda s: s[0] == "B", strings))
for s in head_b:
    strings.remove(s)
if 0 < len(tail_a) and 0 < len(head_b):
    result = tail_a.pop(0) + "".join(ta_hb) + head_b.pop(0)
elif 0 < len(tail_a):
    result = tail_a.pop(0) + "".join(ta_hb)
elif 0 < len(head_b):
    result = join(ta_hb) + head_b.pop(0)
ab = []
for a,b in zip(tail_a, head_b):
    ab.append(a+b)
    tail_a.remove(a)
    head_b.remove(b)
result = result + "".join(ab) + "".join(strings) + "".join(tail_a) + "".join(head_b)
print(result.count("AB"))

Submission status: Runtime Error

Solution code:
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: Runtime Error

Solution code:



R,G,B,N=[int(c) for c in input().split()]

C=sorted([R,G,B])




if(C==[1,1,1]):
	input()
    print(4504501)
	exit()
if(C==[1,1,2]):
	print(2253001)
	exit()






count=0
for i in range(N//C[2]+1):
	for j in range(N//C[1]+1):
		if(N-C[2]*i-C[1]*j>=0 and (N-C[2]*i-C[1]*j)%C[0]==0):
			count+=1
print(count)

Submission status: Runtime Error

Solution code:
#問題C
N = int(input())
s = []

naka=0
a_kazu=0
b_kazu=0
b_a_kazu=0
for i in range(N):
    a = str(input())
    naka+=a.count("AB")
    if (a[0]=="B" and a[-1]=="A"):
        b_a_kazu+=1
    elif a[0]=="B":
        b_kazu+=1
    elif a[-1]=="A":
        a_kazu+=1
if b_a_kazu==0:
    print(naka+min(a_kazu, b_kazu))
else:
    if a_kazu==0 and b_kazu==0:
        print(naka+(b_a_kazu-1))
    elif a_lazu==0 or b_kazu==0:
        print(naka+(b_a_kazu))
    else:
        print()
    print(naka+(b_a_kazu-1)+min(a_kazu+1, b_kazu+1))

Submission status: Runtime Error

Solution code:
n = int(input())
strings = [input() for _ in range(n)]
ta_hb = list(filter(lambda s: s[-1] == "A" and s[0] == "B", strings))
for s in ta_hb:
    strings.remove(s)
tail_a = list(filter(lambda s: s[-1] == "A", strings))
for s in tail_a:
    strings.remove(s)
head_b = list(filter(lambda s: s[0] == "B", strings))
for s in head_b:
    strings.remove(s)
if 0 < len(tail_a) and 0 < len(head_b):
    result = tail_a.pop(0) + "".join(ta_hb) + head_b.pop(0)
elif 0 < len(tail_a):
    result = tail_a.pop(0) + "".join(ta_hb)
elif 0 < len(head_b):
    result = join(ta_hb) + head_b.pop(0)
ab = []
for a,b in zip(tail_a, head_b):
    ab.append(a+b)
result = result + "".join(ab) + "".join(strings)
l = min(len(tail_a), len(head_b))
result = result + "".join(tail_a[l:]) if len(tail_a) > len(head_b) else result + "".join(head_b[l:])
print(result.count("AB"))

Submission status: Runtime Error

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:
        for i in range(len(ss)-1):
            if ss[i:i+2] == 'AB':
                AB += 1
        if ss[0] == 'B':
            if ss[-1] == 'A':
                B_A += 1
            else:
                B_ += 1
        elif ss[-1] == 'A':
            _A += 1
    if _A == B_ && _A > 1:
        print(AB + max((B_A - 1), 0) + max(min(B_ + int(B_A > 0), _A), min(B_, _A + int(B_A > 0))) + 1)
    else:
        print(AB + max((B_A - 1), 0) + max(min(B_ + int(B_A > 0), _A), min(B_, _A + int(B_A > 0))))
    # print(AB, B_, _A, B_A)

if __name__ == '__main__':
    main()