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:
#https://atcoder.jp/contests/diverta2019/tasks/diverta2019_a
# -*- coding: utf-8 -*-

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

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

for i in s:
        if "AB" in i:
                cnt += 1
        if i[len(i)-1] == "A" and i[0] != "B":
                cntA += 1
        if i[len(i)-1] != "A" and i[0] == "B":
                cntB += 1
        if i[len(i)-1] == "A" and i[0] == "B":
                cntAB += 1

ans = cnt
ans += min(cntA,cntB)
ans += min( ( max(cntA,cntB) - min(cntA,cntB) ), cntAB)
if cntAB > ( max(cntA,cntB) - min(cntA,cntB) ):
        ans+= (cntAB -  max(cntA,cntB) - min(cntA,cntB) ) // 2

print(ans)

Submission status: Wrong Answer

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


total = 0
a = 0
b = 0
ba = 0

for _s in s:
    total += _s.count('AB')
    if _s[-1] == 'A':
        a += 1
    if _s[0] == 'B':
        b += 1
    if _s[-1] == 'A' and _s[0] == 'B':
        ba += 1


if a < b:
    ba -= (b - a)
    b = a
else:
    ba -= (a - b)
    a = b

if ba % 2 == 0:
    diff = ba // 2
    a -= diff
    b -= diff
else:
    diff = ba // 2
    a -= diff
    b -= diff
    if a < b:
        b -= 1
    else:
        a -= 1

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

Submission status: Wrong Answer

Solution code:
n=int(input())
f=0
b=0
c=0
d=0
a=0
k=0
for i in range(n):
    s=input()
    if "AB" in s:
        c+=s.count("AB")
    if s[0]=="B":
        b+=1
    if s[len(s)-1]=="A":
        f+=1
    if s[0]=="B" and s[len(s)-1]=="A":
        d+=1
    if s[0]=="B" and s[len(s)-1]!="A":
        a+=1
    if s[len(s)-1]=="A" and s[0]!="B":
        k+=1
e=0
if a==0 and k==0:
    e=1
    print(c+min(f,b)-e)

Submission status: Wrong Answer

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: Wrong Answer

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

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

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

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

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

print(num + default)

Submission status: Wrong Answer

Solution code:
N=int(input())
s=[input() for _ in range(N)]
def checkAB(si):
  cnt=0
  for i in range(len(si)-1):
    if si[i]=='A' and si[i+1]=='B':cnt+=1
  return cnt

ABin=sum([checkAB(si) for si in s])
Aend=sum([si[len(si)-1]=='A' for si in s])
Bstart=sum([si[0]=='B' for si in s])
BstrA=sum([si[0]=='B' and si[len(si)-1]=='A' for si in s])
#print(ABin,Aend,Bstart)
if BstrA==Aend and BstrA==Bstart:
  ans=ABin+BstrA-1
else:
  ans=ABin+min(N-1,Aend,Bstart)
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())

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

for i in range(n):
  word = input()
  cnt += 1 if "AB" in word else 0
  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

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

print(cnt)

Submission status: Wrong Answer

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' and str[0] == 'B'):
    except_Dcount = except_Dcount + 1

if(tail_a == head_b):
	total_cand = inside_str + min(tail_a, head_b) - 1
else:
  	total_cand = inside_str + min(tail_a, head_b)

print(total_cand)

Submission status: Wrong Answer

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 > 0 and b > 0 and 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: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import re
n = int(input())
s = []
compiled = re.compile(r"AB")

pre = 0
xa=0
bx=0
ba=0
for i in range(n):
    one = input()
    pre+=len(compiled.findall(one))
    f=one[0]
    l=one[-1]


    if f=="B" and l=="A":
        ba+=1
    elif f=="B" and l!="A":
        bx+=1
    elif f!="B" and l=="A":
        xa+=1
    
            

print(xa,bx,ba,pre)
cnt=ba-1
if xa>=1:
    xa-=1
    cnt+1
    
if bx>=1:
    bx-=1
    cnt+1

cnt += xa if xa>bx else bx
print(cnt+pre)

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_A_B = 0
for i in range(n):
    if "A" in s[i][-1]:
        count_A += 1
    if "B" in s[i][0]:
        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


print(min(count_A, count_B)+count)

Submission status: Wrong Answer

Solution code:
N = int(input())
s_list = []
count = 0
n, m, l = 0, 0, 0

for i in range(N):
  s = input()
  s_list.append(s)
  count += s.count('AB')
  if s[0] == 'B' and s[-1] == 'A':
    n += 1
    continue
  elif s[-1] == 'A':
    m += 1
  elif s[0] == 'B':
    l += 1

if n < m and n < l:
  print(int(count + 2 * n + min(m - n, l - n)))
else:
  print(int(count + 2 * n))

Submission status: Wrong Answer

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

count_ab = sum([s.count('AB') for s in S])
count_top_b = len([1 for s in S if s[0] == 'B'])
count_last_a = len([1 for s in S if s[-1] == 'A'])
count_ba = len([1 for s in S if s[-1] == 'A' and s[0] == 'B'])

if count_ba > 0:
    len_ba_chain = count_ba + min(1, count_top_b - count_ba) + min(1, count_last_a - count_ba)
    result = count_ab + len_ba_chain + min(count_top_b - count_ba - 1, count_last_a - count_ba - 1, 0)
else:
    result = count_ab + min(count_top_b, count_last_a, n - 1)
    
print(result)

Submission status: Wrong Answer

Solution code:
import re

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

tmp = 0

B_A = 0
B_any = 0
any_A = 0

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

add = 0

add1 = min(B_any,any_A)

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

if B_any != any_A:
    if B_A >= 1:
        add1 = add1 + 1

print(tmp+add+add1)

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
print(ans+min(min(last_a,first_b),max(last_a,first_b)-both_ab))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[]
edges=[]
left=0
left=0
right=0
right_b=0
num_ab=0

for i in range(N):
    item=input()
    left=0
    right=0
    for i in range(len(item)-1): 
        if item[i]=='A' and item[i+1]=='B':
            num_ab+=1    
    if item[0]=='A':
        left=1
    elif item[0]=='B':
        left=2
    if item[-1]=='A':
        right=1
    elif item[-1]=='B':
        right=2            
    if left>0 or right >0:
        edges.append([left,right])            
    A.append(item)
    
comb=0
for a,i in enumerate(edges):
    if i[0]==1:
        for b,j in enumerate(edges):
            if a!=b and j[1]==2:
                comb+=1
                i[0]=0
                j[1]=0
    elif i[0]==2:
        for b,j in enumerate(edges):
            if a!=b and j[1]==1:
                comb+=1
                i[0]=0
                j[1]=0                
                
ans=num_ab+comb
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
string_list=[input() for i in range(n)]
new_list = {}
target_chr = "AB"
count = 0
left_b_count = 0
right_a_count = 0
for string in string_list:
    if string.count(target_chr) > 0 and (string[0] != "B" or string[-1] != "A"):
        count += string.count(target_chr)
        continue
    if string[0] == "B":
        left_b_count +=1
        count += string.count(target_chr)
        continue
    if string[-1] == "A":
        right_a_count += 1
        count += string.count(target_chr)
        continue

if left_b_count > right_a_count:
    print(count + right_a_count + 1)
else:
    print(count + left_b_count)

Submission status: Wrong Answer

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

x = ','.join(S).count('AB')

A = [ s[-1] == "A" for s in S ].count(True)
B = [ s[0] == "B" for s in S ].count(True)
AandB = [ s[-1] == "A" and s[0] == "B" for s in S ].count(True)
for i in range(AandB):
  if A < B:
    A+=1
  elif A > B:
    B+=1
  else:
    A+=1
    B+=1
y = min( A, B ) - AandB

print(x + y)

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' and s[0] != 'B':
        A += 1 
    if s[0] == 'B' and s[-1] != 'A':
        B += 1
    if s[-1] == 'A' and s[0] == 'B':
        AB += 1

if AB > 0:
    ans += AB - 1
    ans += min(A, B)
    rest_A_B = max(A, B) - min(A, B)
    ans += min(1, rest_A_B)
else:
    ans += min(A, B)
    
print(ans)

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)

if countA > countB:
    ans += countB
    countA -= countB
    
    if countA >= countBA:
        ans += countBA
        
    else:
        ans += countA
        countBA -= countA
        ans += countBA // 2
        ans += 1
        
else:
    ans += countA
    countB -= countA
    
    if countB >= countBA:
        ans += countBA
        
    else:
        ans += countB
        countBA -= countB
        ans += countBA // 2
        
print(ans)

Submission status: Wrong Answer

Solution code:
import sys 
import re

input()
bsn = 0
aen = 0
cen = 0
t = 0
for line in sys.stdin:
    st = line.strip()
    t += st.count("AB")
    if st[0] == "B" and st[-1] == "A":
        cen += 1
    elif st[0] == "B":
        bsn += 1
    elif st[-1] == "A":
        aen += 1

if aen != 0:
    if bsn != 0:
        print(t+cen+1+min(bsn-1, aen-1))
    else:
        print(t+cen)
else:
    if bsn != 0:
        print(t+cen)
    else:
        print(t+cen-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
s_list = list()
counter = 0
a_counter = 0
b_counter = 0
ab_counter = 0

for i in range(n):
    s_list.append(input())
    
for s in s_list:
    counter += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        ab_counter += 1
    else:
        if s[0] == "B":
            b_counter += 1
        if s[-1] == "A":
            a_counter += 1
    
        
if a_counter >=1 and b_counter >= 1:
    a_counter += ab_counter
    b_counter += ab_counter
    count = min(a_counter,b_counter)
elif a_counter >=1 or b_counter >= 1:
    count = ab_counter
else:
    count = 0


counter += count

print(counter)

Submission status: Wrong Answer

Solution code:
ans=0
a_end=0
b_start=0
flag_c=0

N=int(input())
for i in range(N):
    flag=0
    s=input()
    ans+=s.count("AB")
    if s[-1]=="A":
        a_end+=1
        flag+=1
    if s[0]=="B":
        b_start+=1
        flag+=1
        
    if flag==2:
        flag_c+=1

a_end-=flag_c
b_start-=flag_c

c=0
for i in range(flag_c):
    if min(a_end,b_start)>0:
        a_end-=1
        b_start-=1
        c+=1

ans+=2*c
ans+=min(a_end,b_start)
print(ans)

Submission status: Wrong Answer

Solution code:
a=input()
a=int(a)
ll=[]
count=0
pcount=0
ll=[]
ap=0
bp=0
abc=0
for i in range(a):
    app=0
    bpp=0
    switch=0
    b=input()
    ofset=0
    for i in b:
        if i=="B" and ofset==0:
            bp+=1
            bpp+=1
        elif i=="A":
            switch+=1
            ofset+=1
            continue
        elif i=="B" and switch==1:
            count+=1
            switch=0
        else:
            switch=0
        ofset+=1
    if switch==1:
        ap+=1
        app=1
    if app==1 and bpp==1:
        abc+=1
        ap-=1
        bp-=1
if ap>bp:
    count+=bp
    bp=0
    ap-=bp
else:
    count+=ap
    ap=0
    bp-=ap
if abc>0:
    count+=abc-1
    if ap>0 or bp>0:
        count+=1
print(count)

Submission status: Wrong Answer

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 xa!=0 and xa!=bx:
  ans += 1

print(ans)

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 < 1 and xA > 0 and Bx > 0:
    print(AB + min(xA, Bx))
elif BxA < 1:
    print(AB)
elif xA > 0 and Bx > 0 :
    print(AB + (BxA - 1) + 2 + min(xA - 1, Bx - 1))
else :
    print(AB + (BxA - 1) + 1)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[input() for __ in range(n)]
a_ed=[a for a in s if a.endswith('A') and not a.startswith('B')]
b_st=[b for b in s if b.startswith('B') and not b.endswith('A')]
a_b=[anb for anb in s if anb.endswith('A') and anb.startswith('B')]
ab_in=[ab for ab in s if ('AB') in ab]

if len(a_ed)==0 and len(b_st)==0:
    a_b_num=max(0,len(a_b)-1)
else:
    a_b_num=len(a_b)

print(len(ab_in)+min(len(a_ed),len(b_st))+a_b_num)

Submission status: Wrong Answer

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

cnt_ab = 0
cnt_a = 0
cnt_b = 0
cnt_ba = 0

for i in range(N):
    cnt_ab += List[i].count("AB")
for j in range(N):
    if (List[j][-1]=="A")&(List[j][0]!="B"):
        cnt_a+=1
for k in range(N):
    if (List[k][0]=="B")&(List[k][-1]!="A"):
        cnt_b +=1
for l in range(N):
    if (List[l][-1]=="A")&(List[l][0]=="B"):
        cnt_ba+=1
        
if (cnt_ba>abs(cnt_a-cnt_b)):
    cnt = cnt_ab+max(cnt_a, cnt_b)+cnt_ba-abs(cnt_a-cnt_b)
    print(cnt)
else:
    cnt = cnt_ab+min(cnt_a, cnt_b)+cnt_ba
    print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
onlytop = False
onlytail = False
topb = 0
taila = 0
counter = 0
for i in range(n):
    stmp = input()
    flag = False
    if stmp[0] == 'B':
        topb+=1
        if stmp[-1] == 'A':
            taila += 1
        else:
            onlytop = True
    elif stmp[-1] == 'A':
        taila +=1
        onlytail = True
    for i in stmp:
        if flag and i == 'B':
            counter += 1
        flag = False
        if i == 'A':
            flag = True
print('counter is ', counter)
if topb == taila:
    print(topb - 2 + onlytail + onlytop + counter)
else:
    print(min(topb,taila)+counter)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
S = []
for i in range(N):
    S.append(str(input()))
    
start_b = 0
end_a = 0
both = 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 == both:
    additional = additional  - 1
ans = ab_count + additional
print(ab_count)
print(additional)
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 += 1
        continue
    if S[0] == 'B':
        b += 1
    if S[-1] == 'A':
        a += 1
'''
if v >= 1:
    if a < b:
        a += 1
    else:
        b += 1
k = max(a, b) - min(a, b)

v = max(0, v-1)
'''
print(c + v + min(a, b))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = []
ab = []  # if include ab [num, ,] if b start [, 1], if a end [, ,1]
for i in range(N):
    S.append(input())

ab_num = 0

"""
b_start = 0
a_end = 0
for i in range(N):
    s = S[i]
    for j in range(1, len(s)):
        if s[j] == 'B' and s[j-1] == 'A':
            ab_num += 1
    if s[0] == 'B':
        b_start += 1
    if s[len(s)-1] == 'A':
        a_end += 1

print(ab_num + min(a_end, b_start))


BABA
ABABA
BAABBAAB
BBAAAAB
ABABABAB
"""

x_A = 0
B_A = 0
B_x = 0


for i in range(N):
    s = S[i]
    for j in range(1, len(s)):
        if s[j] == 'B' and s[j-1] == 'A':
            ab_num += 1

    if s[0] == 'B':
        if s[len(s) - 1] == 'A':
            B_A += 1
        else:
            B_x += 1
    elif s[len(s) - 1] == 'A':
        x_A += 1

if B_A > 0:
    if x_A * B_x > 0:
        print(ab_num + B_A-1 + 2 + min(x_A-1, B_x-1))
    else:
        print(ab_num + B_A-1 + 1)
else:
    print(ab_num + min(x_A, B_x))

Submission status: Wrong Answer

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


def main():
    n = int(input())
    s = [input() for _ in range(n)]
    ans = 0
    back_a_count = 0
    front_b_count = 0

    for si in s:
        if si[0] == 'B':
            front_b_count += 1
        if si[-1] == 'A':
            back_a_count += 1

        for j in range(len(si) - 1):
            if (si[j] == 'A' and si[j + 1] == 'B'):
                ans += 1

    print(ans + min(back_a_count, front_b_count))


if __name__ == '__main__':
    main()

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[1]>tokushu[2]:
    gacchi=tokushu[2]
    nokori=tokushu[1]-gacchi
    if nokori==0 and tokushu[3]>0:
        gacchi2=tokushu[3]-1
    elif nokori>=tokushu[3]:
        gacchi2=tokushu[3]
    else:
        gacchi2=tokushu[3]
elif tokushu[1]==tokushu[2]:
    if tokushu[1]==0:
        gacchi=0
        gacchi2=tokushu[3]
    else:
        gacchi=tokushu[1]
        gacchi2=tokushu[3]-1
else:
    gacchi=tokushu[1]
    nokori=tokushu[2]-gacchi
    if nokori==0 and tokushu[3]>0:
        gacchi2=tokushu[3]-1
    elif nokori>=tokushu[3]:
        gacchi2=tokushu[3]
    else:
        gacchi2=tokushu[3]

#print(kosu,gacchi,gacchi2)
print(kosu+gacchi+gacchi2)

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 = [s[-1] == 'A' for s in ss]
start_b = [s[0] == 'B'for s in ss]

end_a_start_b = [is_end_a and is_start_b
                 for is_end_a, is_start_b in zip(end_a, start_b)]

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

print(ss_ab_count, end_a_count, start_b_count, end_a_start_b_count)
# 大きい方をstart_b_countにする
if start_b_count < end_a_count:
    start_b_count, end_a_count = end_a_count, start_b_count

if end_a_start_b_count > start_b_count - end_a_count:
    end_a_start_b_count -= start_b_count - end_a_count
    end_a_count -= end_a_start_b_count // 2

count = ss_ab_count + end_a_count

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)
  count += a_b_min
  count += min(a_b_min, 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())
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,both)
count+=int(index*2)
A_list-=index
B_list-=index
both-=index

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

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[input() for i in range(n)]
ans=sum([s[i].count("AB") for i in range(n)])
xxA=sum([1 if s[i][0]!="B" and s[i][-1]=="A" else 0 for i in range(n)])
Bxx=sum([1 if s[i][0]=="B" and s[i][-1]!="A" else 0 for i in range(n)])
BxA=sum([1 if s[i][0]=="B" and s[i][-1]=="A" else 0 for i in range(n)])
if (abs(xxA-Bxx))>BxA:
    wer=min(xxA,Bxx)+BxA+1
else:
    wer=min(xxA,Bxx)+BxA
print(ans+wer)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

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

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

ans = 0
ans += count_ab

# あぶれ者をたす
ans += min(count_xa, count_bx)

# 両端がBAなものをくっつける
if count_ba != 0:
    ans += count_ba - 1

# あぶれ者が残ってれば+1
if count_xa != count_bx:
    ans += 1
    


print(ans)

Submission status: Wrong Answer

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

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

max_val = max(b_start, a_end)
min_val = min(b_start, a_end)
if max_val - min_val >= both:
    print(ok + min_val)
else:
    fusoku = both - (max_val - min_val)
    print(ok + min_val - (fusoku // 2 + fusoku % 2))

Submission status: Wrong Answer

Solution code:
N  = int(input())
ansMin = 1000000000
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[len(tmp) - 1] == 'B':
        comn2 = 1
    if tmp[0] == 'A':
        acount2 += 1
    if tmp[len(tmp) - 1] == 'B':
        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(tmp1, tmp2)

print(ans)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
C = [input() for i in range(n)]
N=A=B=0
for c in C:
    if c[0]=='B':
        B+=1
    if c[len(c)-1]=='A':
        A+=1
    for i in range(len(c)-1):
        if (c[i]=='A') & (c[i+1]=='B'):
            N+=1
print(N+min(A, B))

Submission status: Wrong Answer

Solution code:
N = int(input()) 
s = [input() for i in range(N)] 
ans = 0
X = 0
x = 0
y = 0
for n in range(N):
    if s[n].count('AB') > 0:
        ans += s[n].count('AB')
    if s[n][0] != 'B' and s[n][-1] == 'A':
        X += 1
    if s[n][0] == 'B' and s[n][-1] != 'A':
        x += 1
    if s[n][0] == 'B' and s[n][-1] == 'A':
        y += 1
if X-x <= 0:
    ans += X
if X-x >0 and X-x-y >0:
    ans += x + y
if X-x >0 and X-x-y <=0:
    ans += X
print(ans)

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):
    min_a_b = min(initial_b, last_a)
    cnt += min_a_b
    if both_ab == 0:
        return cnt

    if initial_b == last_a:
        return cnt + both_ab - 1

    diff_a_b = 0
    if last_a - min_a_b == 0:
        diff_a_b = initial_b - min_a_b
    else:
        diff_a_b = last_a - min_a_b

    second = min(both_ab, diff_a_b)

    cnt += second
    if both_ab - second > 0:
        cnt += both_ab - second

    return cnt


ans = calc(cnt)
print(ans)

Submission status: Wrong Answer

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

        
if counter_AB >= 2:
  counter += counter_AB - 1
        
counter_min = min(counter_A, counter_B)

print(counter + counter_min + 1)

Submission status: Wrong Answer

Solution code:
import re

N = int(input())

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

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


if BtoA > 0 and abs(startB - stopA) == 0:
    ans = AB + min(startB, stopA) + BtoA - 1
else:
    ans = AB + min(startB, stopA) + BtoA
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
    #print(c_need_a_and_b)
    if c_need_a_and_b < 0:
        c_need_a_and_b = -c_need_a_and_b
    else:
        c_need_a_and_b = 0
    #print(c_need_a_and_b)
    if c_need_a_and_b != 0:
        count_combi_a_b = min(c_head_has_b, c_tail_has_a) - c_need_a_and_b + 1
    else:
        count_combi_a_b = min(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()