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

count_A = 0
count_B = 0
count_AB = 0
count = 0

for i in range(N):
    flag = False
    for j in range(len(s[i])):
        if j == 0 and s[i][j] == "B":
            count_B += 1
            flag = True
        if s[i][j] == "A":
            if j == len(s[i]) - 1:
                count_A += 1
                if flag:
                    count_A -= 1
                    count_B -= 1
                    count_AB += 1
            elif s[i][j+1] == "B":
                count += 1

if count_A == 0 and count_B == 0:
    count += count_AB - 1

else:
    if count_A <= count_B:
        count += count_A
    else:
        count += count_B

    count += count_AB

print(count)

Submission status: Wrong Answer

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

count_A = 0
count_B = 0
count_AB = 0
count = 0

for i in range(N):
    for j in range(len(s[i])):
        tmp_A = count_A
        tmp_B = count_B

        if j == 0 and s[i][j] == "B":
            count_B += 1
        if s[i][j] == "A":
            if j == len(s[i]) - 1:
                count_A += 1
            elif s[i][j+1] == "B":
                count += 1
        if tmp_A != count_A and tmp_B != count_B:
            count_A -= 1
            count_B -= 1
            count_AB += 1

if count_A != 0 or count_B != 0:
    if count_A <= count_B:
        count += count_A
    else:
        count += count_B

    count += count_AB

else:
    count += count_AB - 1

print(count)

Submission status: Wrong Answer

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

count_A = 0
count_B = 0
count_AB = 0
count = 0

for i in range(N):
    for j in range(len(s[i])):
        tmp_A = count_A
        tmp_B = count_B

        if j == 0 and s[i][j] == "B":
            count_B += 1
        if s[i][j] == "A":
            if j == len(s[i]) - 1:
                count_A += 1
            elif s[i][j+1] == "B":
                count += 1
        if tmp_A != count_A and tmp_B != count_B:
            count_A -= 1
            count_B -= 1
            count_AB += 1

if count_A != 0 and count_B != 0:
    if count_A <= count_B:
        count += count_A
    else:
        count += count_B

    count += count_AB

else:
    count += count_AB - 1

print(count)

Submission status: Wrong Answer

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

count_A = 0
count_B = 0
count = 0

for i in range(N):
    for j in range(len(s[i])):
        if j == 0 and s[i][j] == "B":
            count_B += 1
        if j == len(s[i]) - 1 and s[i][j] == "A":
            count_A += 1
        if s[i][j] == "A" and j < len(s[i]) - 1:
            if s[i][j+1] == "B":
                count += 1

if count_A <= count_B:
    count += count_A
else:
    count += count_B
print(count)

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
    
ans += min(A+AB, B+AB)


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

print(ans)

Submission status: Wrong Answer

Solution code:
# BA=0, B=0, A=0, 0
# BA=2, B=0, A=0, 1
# BA=0, B=1, A=0, 0
# BA=0, B=0, A=1, 0
# BA=2, B=1, A=0, 2
# BA=2, B=0, A=1, 2
# BA=2, B=1, A=1, 2

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 A==0 and B==0:
#     ans -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
# BA=0, B=0, A=0, 0
# BA=2, B=0, A=0, 1
# BA=0, B=1, A=0, 0
# BA=0, B=0, A=1, 0
# BA=2, B=1, A=0, 2
# BA=2, B=0, A=1, 2
# BA=2, B=1, A=1, 2

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 A==0 and B==0:
    ans -= 1

print(ans)

Submission status: Wrong Answer

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

res = sum([i.count("AB") for i in l])+min(sum([1 for i in l if i[-1]=="A"]),sum([1 for i in l if i[0] =="B"]))
if [1 if i[-1]=="A" else 0 for i in l]== [1 if i[0] =="B" else 0 for i in l]:
  res -= 1
print(res)

Submission status: Wrong Answer

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

res = sum([i.count("AB") for i in l])+min(sum([1 for i in l if i[-1]=="A"]),sum([1 for i in l if i[0] =="B"]))
if sum([1 for i in l if i[-1]=="A"])==sum([1 for i in l if i[0] =="B"]):
  res -= 1
print(res)

Submission status: Wrong Answer

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

print(sum([i.count("AB") for i in l])+min(sum([1 for i in l if i[-1]=="A"]),sum([1 for i in l if i[0] =="B"])) )

Submission status: Wrong Answer

Solution code:
from re import findall


def main():
    N = int(input())
    num_full = 0
    begin_b = 0
    end_a = 0
    both = 0
    for i in range(N):
        s_i = input()
        num_full += len(findall('AB', s_i))
        if s_i[0] == 'B' and s_i[-1] == 'A':
            both += 1
        if s_i[0] == 'B':
            begin_b += 1
        if s_i[-1] == 'A':
            end_a += 1
    ans = num_full
    if begin_b == end_a:
        if begin_b == both:
            ans += (begin_b - 1)
        else:
            ans += begin_b
    else:
        ans += min(begin_b, end_a)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# n = int(input())

# arr = list(map(int,input().split(" ")))

# arr.sort()

# print(arr)




# # k = 99999999999999

# # for i in range(n):

# # 	for j in range(i+1,n):

# # 		mini = min(arr[i],arr[j])

# # 		diff = abs(j-i)

# # 		temp = mini//diff


# # 		if(temp <= k):

# # 			k = mini//diff


# # print(k)


n = int(input())

a,b=0,0
flg = 1
ans = 0

for _ in range(n):

	s = input()

	if(s[0] == 'B' and s[-1] == 'A'):
		a += 1
		b += 1

	elif(s[0] == 'B'):
		b += 1
		flg = 0


	elif(s[-1] == 'A'):
		a += 1
		flg = 0 


	ans += s.count('AB')



print(ans+min(a,b) - flg)

Submission status: Wrong Answer

Solution code:
from re import findall


def main():
    N = int(input())
    num_full = 0
    begin_b = 0
    end_a = 0
    both = 0
    for i in range(N):
        s_i = input()
        num_full += len(findall('AB', s_i))
        if s_i[0] == 'B' and s_i[-1] == 'A':
            both += 1
        if s_i[0] == 'B':
            begin_b += 1
        if s_i[-1] == 'A':
            end_a += 1
    ans = num_full
    if begin_b == end_a:
        if begin_b == both:
            ans += begin_b - 1
        else:
            ans += begin_b
    else:
        ans += min(begin_b, end_a)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from re import findall


def main():
    N = int(input())
    num_full = 0
    begin_b = 0
    end_a = 0
    both = 0
    for i in range(N):
        s_i = input()
        num_full += len(findall('AB', s_i))
        if s_i[0] == 'B' and s_i[-1] == 'A':
            both += 1
        if s_i[0] == 'B':
            begin_b += 1
        if s_i[-1] == 'A':
            end_a += 1
    ans = num_full
    if begin_b == end_a:
        if begin_b == N:
            ans += begin_b - 1
        elif begin_b == both:
            ans += begin_b - 1
        else:
            ans += begin_b
    else:
        ans += min(begin_b, end_a)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
ans=0
cntA,cntB,cntAB=0,0,0
for _ in range(n):
    s=input().strip()
    ans+=s.count('AB')
    if s[0]=='B':
        cntB+=1
        if s[-1]=='A':
            cntAB+=1
    if s[-1]=='A':
        cntA+=1
ans+=min(cntA,cntB)
if cntA==cntB==cntAB:
    ans-=1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
ans=0
cntA,cntB,cntAB=0,0,0
for _ in range(n):
    s=input().strip()
    ans+=s.count('AB')
    if s[0]=='B':
        cntB+=1
        if s[-1]=='A':
            cntAB+=1
    if s[-1]=='A':
        cntA+=1
ans+=min(cntA,cntB)
if cntA==cntB and cntAB:
    ans-=1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())


ans = 0
a,b = 0,0

for i in range(n):

	s = input()

	if(s.count('AB') > 0):
		ans += 1

	if(s[-1] == 'A'):
		a += 1

	if(s[0] == 'B'):
		b += 1



print(min(a,b) + ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a,b,c=0,0,0
for i in range(n):
    s=input()
    if len(s)>1:
        for j in range(1,len(s)):
            if s[j-1]+s[j]=="AB":
                c+=1
    if s[0]=="B":
        b+=1
    if s[-1]=="A":
        a+=1
print(c+min(min(a,b),n-1))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
c = 0
check = [0,0,0]
for i in range(n):
  s.append(input())
  if "AB" in s[i]:
    c+=1
  if s[i][0] == 'B' and s[i][-1] == 'A':
    check[0]+=1
  elif s[i][0] == 'B':
    check[1]+=1
  elif s[i][-1] == 'A':
    check[2]+=1
    
if check[0] == 0:
  c+=min(check[1],check[2])
elif check[1] + check[2] == 0:
  c+=check[0]-1
else:
  c+=check[0]+min(check[1],check[2])
print(c)

Submission status: Wrong Answer

Solution code:
n = int(input())

s=[]
res = 0
tmp = 0
eA=0
sB=0
eAsB=0
for i in range(n):
	s.append(input())
	tmp = s[i].count("AB")
	if tmp != 0:
		res += tmp
	if s[i][0]=="B" and s[i][-1]=="A":
		eAsB += 1
	elif s[i][-1]=="A":
		eA += 1
	elif s[i][0]=="B":
		sB += 1

if eAsB != 0:
	if eA < sB:
		res += eA+1
	else:
		res += sB+1
else:
	if eA < sB:
		res += eA
	else:
		res += sB

if eAsB > 1:	
	res += eAsB-1

print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())

S = []
for n in range(N):
	S.append(input())

Acount  = 0
Bcount  = 0
ABcount = 0
BXAcount = 0

for s in S:
	if s[0]  == "B" :
		Bcount += 1
	if s[-1] == "A" :
		Acount += 1
	for i in range(len(s)-1) : 
		if s[i] == "A" and s[i+1] == "B" :
			ABcount += 1
	if s[0] == "B" and s[-1] == "A" :
		BXAcount += 1

#print(S)
#print("Acount  = " + str(Acount))
#print("Bcount  = " + str(Bcount))
#print("ABcount = " + str(ABcount))
if BXAcount >= 1 :
	if Acount == BXAcount and Bcount == BXAcount :
		print(min(Acount, Bcount) + ABcount -1)
else:
	print(min(Acount, Bcount) + ABcount)

Submission status: Wrong Answer

Solution code:
n = int(input())

ab, start_b, end_a = 0, 0, 0

if n == 1:
  print(input().count('AB'))
else:
  for _ in range(n):
    chars = input()
    if 'AB' in chars:
      ab += chars.count('AB')
    if chars[0] == 'B':
      start_b += 1
    if chars[-1] == 'A':
      end_a += 1
  print(ab + min(start_b, end_a))

Submission status: Wrong Answer

Solution code:
n = int(input())

ab, start_b, end_a = 0, 0, 0
for _ in range(n):
  chars = input()
  if 'AB' in chars:
    ab += chars.count('AB')
  if chars[0] == 'B':
    start_b += 1
  if chars[-1] == 'A':
    end_a += 1
    
print(ab + min(start_b, end_a))

Submission status: Wrong Answer

Solution code:
n = int(input())

s=[]
res = 0
tmp = 0
eA=0
sB=0
eAsB=0
for i in range(n):
	s.append(input())
	tmp = s[i].count("AB")
	if tmp != 0:
		res += tmp
	if s[i][0]=="B" and s[i][-1]=="A":
		eAsB += 1
	elif s[i][-1]=="A":
		eA += 1
	elif s[i][0]=="B":
		sB += 1

if eAsB != 0:
	if eA < sB:
		res += eA+1
	elif eA == sB:
		res += eA
	else:
		res += sB+1
else:
	if eA < sB:
		res += eA
	else:
		res += sB

if eAsB > 1:	
	res += eAsB-1

print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())

S = []
for n in range(N):
	S.append(input())

Acount  = 0
Bcount  = 0
ABcount = 0

for s in S:
	if s[0]  == "B" :
		Bcount += 1
	if s[-1] == "A" :
		Acount += 1
	for i in range(len(s)-1) :
		if s[i] == "A" and s[i+1] == "B" :
			ABcount += 1

#print(S)
#print("Acount  = " + str(Acount))
#print("Bcount  = " + str(Bcount))
#print("ABcount = " + str(ABcount))
print(min(Acount, Bcount) + ABcount)

Submission status: Wrong Answer

Solution code:
N = int(input())
headB=0
tailA=0 
both = 0
ans = 0
for j in range(N):
    t = input()
    if t[0] == "B":
        if t[-1] == "A":
            both += 1
        else:
            headB += 1
    elif t[-1] == "A":
        tailA += 1
    ans += t.count("AB")
if tailA > 0 or headB > 0:
    while both > 0:
        tmp = min(tailA,both)
        ans += tmp
        both -= tmp
        tmp = min(headB,both)
        ans += tmp
        both -= tmp
else:
    ans += both-1
ans += min(headB,tailA)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
headB=0
tailA=0 
both = 0
ans = 0
for j in range(N):
    t = input()
    if t[0] == "B":
        if t[-1] == "A":
            both += 1
        else:
            headB += 1
    elif t[-1] == "A":
        tailA += 1
    ans += t.count("AB")
if tailA > 0 or headB > 0:
    while both > 0:
        tmp = min(tailA+headB,both)
        ans += tmp
        both -= tmp
else:
    ans += both-1
ans += min(headB,tailA)
print(ans)

Submission status: Wrong Answer

Solution code:
import re
aflag = 0
bflag = 0
cnt = 0
cntna = 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("ABABABABAB", s):
        cnt += 1
    if s[-1:]=="A" and s[:1]=="B":
        cntna +=1

cnt += min(aflag, bflag)
if min(aflag,bflag) == N:
    cnt -= 1
if cntna == min(aflag, bflag) and aflag == bflag:
    cnt -= 1
if aflag == bflag == N:
    cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
import re
aflag = 0
bflag = 0
cnt = 0
cntna = 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("ABABABABAB", s):
        cnt += 1
    if s[-1:]=="A" and s[:1]=="B":
        cntna +=1

cnt += min(aflag, bflag)
if min(aflag,bflag) == N:
    cnt -= 1
if cntna == min(aflag, bflag) and aflag == bflag:
    cnt -= 1
print(cnt)

Submission status: Wrong Answer

Solution code:
def count(a):
    k = 0
    for i in range(len(a)-1):
        if a[i]=='A' and a[i+1]=='B':
            k = k+1
    return k
def Substrings(n,S):   
    res = 0
    m = 0
    n = 0
    t = 0
    for i in range(len(S)):
        res = count(S[i]) + res
        if S[i][-1:]=='A':
            m = m+1
        if S[i][:1]=='B':
            n = n+1   
        if S[i]=='AB':
            t = t+1
    return res+min(m,n)-t
n = int(input())
S = [input() for i in range(n)]
print(Substrings(n,S))

Submission status: Wrong Answer

Solution code:
def count(a):
    k = 0
    for i in range(len(a)-1):
        if a[i]=='A' and a[i+1]=='B':
            k = k+1
    return k
def Substrings(n,S):   
    res = 0
    m = 0
    n = 0
    t = 0
    for i in range(len(S)):
        res = count(S[i]) + res
        if S[i][-1:]=='A':
            m = m+1
        if S[i][:1]=='B':
            n = n+1   
        if S[i][-1:]=='A'and S[i][:1]=='B':
            t = t+1
    return res+min(m,n)-t
n = int(input())
S = [input() for i in range(n)]
print(Substrings(n,S))

Submission status: Wrong Answer

Solution code:
def count(a):
    k = 0
    for i in range(len(a)-1):
        if a[i]=='A' and a[i+1]=='B':
            k = k+1
    return k
def Substrings(n,S):   
    res = 0
    m = 0
    n = 0
    for i in range(len(S)):
        res = count(S[i]) + res
        if S[i]!='AB' and S[i]!='BA':
            if S[i][-1:]=='A':
                m = m+1
            if S[i][:1]=='B':
                n = n+1   
    return res+min(m,n) 
n = int(input())
S = [input() for i in range(n)]
print(Substrings(n,S))

Submission status: Wrong Answer

Solution code:
def count(a):
    k = 0
    for i in range(len(a)-1):
        if a[i]=='A' and a[i+1]=='B':
            k = k+1
    return k
def Substrings(n,S):   
    res = 0
    m = 0
    n = 0
    for i in range(len(S)):
        res = count(S[i]) + res
        if S[i][-1:]=='A':
            m = m+1
        if S[i][:1]=='B':
            n = n+1   
    return res+min(m,n) 
n = int(input())
S = [input() for i in range(n)]
print(Substrings(n,S))

Submission status: Wrong Answer

Solution code:
def c(strs):
    count = 0
    count_a = 0
    count_b = 0
    flag = 1
    for s in strs:
        count += s.count('AB')
        if s[0]=='B' and s[-1]=='A':
            count_a += 1
            count_b += 1
        elif s[0]=='B':
            count_b += 1
            flag = 0
        elif s[-1]=='A':
            count_a += 1
            flag = 0
    print(count,count_a,count_b)
    if count_a:
        return count + min(count_a,count_b) - flag
    return count


if __name__=='__main__':
    n = int(input())
    s = []
    for _ in range(n):
        s.append(input())
    print(c(s))

Submission status: Wrong Answer

Solution code:
def c_ab_substrings(N, S):
    """
    N個の文字列を好きな順序で並べたあと、連結して1つの文字列を作る。
    作った文字列に 'AB' という部分文字列が含まれる個数として
    ありうる値のうち最大のものを求めよ。
    """

    num_start_b_end_a = 0
    num_start_b_not_end_a = 0
    num_not_start_b_end_a = 0
    ans = 0
    for s in S:
        if s[0] == 'B' and s[-1] == 'A':
            num_start_b_end_a += 1
        if s[0] == 'B' and s[-1] != 'A':
            num_start_b_not_end_a += 1
        if s[0] != 'B' and s[-1] == 'A':
            num_not_start_b_end_a += 1
        if 'AB' in s:
            ans += 1
    if num_start_b_end_a == 0:
        # 'B.*A' がない場合、'B.*^A' と '^B.*A' を結合して '.*AB.*' を生み出せる
        ans += min(num_start_b_not_end_a, num_not_start_b_end_a)
    else:
        # 'B.*A' がある場合、その後に 'B.*^A' を結合するか、その前に '^B.*A' を結合する
        # 'B.*^A' も '^B.*A' もない場合、 'B.*A' 同士で結合させる
        if num_start_b_not_end_a + num_not_start_b_end_a > 0:
            ans += num_start_b_end_a + min(num_start_b_not_end_a, num_not_start_b_end_a)
        else:
            ans += num_start_b_end_a - 1
    return ans

N = int(input())
S = [input() for _ in range(N)]
print(c_ab_substrings(N, S))

Submission status: Wrong Answer

Solution code:
def c_ab_substrings(N, S):
    """
    N個の文字列を好きな順序で並べたあと、連結して1つの文字列を作る。
    作った文字列に 'AB' という部分文字列が含まれる個数として
    ありうる値のうち最大のものを求めよ。
    """

    num_start_b_end_a = 0
    num_start_b_not_end_a = 0
    num_not_start_b_end_a = 0
    ans = 0
    for s in S:
        if s[0] == 'B' and s[-1] == 'A':
            num_start_b_end_a += 1
        elif s[0] == 'B' and s[-1] != 'A':
            num_start_b_not_end_a += 1
        elif s[0] != 'B' and s[-1] == 'A':
            num_not_start_b_end_a += 1
        if 'AB' in s:
            ans += 1
    if num_start_b_end_a == 0:
        # 'B.*A' がない場合、'B.*^A' と '^B.*A' を結合して '.*AB.*' を生み出せる
        ans += min(num_start_b_not_end_a, num_not_start_b_end_a)
    else:
        # 'B.*A' がある場合、その後に 'B.*^A' を結合するか、その前に '^B.*A' を結合する
        # 'B.*^A' も '^B.*A' もない場合、 'B.*A' 同士で結合させる
        if num_start_b_not_end_a + num_not_start_b_end_a > 0:
            ans += num_start_b_end_a + min(num_start_b_not_end_a, num_not_start_b_end_a)
        else:
            ans += num_start_b_end_a - 1
    return ans

N = int(input())
S = [input() for _ in range(N)]
print(c_ab_substrings(N, S))

Submission status: Wrong Answer

Solution code:
def c_ab_substrings(N, S):
    """
    N個の文字列を好きな順序で並べたあと、連結して1つの文字列を作る。
    作った文字列に 'AB' という部分文字列が含まれる個数として
    ありうる値のうち最大のものを求めよ。
    """

    num_start_b = 0
    num_end_a = 0
    ans = 0
    for s in S:
        if s[0] == 'B':
            num_start_b += 1
        if s[-1] == 'A':
            num_end_a += 1
        if 'AB' in s:
            ans += 1
    ans += min(num_start_b, num_end_a)
    return ans

N = int(input())
S = [input() for _ in range(N)]
print(c_ab_substrings(N, S))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []

counter = 0
alist = 0
blist = 0
ablist = 0

for i in range(n):
    a.append(str(input()))

for i in a:
    counter += i.count('AB')
    if i[-1] == 'A':
        alist += 1
    if i[0] == 'B':
        blist += 1
    if i[-1] == 'A' and i[0] == 'B':
        ablist += 1

if alist < blist:
    counter += alist
else:
    counter += blist

if alist == ablist and blist == ablist:
    print(counter - 1)
else:
    print(counter)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = []
for i in range(N):
    s.append(input())
count = 0
bcount = 0
acount = 0
bacount = 0
for j in s:
    count += j.count("AB")
    if j[0] == "B" and j[-1] == "A":
        bacount += 1
    elif j[0] == "B":
        bcount += 1
    elif j[-1] == "A":
        acount += 1
if min(bcount,acount) >= bacount:
    count += min(bcount,acount) + bacount
    print(count)
else:
    count += min(bcount,acount)*2
    if abs(acount-bcount) > 0 and bacount - min(bcount,acount) > 0:
        count += min(abs(acount-bcount),bacount - min(bcount,acount))
        print(count)

Submission status: Wrong Answer

Solution code:
headB = 0
tailA = 0
both = 0
c = 0
for i in range(int(input())):
    t = input()
    if t[0] =='B' and t[-1]=='A':
        both+=1
    elif t[0] == 'B':
        headB+=1
    elif t[-1] == 'A':
        tailA +=1
    c+=t.count('AB')
cur = sorted([tailA,headB])
# print(c,'Cur:',cur)
# print(c,'A:',tailA,headB)
if headB!=tailA:
    print(c+min(headB,tailA)+both)
else:
    print(c+tailA+max(both-1,0))

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

N = int(input())
A, B, AB = 'A', 'B', 'AB'
endA, startB, startB_endA = 0, 0, 0
ans = 0

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

    if S[0] == B:
        startB += 1

        if S[-1] == A:
            startB -= 1
            startB_endA += 1

    elif S[-1] == A:
        endA += 1
    

    for i in range(1,len(S)):
        if S[i-1] + S[i] == AB:
            ans += 1


if startB_endA == 0:
    ans = min(endA, startB)

elif endA == 0 and startB == 0:
    ans = max(0, startB_endA - 1)

else:
    ans = startB_endA + min(endA, startB)

print(ans)

Submission status: Wrong Answer

Solution code:
headB = 0
tailA = 0
both = 0
c = 0
for i in range(int(input())):
    t = input()
    if t[:2] == 'AB':
        c+=1
        temp = 'ZZ'+t[2:]
        if temp[-1] == 'A':
            tailA +=1
        c += temp.count('AB')
    elif 'AB' in t[-2:]:
        c +=1
        t = t[0:-2]+'ZZ'
        if t[0] == 'B':
            headB+=1
        c+= t.count('AB')
    elif t[0]=='B' and t[-1]=='A':
        both+=1
        c+=t.count('AB')
    else:
        if t[0]== 'B':
            headB +=1
        elif t[-1] == 'A':
            tailA +=1
        c+=t.count('AB')
cur = sorted([tailA,headB])
# print('Cur:',cur)
# print(c,'A:',tailA,headB)
if cur[0]==cur[1]:
    print(c+cur[0]+max(both-1,0))
else:
    print(c+cur[0]+both)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

N = int(input())
A, B, AB = 'A', 'B', 'AB'
endA, startB, startB_endA = 0, 0, 0
ans = 0

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

    if S[0] == B:
        startB += 1

        if S[-1] == A:
            startB -= 1
            startB_endA += 1

    elif S[-1] == A:
        endA += 1
    

    for i in range(1,len(S)):
        if S[i-1] + S[i] == AB:
            ans += 1


tmp = min(endA, startB)
ans += tmp
endA -= tmp
startB -= tmp

if max(endA, startB) > startB_endA:
    ans += startB_endA

else:
    tmp = max(endA, startB)
    ans +=tmp
    startB_endA -= tmp
    ans += min(startB_endA, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
end_a = 0
start_b = 0
a_and_b = 0
count = 0
for i in range(N):
    s = input()
    count += s.count('AB')
    s.replace('AB', '')
    if s[0] == 'B' and s[-1] == 'A':
        a_and_b += 1
    elif s[0] == 'B':
        start_b += 1
    elif s[-1] == 'A':
        end_a += 1



result = min(start_b+a_and_b, end_a+a_and_b) + count
print(result)

Submission status: Wrong Answer

Solution code:
N = int(input())
A, B, AB = 'A', 'B', 'AB'
endA, startB = 0, 0
ans = 0

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

    if S[0] == B:
        startB += 1

    if S[-1] == A:
        endA += 1
    

    for i in range(1,len(S)):
        if S[i-1] + S[i] == AB:
            ans += 1

ans += min(endA, startB)

print(ans)

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):
    count += s[i].count("AB")
    if s[i][-1] == "A" and s[i][0] != "B":
        count_a += 1
    if s[i][0] == "B" and s[i][-1] != "A":
        count_b += 1
    if s[i][0] == "B" and s[i][-1] == "A":
        count_ab += 1

# print([count,count_a,count_b,count_ab])

if n == 0:
    print(count+min([count_a,count_b]))
else:
    if count_a + count_b == 0:
        print(count+count_ab-1)
    else:
        print(count+min([count_a,count_b])+count_ab)

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 + min(last_a, start_b))
# elif reversible == 0:
#     print(res + min(last_a, start_b))
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())
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 + min(last_a, start_b))
elif reversible == 0:
    print(res + min(last_a, start_b))
else:
    print(res + min(last_a, start_b) )

# 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())
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 + min(last_a, start_b))
elif reversible == 0:
    print(res + min(last_a, start_b))
else:
    print(res + min(last_a, start_b) + reversible)

Submission status: Wrong Answer

Solution code:
headA = 0
headB = 0
tailA = 0
tailB = 0
both = 0
c = 0
for i in range(int(input())):
    t = input()
    # print('Input',t)
    if t[:2] == 'AB':
        c+=1
        temp = 'ZZ'+t[2:]
        if 'AB' in temp[-2:]:
            c += temp.count('AB')
        elif temp[-1] == 'A':
            tailA +=1
            c += temp.count('AB')
        else:
            c += temp.count('AB')
    elif 'AB' in t[-2:]:
        c +=1
        t = t[0:-2]+'ZZ'
        if t[0] == 'B':
            headB+=1
        c+= t.count('AB')
    elif t[0]=='B' and t[-1]=='A':
        both+=1
        c+=t.count('AB')
    else:
        if t[0]== 'B':
            headB +=1
        elif t[-1] == 'A':
            tailA +=1
        c+=t.count('AB')
if both>1:
    c+=both-1
    both=1
a = [tailA,headB]
a.sort()
need = a[1] - a[0]
if need>0:
    a[0]+=both
print(c+a[0])

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)
elif reversible == 0:
    print(res + min(last_a, start_b))
else:
    print(res + min(last_a, start_b) + reversible)

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 startB + stopA != 0:
    ans = AB + min(startB, stopA) + BtoA
else:
    ans = AB + min(startB, stopA) + BtoA - 1
print(ans)