Problem:


Score : 400 points

Problem Statement

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

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

Constraints

1 \leq N \leq 10^{4}

2 \leq |s_i| \leq 10

s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

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

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4



Sample input:
3
ABCA
XBAZ
BAD


Sample output:
2

Submission status: Wrong Answer

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

S = [input() for _ in range(int(input()))]

AB = 0
end_A = 0
start_B = 0
sB_eA = 0

for s in S:
    AB += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        sB_eA += 1
    elif s[0] == 'B':
        start_B += 1
    elif s[-1] == 'A':
        end_A += 1

ans = AB
   
minab = min(start_B,end_A,sB_eA)
start_B -= minab
end_A -= minab
sB_eA -= minab
ans += minab*2

if sB_eA == 0:
    ans += min(start_B,end_A)
else:
    ans += sB_eA
print(ans)

Submission status: Wrong Answer

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 val3 == 0 and val4 % 2 == 1:
  val4 -= 1
print(val1+val2+val3+val4)

Submission status: Wrong Answer

Solution code:
N  = int(input())
ans = 0
acount1 = 0
bcount1 = 0
acount2 = 0
bcount2 = 0
comn1 = 0
comn2 = 0
for i in range(N):
    tmp = input()
    ans += tmp.count('AB')
    if tmp[0] == 'B' and tmp[len(tmp) - 1] == 'A':
        comn1 = 1
    if tmp[0] == 'B':
        bcount1 += 1
    if tmp[len(tmp) - 1] == 'A':
        acount1 += 1

    if tmp[0] == 'A' and tmp[1] != 'B' and tmp[len(tmp) - 1] == 'B' and tmp[len(tmp) - 2 != 'A']:
        comn2 = 1
    if tmp[0] == 'A' and tmp[1] !='B':
        acount2 += 1
    if tmp[len(tmp) - 1] == 'B' and tmp[len(tmp) - 2] != 'A':
        bcount2 += 1

tmp1 = max(acount1, bcount1) - comn1
tmp11 = min(tmp1, min(acount1, bcount1))
tmp2 = max(acount2, bcount2) - comn2
tmp22 = min(tmp2, min(acount2, bcount2))

ans += max(tmp11, tmp22)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
f1, f2, f3 = 0, 0, 0
ans = 0
for i in range(n):
    t = input(); ans += t.count("AB")
    if t[0] == "B" and t[-1] != "A":
        f1 += 1
    if t[0] != "B" and t[-1] == "A":
        f2 += 1
    if t[0] == "B" and t[-1] == "A":
        f3 += 1
# f1: B....X
# f2: X....A
# f3: B....A

t = min(f1, f2); ans += t
f1 -= t; f2 -= t;

if f1 == 0:
    if f2 == 0:
        ans += max(0, f3-1)
    else:
        tt = min(f2, f3)
        ans += tt
        f2 -= tt; f3 -= tt;
        ans += f3
else:
    if f1 == 0:
        ans += max(0, f3-1)
    else:
        tt = min(f1, f3)
        ans += tt
        f1 -= tt; f3 -= tt
        ans += max(0, f3-1)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

n_fin_is_A = 0
n_ini_is_B = 0
n_each_ab = 0

for n in range(N):
    S = input()
    n_each_ab += S.count('AB')
    n_fin_is_A += int(S[-1]=='A')
    n_ini_is_B += int(S[0]=='B')

print(n_each_ab + min(n_fin_is_A,n_ini_is_B))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
count=countA=countB=countAB=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
    if s[-1]=="A" and s[0]=="B":countAB+=1
print(count+min(countA,countB) if countAB!=0 else count+max(countA,countB)-countAB)

Submission status: Wrong Answer

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

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

for i in range(n):
    if s[i][0]=='B':
        if mojiretuab[-1]=='A':
            mojiretuab=mojiretuab+s[i]
        else:
            mojiretuab=s[i]+mojiretuab
    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: Wrong Answer

Solution code:
N = int(input())
A = 0
B = 0
ans = 0
case = 0
for i in range(N):
  S = input()
  for j in range(len(S)-1):
    if S[j] == 'A' and S[j+1] == 'B':
      ans += 1
  if S[0] == 'B' and S[len(S)-1] == 'A':
    case += 1
  else:
    if S[0] == 'B':
      B += 1
    if S[len(S)-1] == 'A':
      A += 1
if case == 0:
  print(ans+min(A,B))
else:
  if A > 0 and B > 0:
    print(ans+case+1+min(A-1,B-1))
  else:
    print(ans+case-1)

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] and "B" not in input[i][0]:
        count_A+=1
        #continue
    if "B" in input[i][0] and "A" not in input[i][len(input[i])-1]:
        count_B+=1
    if "A" in input[i][len(input[i])-1] and "B" in input[i][0]:
        count_AB+=1

#print(count_A,count_B,count_AB)

if count_AB!=0:
    if count_A!=0 or count_B!=0:
        if count_A==count_B:
            print(output+count_A+count_AB-1)
        else:
            if count_A<count_B:
                #print(output+count_A)
                print(output+count_A+count_AB)
            else:
                print(output+count_B+count_AB)
    else:
        print(output)
else:
    if count_A<count_B:
        print(output+count_A)
        #print(output+count_A+count_AB)
    else:
        print(output+count_B)

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
    both-=index
    if (both>0):
        count+=both
elif(B_list==0):
    index=min(A_list,both)
    count+=index
    both-=index
    if (both>0):
        count+=both
elif(both==0):
    index=min(B_list,A_list)
    count+=index
print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())

ans=0
ba=0
bstart=0
aend=0
for i in range(n):
  s=input()
  for j in range(len(s)):
    if s[j]=="A":
      if not j+1==len(s):
        if s[j+1]=="B":
          ans+=1
  
  if s[0]=="B" and s[-1]=="A":
    ba+=1
  else:
    if s[0]=="B":
      bstart+=1
    if s[-1]=="A":
      aend+=1
      
print(ans,ba,bstart,aend)
      
if ba==0:
  print(ans+min(bstart,aend))
else:
  if bstart>=1 and aend>=1:
    print(ans+ba-1+2+min(bstart-1,aend-1))
  else:
    print(ans+ba-1+max(min(1,bstart),min(1,aend)))

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
n = int(input())
s_list = []
a_end_num = 0
b_start_num = 0
cnt = 0
for _ in range(n):
    s_in = input()[:-1]
    s_len = len(s_in)
    if s_len == 1:
        if s_in[0] == 'A':
            a_end_num += 1
        elif s_in[0] == 'B':
            b_start_num += 1
    else:
        flag = 0
        for j in range(s_len):
            if s_in[j] == 'A':
                if j == s_len-1:
                    a_end_num += 1
                flag = 1
            elif s_in[j] == 'B':
                if j == 0:
                    b_start_num += 1
                if flag == 1:
                    cnt += 1
                flag = 0
            else:
                flag = 0
print(cnt+min(a_end_num,b_start_num))

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':
    first_b+=1
  if s[-1] == 'A':
    last_a+=1
  if s[0] == 'B' and s[-1] == 'A':
    both_ab+=1
if n == 1:
  print(ans)
elif last_a == first_b and last_a > 0 and both_ab > 0:
  print(ans+last_a-1)
else:
  print(ans+min(last_a,first_b))

Submission status: Wrong Answer

Solution code:
import re
aflag = 0
bflag = 0
cnt = 0
N = int(input())
for i in range(N):
    s = input()
    if s[-1:] == "A":
        aflag += 1
    if s[:1] == "B":
        bflag += 1
    if re.search("AB", s):
        cnt += 1
    if re.search("AB.*AB", s):
        cnt += 1
    if re.search("AB.*AB.*AB", s):
        cnt += 1
    if re.search("AB.*AB.*AB.*AB", s):
        cnt += 1
    if re.search("AB.*AB.*AB.*AB.*AB", s):
        cnt += 1

cnt += min(aflag, bflag)
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
eni_B = 0
end_A = 0
both = 0
count = 0

for i in range(N):
    s = list(input())
    if s[0] == 'B':
        eni_B += 1
    if s[-1] == 'A':
        end_A += 1
    if s[0] == 'B' and s[-1] == 'A':
        eni_B -= 1
        end_A -= 1
        both += 1
    for j in range(len(s)-1):
        if s[j] == 'A':
            if s[j+1] == 'B':
                count += 1

m = min(end_A,eni_B)
M = max(end_A,eni_B)

if M == 0 and m == 0:
    count = count + both - 1
elif both == 0:
    count = count + m
else:
    count = count + both + m 

print(count)

Submission status: Wrong Answer

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

ba = 0
abc = 0
aend = 0
bbeg = 0
for s in S:
	if "AB" in s:
		abc += s.count("AB")
	if "A" == s[-1] and "B" == s[0]:
		ba += 1
	elif "A" == s[-1]:
		aend += 1
	elif "B" == s[0]:
		bbeg += 1

if (aend + bbeg + 1) < ba:
	ba -= 1

#print(abc, aend, bbeg, ba)
print(abc + min(aend + ba, bbeg + ba))

Submission status: Wrong Answer

Solution code:
N = int(input())
res=0
first, end, both = 0, 0, 0


for i in range(N):
    str = input()
    res+=str.count("AB")
    if str[0]=="B":
        first+=1
    if str[-1]=="A":
        end+=1
    if str[0]=="B" and str[-1]=="A":
        both+=1

res+=min(first, end)
if both==max(first, end):
    res-=1

print(res)

Submission status: Wrong Answer

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

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

cnt = 0
both_ab = 0
last_a = 0
initial_b = 0
for i in range(N):
    if s[i].count('AB') > 0:
        cnt += s[i].count('AB')

    if s[i][-1] == 'A' and s[i][0] == 'B':
        both_ab += 1
    elif s[i][-1] == 'A':
        last_a += 1
    elif s[i][0] == 'B':
        initial_b += 1


def calc(cnt, both_ab, last_a, initial_b):
    if both_ab == 0:
        min_a_b = min(initial_b, last_a)
        cnt += min_a_b
        return cnt

    if min(both_ab, last_a) > min(both_ab, initial_b):
        cnt += min(both_ab, initial_b)
        both_ab -= min(both_ab, initial_b)
    else:
        cnt += min(both_ab, last_a)
        both_ab -= min(both_ab, last_a)

    min_a_b = min(initial_b, last_a)
    cnt += min_a_b

    return both_ab + cnt


ans = calc(cnt, both_ab, last_a, initial_b)
print(ans)

Submission status: Wrong Answer

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


if __name__ == '__main__':
	main()

Submission status: Wrong Answer

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

if a>0 and b>0 and ab>0:
    print(count + ab + min(a,b))
elif ab == 0:
    print(count + min(a,b))
else:
    print(count + ab)

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
all_ba=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 s[0]=="B" and s[-1]=="A":
        all_ba+=1
if last_a==first_b and min(last_a,first_b) == all_ba:
    print(base_ab+min(last_a,first_b)-1)
else:    
    print(base_ab+min(last_a,first_b))

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
bx = 0
xa = 0
ba = 0

for i in range(N):
    S = input()
    ans += S.count('AB')
    if S[0] == 'B' and S[-1] == 'A':
        ba += 1
    elif S[0] == 'B':
        bx += 1
    elif S[-1] == 'A':
        xa += 1
while bx >= 1 and xa >= 1:
    bx -= 1
    xa -= 1
    ans += 1
while ba >= 2:
    ba -= 1
    ans += 1
ans += min(bx + xa, ba)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[0]*n
for i in range(n):
    s[i]=input()
sab=0
cntAB=0
cntA=0
cntB=0
for i in range(n):
    if s[i][0]=="B":
        c1=1
    else:
        c1=0
    if s[i][len(s[i])-1]=="A":
        c2=1
    else:
        c2=0
    if c1==1 and c2==1:
        cntAB+=1
    elif c1==1:
        cntA+=1
    elif c2==1:
        cntB+=1
    for j in range(len(s[i])-1):
        if s[i][j]=="A" and s[i][j+1]=="B":
            sab+=1
if cntAB==n:
    cntAB-=1
print(sab+cntAB+min(cntA,cntB))

Submission status: Wrong Answer

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

count=0
count_a=0
count_b=0
count_ab=0
for i in range(n):
  if(s[i][0] == 'B' and s[i][-1:] == 'A'):
    count_ab+=1
  elif(s[i][0] == 'B'):
    count_b+=1
  elif(s[i][-1:] == 'A'):
    count_a+=1

  flag=0
  for j in range(len(s[i])):
    if(flag==0 and s[i][j] == 'A'):
      flag=1
      continue
    if(flag==1 and s[i][j] == 'B'):
      count+=1
      flag=0
      continue
    if(flag==1 and s[i][j] != 'A'):
      flag=0

if(max([count_a,count_b,count_ab]) == count_ab):
  m=min([count_a,count_b])
  count+=m*2
  count_a-=m
  count_b-=m
  count_ab-=m

  count+=count_a+count_b
  count_ab-=count_a+count_b

  count+=count_ab
  if(count_a == count_b and count_a != count_ab):
    count-=1
elif(min([count_a,count_b,count_ab]) == count_ab):
  count+=count_ab*2
  count_a-=count_ab
  count_b-=count_ab
  count_ab=0

  count+=min([count_a,count_b])
else:
  m=min([count_a,count_b])
  count+=m*2

print(count)

Submission status: Wrong Answer

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

a = 0
b = 0
ba = 0
for i in range(N):
  str = input()
  count += str.count('AB')
  if str[:1] == "B":
    if str[-1:] == "A":
      ba += 1
    else:
      b +=1
  elif str[-1:] == "A":
    a += 1

if a != 0 and b != 0:
  a_b_min = min(a, b)
  a_b_max = max(a, b)
  a_b_diff = a_b_max - a_b_min
  count += a_b_min
  if a == b:
    count += ba
    count -= 1
  else:
    count += ba
elif ba < max(a, b):
  count += ba
elif ba > max(a, b):
  count += ba
  
print(count)

Submission status: Wrong Answer

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

c = 0
for st in s:
    i = 0
    while i < len(st):
        if st[i] != 'A' and st[i] != 'B':
            i += 2
            continue
        if i < len(st)-1 and st[i:i+2] == 'AB':
            c += 1
        elif i > 0 and st[i-1:i+1] == 'AB':
            c += 1
        i += 2

a = list(map(lambda x: x[-1] == 'A', s))
numa = len(list(filter(lambda x: x, a)))
b = list(map(lambda x: x[0] == 'B', s))
numb = len(list(filter(lambda x: x, b)))

head = False
tail = False
els = 0
for i in range(n):
    if a[i] and not b[i]:
        head = True
    elif not a[i] and b[i]:
        tail = True

if not head:
    numb -= 1
if not tail:
    numa -= 1

con = min(numa, numb)


print(c+con)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[0]*n
ct=0
a=0
b=0
c=0
for i in range(n):
    s[i]=input()
for i in range(n):
    for j in range(len(s[i])):
        t=s[i]
        ts=list(t)
        ts.append(0)
        if ts[j]=='A' and ts[j+1]=='B':
            ct+=1
    if ts[0]=='B' and ts[len(s[i])-1]!='A':
        a+=1
    if ts[len(s[i])-1]=='A' and ts[0]!='B':
        b+=1
    if ts[0]=='B' and ts[len(s[i])-1]=='A':
        c+=1
print(ct+min(a,b)+c)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
N = int(input())
counter = 0
master_parts = 0
a_parts = 0
b_parts = 0
for i in range(N):
    temp = input()
    counter += temp.count('AB')
    if temp[-1] == 'A' and temp[0] == 'B':
        master_parts += 1
    elif temp[-1] == 'A':
        a_parts += 1
    elif temp[0] == 'B':
        b_parts += 1
if a_parts < b_parts:
    if not master_parts + a_parts == 1:
        counter += (master_parts + a_parts)/2 + 1
    else:
        counter += 1
elif b_parts < a_parts:
    if not master_parts + b_parts == 1:
        counter += (master_parts + b_parts) / 2 + 1
    else:
        counter += 1
else:
    if master_parts and a_parts:
        counter += (master_parts + a_parts) / 2
    else:
        counter += a_parts
print(math.ceil(counter))

Submission status: Wrong Answer

Solution code:
N = int(input())

AB_cnt = 0
A_cnt = 0
B_cnt = 0
for i in range(N):
    S = input()
    for j in range(len(S)-1):
        if S[j:j+2] == 'AB':
            AB_cnt += 1
    if S[-1] == 'A':
        A_cnt += 1
    if S[0] == 'B':
        B_cnt += 1

print(AB_cnt + min(A_cnt, B_cnt))

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':
    first_b+=1
  if s[-1] == 'A':
    last_a+=1
  if s[0] == 'B' and s[-1] == 'A':
    both_ab+=1
if last_a == first_b and last_a > 0 and n > 1 and both_ab > 0:
	print(ans+last_a-1)
else:
	print(ans+min(last_a,first_b))

Submission status: Wrong Answer

Solution code:
N = int(input())

counter = 0
S = []
for s in range(N):
  s = input()
  S.append(s)

a_counter = 0
b_counter = 0
  
for s in S:
  counter += s.count('AB')
  if s[0] =='B':
    b_counter += 1
  if s[-1] == 'A':
    a_counter += 1

counter += min(a_counter, b_counter)
 
print(counter)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=0
B=0
A=0
for i in range(N):
    S=input()
    a+=S.count("AB")
    if S[0]=="B":
        B+=1
    if S[len(S)-1]=="A":
        A+=1
C=min(A,B)
D=min(C,N-1)
print(a+D)

Submission status: Wrong Answer

Solution code:
N = int(input())
list1 = []
count1 = 0
taila = 0
headb = 0
same1 = 0
for i in range(N):
    check1 = 0
    list1.append(input())
    if list1[i][0] == "B":
         headb +=1
    if list1[i][len(list1[i])-1] == "A":
         taila +=1
    if (list1[i][len(list1[i])-1] == "A" and list1[i][0] == "B"):
        same1 +=1  
    for j in list1[i]:
        if j == "A":
            check1 = 1
        if (check1 ==1 and j =="B"):
            count1 += 1
            check1 = 0
        if (j != "A" and j != "B"):
            check1 = 0
if max(headb,taila) > same1:
    p1 = min(headb,taila)
else:
    p1 = same1 -1

print(count1+p1)

Submission status: Wrong Answer

Solution code:
n=int(input())

#normal, start_b, end_a, both

kosu=0
tokushu=[0,0,0,0]

for i in range(n):
    line=input()
    switch=0
    for l in range(len(line)):
        if switch==1:
            if line[l]=='B':
                kosu+=1
            else:
                switch=0
        if line[l]=='A':
            switch=1

    if line[0]=='B':
        if line[-1]=='A':
            tokushu[3]+=1
        else:
            tokushu[1]+=1
    else:
        if line[-1]=='A':
            tokushu[2]+=1
        else:
            tokushu[0]+=1

#print(kosu)
#print(tokushu)
"""
if tokushu[3]>=1:
    mazu=tokushu[3]-1

else:
    mazu=0
"""
if tokushu[3]>=1:
    mazu=tokushu[3]-1
    sukunai=min(tokushu[1],tokushu[2])
    mazu+=(sukunai+1)
else:
    mazu=min(tokushu[1], tokushu[2])-1
    

print(kosu+mazu)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    s = [input() for i in range(N)]
    Acnt = 0
    Bcnt = 0
    ABcnt = 0
    for i in range(N):
        if s[i][-1] == 'A':
            Acnt += 1
        if s[i][0] == 'B':
            Bcnt += 1
        index = -2
        while True:
            index = s[i].find('AB', index + 2)
            if index == -1:
                break
            ABcnt += 1
    if Acnt == N:
        Acnt -= 1
    if Bcnt == N:
        Bcnt -= 1
    print(ABcnt + min(Acnt, Bcnt))


if __name__ == '__main__':
    main()

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
if a_end == b_start == a_and_b:
    print(cnt+min(a_end, b_start)-1)
else:
    print(cnt+min(a_end, b_start))

Submission status: Wrong Answer

Solution code:
N=int(input())
result=0
fb=0# first is b
la=0# last is a
lafb=0
for i in range(N):
    s=input()
    result+=s.count('AB')
    if s[0]=="B" and s[-1]!="A":
        fb+=1
    elif s[-1]=="A" and s[0]!="B":
        la+=1
    elif s[-1]=="A" and s[0]=="B":
        lafb+=1


k=-1
mn=min(la,fb)-1
if mn==-1:
    mn+=1
    if la>0 or fb>0:
        k+=1
else:
    k+=2
result+=mn+lafb+k
print(result)

Submission status: Wrong Answer

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

ct_a=0
ct_b=0
ct_ab=0
ct_ba=0

for i in range(N):
    if s[i][0]=='B' and s[i][-1]=='A':
        ct_ba+=1
    elif s[i][0]=='B':
        ct_b+=1
    elif s[i][-1]=='A':
        ct_a+=1
    for j in range(len(s[i])-1):
        if s[i][j]=='A' and s[i][j+1]=='B':
            ct_ab+=1

if ct_a!=ct_b:
    ct_a+=ct_ba
    ct_b+=ct_ba
else:
    ct_a+=ct_ba-1
    ct_b+=ct_ba-1
print(ct_ab+min(ct_a,ct_b))

Submission status: Wrong Answer

Solution code:
n = int(input())

num___A = 0
num_B__ = 0
num_B_A = 0 
cnt = 0

for _ in range(n):
  word = input()
  cnt += word.count("AB")
  if word[-1] =="A" and word[0]=="B":
    num_B_A += 1
  elif word[-1] == "A":
    num___A += 1
  elif word[0] == "B":
    num_B__ += 1

num_triple = min(num___A, num_B__ , num_B_A - 1) # __B A__B A__ になる形の数

cnt += num_triple  * 2

num___A -= num_triple
num_B__ -= num_triple
num_B_A -= num_triple

if num_B_A != 0:
  cnt += num_B_A
else:
  cnt += min(num___A, num_B__ )

print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
n_a = 0
n_b = 0
n = 0
for i in range(0, N):
    s = input()
    if s[0] == 'B': 
        n_b += 1
    if s[-1] == 'A': 
        n_a += 1
    n += s.count('AB')

print(n+min(n_a, n_b))

Submission status: Wrong Answer

Solution code:
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N = i2n()
    s = [''] * N
    mAB = 0
    hB = 0
    tA = 0
    for i in range(N):
        s[i] = i2s()
        if 'AB' in s[i]:
            mAB += 1
        if s[i][0] == 'B':
            hB += 1
        if s[i][-1] == 'A':
            tA += 1
    print(mAB + min(hB, tA))

main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8

import sys
input = sys.stdin.readline

N = int(input())
# L = [int(x) for x in input().split()]
# N, K = map(int, input().split())

AB  = 0
xA = 0
BxA = 0
Bx = 0

for _ in range(N) :
    S = input()

    AB += S.count("AB")

    if S[0] + S[-2] == "BA" :
        BxA += 1
    elif S[-2] == 'A' :
        xA += 1
    elif S[0] == 'B' :
        Bx += 1

if BxA == 0 :
    print(AB + min(xA, Bx))
elif BxA == 1 and xA <= 1 and Bx <= 1 :
    print(AB + xA + Bx)
elif BxA == 1 and (xA == 0 or Bx == 0) :
    print(AB + 1)
elif BxA == 1 :
    print(AB + min(xA - 1, Bx - 1) + 2)
elif BxA == 1 and xA <= 1 and Bx <= 1 :
    print(AB + xA + Bx + (BxA - 1))
elif BxA == 1 and (xA == 0 or Bx == 0) :
    print(AB + BxA)
else :
    print(AB + min(xA - 1, Bx - 1) + 2 + (BxA - 1))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
S = []
for i in range(n):
    s = str(input())
    S.append(s)
    
ans = 0
countA = 0
countB = 0
countBA = 0
for s in S:
    if s[0] == 'B' and s[len(s)-1] == 'A':
        countBA += 1
        
    elif s[len(s)-1] == 'A':
        countA += 1
        
    elif s[0] == 'B':
        countB += 1
        
    for i in range(len(s) - 1):
        if s[i] == 'A' and s[i+1] == 'B':
            ans += 1
# print(countA, countB, countBA)
# print(ans)

ans = min(countA, countB) + countBA 
print(ans)

Submission status: Wrong Answer

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

if AB > 0 and A == B:
    ans -= 1

print(ans)

Submission status: Wrong Answer

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

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

a = 0
b = 0
ab = 0
ans = 0
all_tail_b_a = True
for i in range(N):
    s = L[i]
    ab = s.count("AB")
    ans += ab

    if s[0] is "B":
        b += 1
    if s[-1] is "A":
        a += 1

    if s[0] is "B" and s[-1] is "A":
        pass
    else:
        all_tail_b_a = False

ans += min(a, b)
if all_tail_b_a is True:
    ans -= 1

print(ans)

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
    c_need_a_and_b = max(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 < -1:
        c_need_a_and_b = 1
    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_head_has_b, c_tail_has_a) - c_need_a_and_b + 1
        count_combi_a_b = min(c_head_has_b, c_tail_has_a) - c_need_a_and_b
        #count_combi_a_b = max(c_head_has_b, c_tail_has_a) - c_head_tail_has_a_and_b + 1
    else:
        count_combi_a_b = min(c_head_has_b, c_tail_has_a)
        #count_combi_a_b = max(c_head_has_b, c_tail_has_a)
    _sum += count_combi_a_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 = [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
elif ab > a + b:
  count += ab-1 + a + b
else:
  count += ab + min(a, b)
print(count)

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
    if s[0] == "B":
        startb += 1
    if s[-1] == "A":
        enda += 1


startb = startb-both
enda = enda-both

# print(count)
# print(both)
# print(enda)
# print(startb)

mi = min(startb,enda)
ma = max(startb,enda)

ans = count + mi
# print(ans)
left = ma-mi
# print(left)
if both > left:
    ans += both
elif both == left:
    ans += both
else:
    ans += both

ans2 = count

if both > mi:
    ans2 += mi*2
    ans2 += both-mi;

print(max(ans,ans2))