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:
num = int(input())
master_count = 0
l_1 = 0
l_2 = 0
l_3 = 0

for a in range(num):
    s = input()
    if s[0]=='B':
        l_1 += 1
        
    if s[-1]=='A':
        l_2 += 1
        if (l_3 == 0) and (s[0]!='B'):
            l_3 = 1
            
        
    
    master_count +=  s.count("AB")
master_count = master_count + min(l_1,l_2)
if l_3 == 0:
    master_count-= 1
print(master_count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import stdin
N=int(stdin.readline().rstrip())

data=[]
ans=0
A=0
B=0
for i in range(N):
    S=input()
    ans+=S.count("AB")
    if S[0] == "B":
        B+=1
    if S[-1] == "A":
        A+=1

print(ans+max(A,B))

Submission status: Wrong Answer

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

ea=0
sb=0
easb=0
cnt= 0
for s in ss:
    cnt+= s.count("AB")
    la= s[-1]=="A"
    lb= s[ 0]=="B"
    if la: ea+=1
    if lb: sb+=1
    if la and lb: easb+=1
#
tot= cnt+min(ea,sb)
if ea==sb==easb: tot-=1
print( tot )

Submission status: Wrong Answer

Solution code:
N = int(input())
cnt_AB = 0
end_A = 0
start_B = 0
start_B_and_end_A= 0
for i in range(N):
    s = input()
    cnt_AB += s.count('AB')
    if s[-1] == 'A' and s[0] == 'B':
        start_B_and_end_A += 1
    else:
        if s[-1] == 'A':
            end_A += 1
        if s[0] == 'B':
            start_B += 1
if end_A == 0 and start_B == 0:
    print(cnt_AB + start_B_and_end_A - 1)
else:
    print(cnt_AB + start_B_and_end_A + min(end_A, start_B))

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 ab == N:
    print(cnt+ab-1)
else:
    print(cnt+min(a, b))

Submission status: Wrong Answer

Solution code:
N = int(input())
topb = 0
taila = 0
both = 0
cnt = 0
for _ in range(N):
    s = input()
    cnt +=s.count("AB")

    if s[0] == "B" and s[-1] == "A":
        both += 1
    elif s[0] == "B":
        topb += 1
    elif s[-1] == "A":
        taila += 1

con_cnt_min,con_cnt_max = min(topb,taila),max(topb,taila)
use_both = min(both,con_cnt_min,con_cnt_max)
cnt += use_both
both -= use_both
print(cnt + con_cnt_min + both)

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' and x[-1] == 'A':
        ab += 1
        continue
    if x[0] == 'B':
        b += 1
    if x[-1] == 'A':
        a += 1
    

if (a>0 and b>0) or ab>=2:
    if a==0:
        a = 1
        ab -= 1
    if b==0:
        b = 1
        ab -= 1
    if a > b:
        cnt += a//b + ab
    else:
        cnt += b//a + ab

print(cnt)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N = int(input())
S = [None] * N
cnt = 0
B_X = 0
X_A = 0
B_A = 0

for i in range(N):
  S[i] = input()
  cnt += S[i].count('AB')
  left_B_frag = S[i][0] == 'B'
  right_A_flag = S[i][-1] == 'A'
  if left_B_frag and right_A_flag:
    B_A += 1
  elif left_B_frag:
    B_X += 1
  elif right_A_flag:
  	X_A += 1

if B_A >= 2:
  cnt += B_A - 1
  B_A = 1
if B_X > X_A:
  cnt += B_A + X_A + min(B_A, B_X-X_A)
else:
  cnt += B_A + B_X + min(B_A, X_A-B_X)
# print(B_A, B_X, X_A)
print(cnt)

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 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())
s=[list(input()) for _ in range(N)]

ct_a=0
ct_b=0
ct_ab=0

for i in range(N):
    if s[i][0]=='B':
        ct_b+=1
    if 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_b==ct_a and ct_a>0:
    ct_b-=1
    ct_a-=1

print(ct_ab+min(ct_a,ct_b))

Submission status: Wrong Answer

Solution code:
n = int(input())
s_array = [input() for x in range(n)]
last_a = 0
reversible = 0
start_b = 0
flag = False
for s in s_array:
    if s[-1] == "A" and s[0] != "B":
        last_a += 1
    elif s[0] == "B" and s[-1] != "A":
        start_b += 1
    elif s[0] == "B" and s[-1] == "A":
        reversible += 1
    else:
        flag = True
res = 0
for s in s_array:
    res += s.count("AB")

if start_b > last_a or flag:
    tmp = 0
else:
    tmp = 1

if n == 1:
    print(res)
elif last_a == 0 and start_b == 0:
    print(res + reversible-1)
elif last_a == 0 or start_b == 0:
    print(res + reversible)
else:
    print(res + min(last_a, start_b) + reversible)

# a = []
# b = []
# other = []
# for s in s_array:
#     if s[0] == "B":
#         b.append(s)
#     elif s[-1] == "A":
#         a.append(s)
#     else:
#         other.append(s)
# res = ""
# print(a, b, other)
# for i in a:
#     res += i
#     for j in b:
#         res += j
#         continue
# for i in other:
#     res += i
# a_flag = False
# cnt = 0
# print(res)
# for i in res:
#     if i == "B" and a_flag:
#         cnt += 1
#     if i == "A":
#         a_flag = True
#     else:
#         a_flag = False
# print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
data = []
for i in range(N):
    data.append(str(input()))
    
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: Wrong Answer

Solution code:
n = int(input())

s = []

ans = 0

both = 0
a_a = 0
b_a = 0

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


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

while(both >0 and a_a > 0):
	both -= 1
	ans += 1


while(both >0 and b_a > 0):
	both -= 1
	ans += 1

ans += min(a_a, b_a)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

a = 0
b = 0
c = 0
co = 0
for i in range(n):
	l = input()
	co += l.count("AB")
	if l[0] == "B":
		b += 1
	if l[-1] == "A":
		a += 1
	if l[-1] == "A" and l[0] == "B":
		c += 1

k = min(a,b,c)

if c == k:
	co += 2 * c
	co += min(a-2*k,b-2*k)
elif a == k:
	co += a
	kk = min(c-a,b-a)
	co += kk
	if kk == b:
		co += c - k - kk + 1
	else:
		co += a
else:
	co += b
	kk = min(c-b,a-b)
	co += kk
	if kk == a:
		co += c - k - kk + 1
	else:
		co += a


print(co)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)

def f(a,b,ba):
  if a>0 and ba>0:
    return 1 + f(a,b,ba-1)
  elif a>0 and b>0:
    return 1+ f(a-1,b-1,ba)
  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 x in range(N):
  z=input()
  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: Wrong Answer

Solution code:
n = int(input())
list = []
cnt = 0
cnta = 0
cntb = 0
cntexp = 0
for i in range(n):
  list.append(input())
  a = list[i].count("AB")
  cnt += a
  if(list[i][-1:] == "A"):
    cnta += 1
  if(list[i][0:1] == "B"):
    cntb += 1
  if((list[i][-1:] == "A")and(list[i][0:1] == "B")):
    cntexp += 1
cntab = min(cnta, cntb)
if(cntab == cntexp):
  cntab -= 1
ans = cnt + cntab
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
ca = 0
cb = 0
cc = 0
for i in range(n):
    t = input()
    s.append(t)
    if t[-1] == "A":
        ca += 1
    if t[0] == "B":
        cb += 1
    if t[-1] == "A" and t[0] == "B":
        cc += 1
cnt = 0
for i in range(n):
    cnt += s[i].count("AB")
if cc >= min(ca, cb) >= n - 1:
    cnt -= 1
print(cnt + min(ca, cb))

Submission status: Wrong Answer

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: 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
if topb == taila:
    print(topb - 2 + onlytail + onlytop + counter)
else:
    print(min(topb,taila)+counter)

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)

# あぶれ者残り
count_xx = max(count_xa, count_bx) - min(count_xa, count_bx)

if count_xx > 0 and count_ba > 0:
    # あぶれ者とBA
    ans += min(count_ba, count_xx)

    # あぶれ者残りとBA残り
    count_ba = max(count_ba - count_xx, 0)
    count_xx = max(count_xx - count_ba, 0)

    # 残りの両端がBAなものをくっつける、端に上で作った一個がつく
    ans += max(count_ba, 0)
elif count_ba > 0:
    ans += count_ba - 1
    


print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = [input() for i in range(N)]
 
b_start = []
a_end = []
count = 0
dob = 0
for i in s:
  b_flag = False
  a_flag = False
  if "AB" in i:
    count += 1
  if i[0] == "B":
    b_start.append(i)
    b_flag = True
  if i[-1] == "A":
    a_end.append(i)
    a_flag = True
  if b_flag and a_flag:
    dob += 1

if min(len(b_start), len(a_end)) == dob:
  count += min(len(b_start), len(a_end))
elif dob == 0:
  count += min(len(b_start), len(a_end)) * 2 - 1
else:
  count += min(len(b_start), len(a_end))
  
print(count)

Submission status: Wrong Answer

Solution code:
import random
x=input()
xx=int(x)
y=[]
b=0
l1=""
for i in range(xx):
  z=input()
  y.append(z)
for j in range(xx):
  lr = random.sample(y, len(y))
  for i in range(xx):
    l1+=lr[i]
  bb=l1.count("AB")
  j+=1
  if bb>=b:
    b=bb
print(b)

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==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:
    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=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
a+=min(A,B,N-1)
print(a)

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[1] != 'B' and tmp[len(tmp) - 1] == 'B':
        comn2 = 1
    if tmp[0] == 'A' and tmp[1] !='B':
        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(tmp11, tmp22)

print(ans)

Submission status: Wrong Answer

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

a = [0] * N
b = [0] * N

ans = 0
for i in range(N):
    if s[i][0] == "B":
        b[i] = 1
    if s[i][-1] == "A":
        a[i] = 1
    for j in range(0,len(s[i])-1):
        if s[i][j] == "A" and s[i][j+1] == "B":
            ans += 1

special = 0

for i in range(N):
    if a[i] == 1 and b[i] == 1:
        special += 1
        a[i] = 0
        b[i] = 0


counta = a.count(1)
countb = b.count(1)




if special > 0:
    ans += special - 1
    if counta > countb:
        countb += 1
    elif countb > counta:
        counta += 1

if counta > countb:
    ans += countb
elif countb > counta:
    ans += counta
else:
    ans += counta


print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
b_start=0
a_last=0
both=0
ans=0
for i in range(n):
    s=input()
    ans+=s.count("AB")
    if s[0]=="B":
        if s[len(s)-1]=="A":
            both+=1
        else:
            b_start+=1
    elif s[len(s)-1]=="A":
        a_last+=1
diff=abs(b_start-a_last)
# print(a_last, b_start, both, ans, diff)
ans+=max(both-1,0)
if a_last>=1:
    ans+=1
    a_last-=1
if b_start>=1:
    ans+=1
    b_start-=1
ans+=min(a_last,b_start)
print(ans)
# if diff==0:
#     print(1)
#     print(max(b_start,a_last)+max(0,(both-1))+ans)
# elif diff >= both:
#     print(2)
#     print(min(b_start,a_last)+both+ans)
# else:
#     print(3)
#     print(max(b_start,a_last)+(both-diff)+ans)

Submission status: Wrong Answer

Solution code:
#C問題
N = int(input())
S = [input() for i in range(N)]
ans = 0
b_top_count = 0
a_down_count = 0
ans_1 = 0

for i in range(N):
    ans += S[i].count("AB")

    if S[i][0] == "B":
        b_top_count += 1
    if S[i][-1] == "A":
        a_down_count += 1

ans_1+= min(b_top_count, a_down_count)
if ans_1>= N:
    ans_1 = N -1
    
print(ans + ans_1)

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())
lstBA = []
lstA = []
lstB = []
lstOther = []
char = ""

for _ in range(N):
    s = input()
    if s[0]=="B" and s[-1]=="A":
        lstBA.append(s)
    elif s[0]=="B":
        lstB.append(s)
    elif s[-1]=="A":
        lstA.append(s)
    else:
        lstOther.append(s)
# print(lstBA, lstB, lstA, lstOther)
charBA = "".join(lstBA)

chartmp = ""
for a, b in zip(lstA, lstB):
    tmp = a + b
    chartmp += tmp
# print(chartmp)

addchar = ""
lenA = len(lstA)
lenB = len(lstB)
if lenA != lenB:
    if lenA == lenB + 1:
        charBA = lstA[-1] + charBA
    elif lenB == lenA + 1:
        charBA = charBA + lstB[-1]

char = charBA + chartmp + "".join(lstOther)
# print(char)
ans = char.count("AB")
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
for i in range(n):
    s.append(input())
ans = 0
last = 0
start = 0
for i in range(n):
    ans += s[i].count('AB')
    list = s[i]
    if list[-1] == 'A':
        last += 1
    if list[0] == 'B':
        start += 1
    # print(ans, last, start)
ans += min(last, start)
print(ans)

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)

add = max(B_A - 1,0)


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())
s = []
ans = 0
for i in range(N):
    si = input()
    s.append(si)

    ans += si.count("AB")




d = {}
d["A"] = 0
d["B"] = 0
d["all"] = 0

for i in range(N):
    if s[i][0] == "B" and s[i][-1] == "A":
        d["all"] += 1
    elif s[i][0] == "B":
        d["B"] += 1
    elif s[i][-1] == "A":
        d["A"] += 1

if d["all"] > 1:
    ans += (d["all"] - 1)
    d["A"] += 1
    d["B"] += 1
    ans += min(d["A"],d["B"])
else:
    if d["all"] == 1:
        a = min(1,d["A"],d["B"])
        ans += a*2
        d["A"] -= a
        d["B"] -= a
        d["all"] -= a
        ans += min(d["A"],d["B"])
    else:
        ans += min(d["A"],d["B"])

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N= int(input())

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

print(ans + min(a,b,k-1))

Submission status: Wrong Answer

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

count=0
ca=0
cb=0
cc=0
for i in range(N):
    if S[i][0]=="B":
        ca+=1
    if S[i][len(S[i])-1]=="A":
        cb+=1
    if S[i][0]=="B" and S[i][len(S[i])-1]=="A":
        cc+=1
    for x in range(len(S[i])-1):
        if S[i][x:x+2]=="AB":
            count+=1

if ca==cc and cb==cc:
    print(count+min(ca,cb)-1)
else:
    print(count+min(ca,cb))

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] != 'B'):
      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
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:
N = int(input())
a = 0
b = 0
c = 0
d = 0

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

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

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

    e = b - c

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

Submission status: Wrong Answer

Solution code:
n = int(input())
s = n*[""]
count = 0
nn = 0
bn = 0
na = 0
ba = 0
for i in range(n):
    s[i] = input()
    count += s[i].count("AB")
    if(s[i][0] == "B" and s[i][-1] == "A"): ba += 1
    elif(s[i][0] == "B"): bn += 1
    elif(s[i][-1] == "A"): na += 1


while(sum([ba,na,bn]) > 1):
    if(na > 0 and ba > 0 and bn > 0):
        count += ba+1
        ba = 0
        na -= 1
        bn -= 1
    elif(na > 0 and bn > 0):
        count += min([na, bn])
        na -= min([na, bn])
        bn -= min([na, bn])
    elif(na > 0 and ba > 0):
        count += ba
        na -= 1
        ba = 0
    elif(bn > 0 and ba > 0):
        count += ba
        bn -= 1
        ba = 0
    elif(ba > 0):
        count += ba - 1
        ba = 0
    else: break


print(count)

Submission status: Wrong Answer

Solution code:
def task_c():
    n = int(input())
    sss = [input().strip() for _ in range(n)]
    
    def count_ab(tt):
        c = 0
        for i in range(1, len(tt)):
            if tt[i-1:i+1] == 'AB':
                c += 1
        return c
    
    c_first_b = sum(1 for ss in sss if ss[:1] == 'B' and ss[-1:] != 'A')
    c_last_a = sum(1 for ss in sss if ss[:1] != 'B' and ss[-1:] == 'A')
    c_ab = sum(1 for ss in sss if ss[:1] == 'B' and ss[-1:] == 'A')
    c_include_ab = sum(count_ab(ss) for ss in sss)
    c = min(c_first_b+c_ab, c_last_a+c_ab) + c_include_ab
    if c_first_b == 0 or c_last_a == 0:
        c -= 1
    print(c)
    
task_c()

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 fb_cnt == ba_cnt else min_cnt
print(cnt + min_cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
cnt = 0
bx = 0
xa = 0
ba = 0

for i in range(n):
  si = input()
  flg = False
  for c in si:
    if c == 'A':
      flg = True
    elif c == 'B' and flg:
      cnt += 1
      flg = False
        
    else:
      flg = False
  
  if si[0] == 'B':
    bx += 1
  if si[-1] == 'A':
    xa += 1
  if si[0] == 'B' and si[-1] == 'A':
    ba += 1
    
bxa = min(bx, xa)
if bxa == n or max(bx, xa) == ba:
  bxa -= 1
print(cnt + bxa)

Submission status: Wrong Answer

Solution code:
N=int(input())
s=[]
for i in range(N):
    s.append(input())
a=[0]*N
b=[0]*N
c=[0]*N
for i in range(N):
    a[i]=s[i].count('AB')
    if s[i][-1]=='A':
        b[i]=1
    else:
        pass
    if s[i][0]=='B':
        c[i]=1

B=sum(b)
C=sum(c)
D=min(B,C)
if min(B,C)==N:
    D=N-1
else:
    pass
d=[0]*N
for i in range(N):
    d[i]=b[i]-c[i]
if sum(d)==0:
    D=D-1
else:
    pass
print(sum(a)+ D)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = []
for i in range(N):
    s.append(input())
first,last,ans = 0,0,0
for i in s:
    if i[0] == "B":
        first += 1
    if i[-1] == "A":
        last += 1

    ans += i.count("AB")

if first < last:
    ans += first
else:
    ans += last

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

ss = [0] * N

a = 65
b = 66

count = 0
for i in range(N):
    s = input()
    ss[i] = s
    for k in range(len(s) - 1):
        if ord(s[k]) == a and ord(s[k + 1]) == b:
            count += 1
start_b = 0
end_a = 0
both = 0
for i in range(N):
    s = ss[i]
    if ord(s[0]) == b:
        start_b += 1
    if ord(s[-1]) == a:
        end_a += 1
    if ord(s[0]) == b and ord(s[-1]) == a:
        both += 1

result = count + min(start_b, end_a)
if min(start_b, end_a) == both:
    result -= 1
print(result)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
AtCoder C
"""

# N = int(input())
# A = list(map(int,input().split())) # N row 1 column
# A = int(input()) for _ in range(N) # 1 row N column

# S = str(input()) # str(input()) == input() -> 'abc'
# S = list(input()) # abc -> ['a','b','c']

def main():
    # A, B = list(map(int,input().split()))
    N = int(input())
    S = [input() for _ in range(N)]# 1 row N column
    tmp=0
    a=0
    b=0
    flag=False
    for s in S:
        tmp+=s.count('AB')
        if s[0]=="B":
            a+=1
            if s[-1]!="A":
                flag=True
        if s[-1]=="A":
            b+=1
            if s[0]!="B":
                flag=True
    if flag==False:
        a-=1
    print(tmp+min(a,b))
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
a=input()
a=int(a)
count=0
ap=0
bp=0
abc=0
for j 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
        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
pp=a-1
if ap>bp:
    count+=bp
    bp=0
    ap-=bp
    pp-=bp
else:
    count+=ap
    ap=0
    bp-=ap
    pp-=ap
if abc>0:
    count+=abc-1
    pp-=abc-1
    if ap>0 or bp>0:
        if pp>0:
            count+=1
print(count)

Submission status: Wrong Answer

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

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

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

if c3>max(c1,c2):
  if c1<c2:
    c+=c1*2
    c2-=c1
    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(c)

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
  s = int(input())
  Str = [0 for a in range(s)]
  for a in range(s):
    Str[a] = input()
  count = 0
  Both  = 0
  flag  = 0
  LastA = 0
  LastB = 0
  for a in range(s):
    count += Str[a].count("AB")

    if Str[a][len(Str[a])-1] == "A":
      print(Str[a][len(Str[a])-1])
      LastA += 1
      flag = 1
    elif Str[a][0] == "B":
      LastB += 1
      if flag == 1:
        Both += 1
        flag = 0
      
  if LastA == s:
    LastA = LastA - 1
  if LastB == s:
    LastB = LastB - 1
    
  if LastA > LastB:
    print(count + LastB)
  else:
    print(count + LastA - Both)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for i in range(n)]
count,a,b,ab = 0,0,0,0
for word in s:
  while "AB" in word:
    count += 1
    word = word.replace("AB","")
  if word[0] == "B" and word[-1] =="A":
    ab += 1
  elif word[0] == "B":
    b += 1
  elif word[-1] == "A":
    a += 1
  else:
    pass
if ab == 0:
  print(min([a,b])+count)
else:
  if a == 0 and b == 0:
    print(ab-1+count)
  elif a == 0 or b == 0:
    print(ab+count)
  else:
    print(ab+1+min([a-1,b-1])+count)

Submission status: Wrong Answer

Solution code:
# https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = s_inpl()   |	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = inpl()	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]

import math
import copy
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
from bisect import bisect_left, bisect_right
# import numpy as np

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

# 四方向: 右, 下, 左, 上
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]

def i_inpl(): return int(input())
def s_inpl(): return map(int,input().split())
def l_inpl(): return list(map(int, input().split()))
INF = float("inf")

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

ab_cnt = 0
f_a_cnt = 0
f_b_cnt = 0
l_a_cnt = 0
l_b_cnt = 0
f_b_l_a_cnt = 0

for s_i in s:
    ab_cnt += s_i.count("AB")
    if s_i[-1] == "A" and s_i[0] == "B":
        f_b_l_a_cnt += 1
    elif s_i[0] == "B":
        f_b_cnt += 1
    elif s_i[-1] == "A":
        l_a_cnt += 1

ans = ab_cnt + min(f_b_cnt, l_a_cnt)
for add_l_a_cnt in range(0, f_b_l_a_cnt*2):
    new_l_a_cnt = l_a_cnt + add_l_a_cnt
    new_f_b_cnt = f_b_cnt + (f_b_l_a_cnt*2-1 - add_l_a_cnt)
    ans = max(ans, ab_cnt + min(new_f_b_cnt, new_l_a_cnt))

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
AtCoder C
"""

# N = int(input())
# A = list(map(int,input().split())) # N row 1 column
# A = int(input()) for _ in range(N) # 1 row N column

# S = str(input()) # str(input()) == input() -> 'abc'
# S = list(input()) # abc -> ['a','b','c']

def main():
    # A, B = list(map(int,input().split()))
    N = int(input())
    S = [input() for _ in range(N)]# 1 row N column
    tmp=0
    a=0
    b=0
    flag=False
    for s in S:
        tmp+=s.count('AB')
        if s[0]=="B":
            a+=1
            if s[-1]!="A":
                flag=True
        if s[-1]=="A":
            b+=1
            if s[0]!="B":
                flag=True
    print(flag,a,b)
    if flag==False:
        a-=1
    print(tmp+min(a,b))
    
if __name__ == "__main__":
    main()