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:
#!/usr/bin/env python3
import sys
import itertools
INF = float("inf")


def solve(N: int, s: "List[str]"):

    ABcount = 0
    Aend = 0
    Bstart = 0
    AendBstart = 0
    for i in range(N):
        if "AB" in s[i]:
            ABcount += 1

        if s[i][0] == "B" and s[i][-1] == "A":
            AendBstart += 1
        elif s[i][0] == "B":
            Bstart += 1
        elif s[i][-1] == "A":
            Aend += 1

    if AendBstart > 1:
        ABcount += AendBstart - 1
        AendBstart = 1
    if Aend > 0 or Bstart > 0:
        ABcount += min(Aend, Bstart)+AendBstart
    print(ABcount)

    # 愚直
    # print("愚直")
    # ans = []
    # for kumi in itertools.permutations(s, N):
    #     ans.append("".join(kumi).count("AB"))
    # print(max(ans))
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(N)]  # type: "List[str]"
    solve(N, s)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import itertools
INF = float("inf")


def solve(N: int, s: "List[str]"):

    ABcount = 0
    Aend = 0
    Bstart = 0
    AendBstart = 0
    for i in range(N):
        if "AB" in s[i]:
            ABcount += 1
        if s[i][0] == "B":
            if s[i][-1] == "A":
                AendBstart += 1
            else:
                Bstart += 1
        elif s[i][-1] == "A":
            Aend += 1

    if AendBstart > 1:
        ABcount += AendBstart - 1
        AendBstart = 1
    if Aend > 0 or Bstart > 0:
        ABcount += min(Aend, Bstart)+AendBstart
    print(ABcount)

    # 愚直
    # print("愚直")
    # ans = []
    # for kumi in itertools.permutations(s, N):
    #     ans.append("".join(kumi).count("AB"))
    # print(max(ans))
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(N)]  # type: "List[str]"
    solve(N, s)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
i = 0
b_top = 0
a_end = 0
ab_count = 0
while i < n:
  str = input()
  if str[0] == 'B':
    b_top += 1
  if str[-1] == 'A':
    a_end += 1
  ab_count += str.count('AB')
  i += 1

print(ab_count + min([b_top, a_end]))

Submission status: Wrong Answer

Solution code:
n = int(input())
i = 0
b_top = 0
a_end = 0
ab_count = 0
while i < n:
  str = input()
  if str[0] == 'B':
    b_top += 1
  if str[-1] == 'A':
    a_end += 1
  if 'AB' in str:
    ab_count += 1
  i += 1

print(ab_count + min([b_top, a_end]))

Submission status: Wrong Answer

Solution code:
def check(S):
    ans = 0
    for i in range(1,len(S)):
        if S[i-1] == 'A' and S[i] == 'B':
            ans += 1
    return ans
 
N = int(input())
s = []
a = 0
b = 0
ab = 0
 
ans = 0
 
for i in range(N):
    S = input()
    ans += check(S)
    if S[0] == 'B' or S[-1] == 'A':
        s.append(S)
        if S[0] == 'B' and S[-1] != 'A':
            b += 1
        elif S[0] != 'B' and S[-1] == 'A':
            a += 1
        else:
            ab += 1
 
ans += min(a, b)
if ab > 1:
    ans += ab - 1
    ab = 1
 
abc = abs(a - b)
 
ans += min(abc, ab)
 
 
print(ans)

Submission status: Wrong Answer

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

# 先頭が B ではなく、末尾が A な文字列
TxBA = [s[n] for n in range(N) if s[n][0] != "B" and s[n][-1] == "A"]

# 先頭が B で、末尾が A な文字列
TBBA = [s[n] for n in range(N) if s[n][0] == "B" and s[n][-1] == "A"]

# 先頭が B で、末尾が A ではない文字列
TBBx = [s[n] for n in range(N) if s[n][0] == "B" and s[n][-1] != "A"]

# 上記を除き中に "AB" が存在する文字列
kouho = TxBA + TBBA + TBBx
ss = s.copy()
for k in kouho:
    ss.remove(k)
    
str = ""
while (len(TxBA)):
    str += TxBA.pop()
    while (len(TBBA)):
        str += TBBA.pop()
    if (len(TBBx)):
        str += TBBx.pop()

while (len(TBBx)):
    str += TBBx.pop()

while (len(ss)):
    str += ss.pop()

print(str.count("AB"))

Submission status: Wrong Answer

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

# 先頭が B ではなく、末尾が A な文字列
TxBA = [s[n] for n in range(N) if s[n][0] != "B" and s[n][-1] == "A"]

# 先頭が B で、末尾が A な文字列
TBBA = [s[n] for n in range(N) if s[n][0] == "B" and s[n][-1] == "A"]

# 先頭が B で、末尾が A ではない文字列
TBBx = [s[n] for n in range(N) if s[n][0] == "B" and s[n][-1] != "A"]

# 上記を除き中に "AB" が存在する文字列
kouho = TxBA + TBBA + TBBx
ss = s.copy()
for k in kouho:
    ss.remove(k)
    
str = ""
while (len(TxBA)):
    str += TxBA.pop()
    while (len(TBBA)):
        str += TBBA.pop()
    if (len(TBBx)):
        str += TBBx.pop()

while (len(TBBx)):
    str += TBBx.pop()

while (len(ss)):
    str += ss.pop()

str.count("AB")

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()
	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)

if(both == 0):
	ans += min(a_a,b_a)
elif(a_a + b_a > 0):
	ans += (both + min(a_a,b_a))
elif(a_a + b_a == 0):
	ans += (both -1)

print(ans)

Submission status: Wrong Answer

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

XA = 0
BX = 0
BXA = 0
cnt = 0
for Si in S:
    cnt += Si.count("AB")

    tp = (Si[0] == "B")*1 + (Si[-1] == "A")*2
    if tp == 1:
        BX += 1
    elif tp == 2:
        XA += 1
    elif tp == 3:
        BXA += 1

if BXA > 0:
    cnt += BXA - 1
    BXA = 1

if BXA > 0:
    BXA -= 1
    BX += 1
    XA += 1

cnt += min(BX, XA)

print(cnt)

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()
	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)

if(both != 0):
	ans += min(a_a,b_a)
elif(a_a + b_a > 0):
	ans += both + min(a_a,b_a)
elif(a_a + b_a == 0):
	ans += both -1

print(ans)

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()
	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)

if(both == 0 ):
	ans += min(a_a,b_a)
elif(a_a + b_a > 0):
	ans += both + min(a_a,b_a)
elif(a_a + b_a == 0):
	ans += both -1

print(ans)

Submission status: Wrong Answer

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


if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
n,*t=open(0).read().split()
n=int(n)
a=0
b=0
c=0
ans=0
for i in t:
    if i[0]=="B"and i[-1]=="A":
        a+=1
    if i[0]=="B":
        b+=1
    if i[-1]=="A":
        c+=1
    ans+=i.count("AB")
if a>0 and b>0 and c>0:
    ans+=a+1
    a=0
    b-=1
    c-=1
if a>0 and b>0:
    ans+=a
    a=0
    b-=1
if a>0 and c>0:
    ans+=a
    a=0
    c-=1
if a>0:
    ans+=a-1
    a=0
if b>0 and c>0:
    ans+=min(b,c)
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
ans=A=B=0
for _ in range(N):
    S=input()
    ans+=S.count("AB")
    if S[0]=='B':B+=1
    if S[-1]=='A':A+=1
print(ans+(A-1 if A==B==N else min(A,B)))

Submission status: Wrong Answer

Solution code:
N = int(input())
ans=cnt_A=cnt_B=0
for _ in range(N):
    S=input()
    ans+=S.count("AB")
    if S[0]=='B':cnt_B+=1
    if S[-1]=='A':cnt_A+=1
print(ans+min(cnt_A, cnt_B))

Submission status: Wrong Answer

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


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

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

print(res)

Submission status: Wrong Answer

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

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

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

ans = AB

minab = min(start_B,end_A,sB_eA)
start_B -= minab
end_A -= minab
sB_eA -= minab
ans += minab*2
 
if sB_eA == 0:
    ans += min(start_B,end_A)
else:
    ans += sB_eA
print(ans)

Submission status: Wrong Answer

Solution code:
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 + min(cnt_a, cnt_b) + cnt_ba
    print(cnt)
else:
    cnt = cnt_ab + min(cnt_a, cnt_b) + cnt_ba - 1
    print(cnt)

Submission status: Wrong Answer

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

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

end_A = 0
start_B = 0
sB_eA = 0
ans = 0

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

ans += min(start_B,end_A)
        
if start_B == 0 and end_A == 0:
    ans += max(0,sB_eA-1)
else:
    ans += sB_eA
print(ans)

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=c
_c+=max(0,c3-1)
_c1=c1
_c2=c2
_c3=c3
if _c3>0 and _c1>0:
  _c+=1
  _c1-=1
if _c3>0 and _c2>0:
  _c+=1
  _c2-=1
if _c1<_c2:
  _c2-=_c1
  _c+=_c1
  if _c2>0 and _c3>0:
    _c+=1
elif _c1==_c2:
  if _c3>0:
    _c+=1
    _c1-=1
    _c2-=1
  _c+=_c1
else:
  _c1-=_c2
  _c+=_c2
  if _c1>0 and _c3>0:
    _c+=1

# print(_c)

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

print(max(c,_c))

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=c
_c+=max(0,c3-1)
_c1=c1
_c2=c2
_c3=c3
if _c3>0 and _c1>0:
  _c+=1
  _c1-=1
if _c3>0 and _c2>0:
  _c+=1
  _c2-=1
if _c1<_c2:
  _c2-=_c1
  _c+=_c1
  if _c2>0 and _c3>0:
    _c+=1
elif _c1==_c2:
  if _c3>0:
    _c+=1
    _c1-=1
    _c2-=1
  _c+=_c1
else:
  _c1-=_c2
  _c+=_c2
  if _c1>0 and _c3>0:
    _c+=1

print(_c)

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

# print(max(c,_c))

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=c
_c+=max(0,c3-1)
_c1=c1
_c2=c2
if c1>0:
  _c+=1
  _c1-=1
if c2>0:
  _c+=1
  _c2-=1
if c1<c2:
  _c2-=c1
  _c+=c1
  if _c2>0 and c3>0:
    _c+=1
elif c1==c2:
  if c3>0:
    _c+=1
    _c1-=1
    _c2-=1
  _c+=_c1
else:
  _c1-=c2
  _c+=c2
  if _c1>0 and c3>0:
    _c+=1

print(_c)

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

# print(max(c,_c))

Submission status: Wrong Answer

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

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

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

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

ans = AB
   
if sB_eA == 0:
    ans += min(start_B,end_A)
elif sum([start_B,end_A]) > 0:
    ans += sB_eA+min(start_B,end_A)
else:
    ans += sB_eA-1
    
print(ans)

Submission status: Wrong Answer

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

s = 0

f_list = list()
b_list = list()
for i in s_list:
    s += i.count('AB')
    f_list.append([i][0])
    b_list.append([i][-1])

f = f_list.count('B')
b = b_list.count('A')
s += min(f,b)

test = 0
for j in s_list:
    if (([j][0]=='B')and([j][-1]=='A')) and (([j][0]!='B') and ([j][-1]!='A')):
        test += 1

if test == N:
    s -= 1
        

print(s)

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=c
_c+=max(0,c3)
_c1=c1
_c2=c2
if c1<c2:
  _c2-=c1
  _c+=c1
  if _c2>0 and c3>0:
    _c+=1
elif c1==c2:
  if c3>0:
    _c+=1
    _c1-=1
    _c2-=1
  _c+=_c1
else:
  _c1-=c2
  _c+=c2
  if _c1>0 and c3>0:
    _c+=1

# print(_c)

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

print(max(c,_c))

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 m == 0 and l == 0:
  print(int(count + (n - 1)))
else:
  print(int(count + min(n+m, n+l)))

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=c
_c+=max(0,c3-1)
_c1=c1
_c2=c2
if c1<c2:
  _c2-=c1
  _c+=c1
  if _c2>0 and c3>0:
    _c+=1
elif c1==c2:
  if c3>0:
    _c+=1
    _c1-=1
    _c2-=1
  _c+=_c1
else:
  _c1-=c2
  _c+=c2
  if _c1>0 and c3>0:
    _c+=1

# print(_c)

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

print(max(c,_c))

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
 
print(int(count + min(n+m, n+l)))

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=c
_c+=max(0,c3-1)
_c1=c1
_c2=c2
if c1<c2:
  _c2-=c1
  _c+=c1
  if _c2>0 and c3>0:
    _c+=1
elif c1==c2:
  if c3>0:
    _c+=1
    _c1-=1
    _c2-=1
  _c+=_c1
else:
  _c1-=c2
  _c+=c2
  if _c1>0 and c3>0:
    _c+=1

print(_c)

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

# print(max(c,_c))

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=c
_c+=max(0,c3-1)
_c1=c1
_c2=c2
if c1<c2:
  _c2-=c1
  _c+=c1
  if _c2>0 and c3>0:
    _c+=1
elif c1==c2:
  if c3>0:
    _c+=1
  _c+=c1
else:
  _c1-=c2
  _c+=c2
  if _c1>0 and c3>0:
    _c+=1

print(_c)

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

# print(max(c,_c))

Submission status: Wrong Answer

Solution code:
N = int(input())

n_ab = 0
n_b = 0
n_a = 0
n_both = 0
for i in range(N):
    S = input()
    # 前後情報とABの数だけ調べておく
    n_ab += S.count("AB")
    if S[-1] == "A" and S[0] != "B":
        n_a += 1
    if S[0] == "B" and S[-1] != "A":
        n_b += 1
    if S[-1] == "A" and S[0] == "B":
        n_both += 1       
ans = n_ab+min(n_b+n_both,n_a+n_both)
if n_a==0 and n_b==0:
    ans -= 1

print(int(ans))

Submission status: Wrong Answer

Solution code:
N = int(input())

n_ab = 0
n_b = 0
n_a = 0
n_both = 0
if N == 1:
    S = input()
    ans = S.count("AB")
else:
    for i in range(N):
        S = input()
        # 前後情報とABの数だけ調べておく
        n_ab += S.count("AB")
        if S[-1] == "A" and S[0] != "B":
            n_a += 1
        if S[0] == "B" and S[-1] != "A":
            n_b += 1
        if S[-1] == "A" and S[0] == "B":
            n_both += 1       
    ans = n_ab+min(n_b+n_both,n_a+n_both)
    if n_a==0 and n_b==0:
        ans -= 1

print(int(ans))

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=c
_c+=max(0,c3-1)
_c1=c1
_c2=c2
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

print(_c)

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

# print(max(c,_c))

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=c
_c+=max(0,c3-1)
_c1=c1
_c2=c2
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
    c3-=c1
    c+=c3
  elif c1==c2:
    c+=c1*2
    c3-=c1
    c+=max(0,c3-1)
  else:
    c+=c2*2
    c3-=c2
    c+=c3
elif c3>=min(c1,c2) and c3<=max(c1,c2):
  if c1<c2:
    c+=c1*2
    c2-=c1
    c3-=c1
    c+=c3
  else:
    c+=c2*2
    c1-=c2
    c3-=c2
    c+=c3
else:
  c+=c3*2
  c1-=c3
  c2-=c3
  c+=min(c1,c2)

print(max(c,_c))

Submission status: Wrong Answer

Solution code:
# C

N = int(input())
cnt = 0
s = []
both = []
a_ = []
b_ = []
for i in range(N):
    s_i = input()
    if 'AB' in s_i:
        cnt+= 1

    if s_i.startswith('B') and s_i.endswith('A'):
        both.append(s_i)
    elif s_i.startswith('B'):
        b_.append(s_i)
    elif s_i.endswith('A'):
        a_.append(s_i)
        
if len(both) > 0:
    if len(a_) and len(b_):
        cnt += (len(both) + min(len(a_), len(b_)))
    else:
        cnt += len(both) - 1
else:
    cnt += min(len(a_), len(b_))
    
print(cnt)

Submission status: Wrong Answer

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

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)))
elif n > m and n > l:
  print(int(count + 2 * (n - max(m, l) + m + l)))
else:
  print(int(count + min(n, m) + min(n, l)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())

n_fin_is_A = 0
n_ini_is_B = 0
n_each_ab = 0

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

_min = min(n_fin_is_A, n_ini_is_B)
if _min > N-1:
    _min += N-1

print(n_each_ab + _min)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

N = int(input())
cnt = 0
s = []
both = []
a_ = []
b_ = []
for i in range(N):
    s_i = input()
    if 'AB' in s_i:
        cnt+= 1

    if s_i.startswith('B') and s_i.endswith('A'):
        both.append(s_i)
    elif s_i.startswith('B'):
        b_.append(s_i)
    elif s_i.endswith('A'):
        a_.append(s_i)
    else:
        s.append(s_i)
        
if len(both):
    if len(a_) and len(b_):
        cnt += (len(both) + min(len(a_), len(b_)))
    else:
        cnt += len(both) - 1
else:
    cnt += min(len(a_), len(b_))
    
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
B_start_cnt = 0
A_end_cnt = 0
AB_cnt = 0
BA_cnt = 0
for i in range(N):
    s = input()
    if s[0] == "B" and s[-1] == "A":
        BA_cnt += 1
    if s[0] == "B" and s[-1] != "A":
        B_start_cnt += 1
    if s[-1] == "A" and s[0] == "B":
        A_end_cnt += 1
    AB_cnt += s.count('AB')
ans = AB_cnt

if BA_cnt == 0:
    ans += min(A_end_cnt,B_start_cnt)
elif max(A_end_cnt,B_start_cnt)>0:
    ans += BA_cnt+min(A_end_cnt,B_start_cnt)
else:
    ans += BA_cnt-1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

count_ab = 0
S_tail_a_head_b = 0
S_tail_a = 0
S_head_b = 0
for _ in range(N):
    s = input()
    count_ab += s.count('AB')

    if s[0] != 'B' and s[-1] != 'A':
        continue

    if s[0] == 'B' and s[-1] == 'A':
        S_tail_a_head_b += 1
    elif s[0] == 'B':
        S_head_b += 1
    elif s[-1] == 'A':
        S_tail_a += 1

print('BxxA', S_tail_a_head_b)
print('xxxA', S_tail_a)
print('Bxxx', S_head_b)

if S_tail_a_head_b == 0:
    count_ab += min(S_tail_a, S_head_b)
else:
    count_ab += S_tail_a_head_b - 1
    if S_tail_a + S_head_b >= 1:
        count_ab += min(S_tail_a, S_head_b) + 1

print(count_ab)

Submission status: Wrong Answer

Solution code:
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    ans = 0
    ba = 0  # Num. of str that the first letter is B and the last one is A.
    xa = 0  # Num. of str that the last letter is A and the first one is not B.
    bx = 0  # Num. of str that the first letter is B and the last one is not A.
    for _ in range(N):
        s = str(input().strip())

        # Count the number of AB contained in the initial strings.
        for i in range(len(s)):
            if s[i:i+2] == 'AB':
                ans += 1

        if s[0] == 'B' and s[-1] == 'A':
            ba += 1
        elif s[-1] == 'A':
            xa += 1
        elif s[0] == 'B':
            bx += 1

    if max(xa, bx) == 0:
        # N of ba make N-1 new "AB".
        # BxxA + BxxA + ... + BxxA -> BxxABxxAB...ABxxA
        ans += ba - 1

    elif ba == 0:
        # One xa ans one bx make one new "AB".
        #   xa + bx -> xABx
        ans += min(xa, bx)

    else:  # There is at least one bx or ax and at least one ba.
        # N of ba make N-1 new "AB" and a ba.
        #   ba + ba + ... + ba -> baba...ba = ba
        # The last remaining ba and bx/ax make a new "AB" and a new bx/ax.
        #   ba + bx -> bABx = bx,  xa + ba -> xABa = xa
        # Then, bx and xa make a new "AB".
        #   xa + bx -> xABx 
        ans += ba + min(xa, bx)

    return ans


if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
N = int(input())

count_ab = 0
S_tail_a_head_b = 0
S_tail_a = 0
S_head_b = 0
for _ in range(N):
    s = input()
    count_ab += s.count('AB')

    if s[0] != 'B' and s[-1] != 'A':
        continue

    if s[0] == 'B' and s[-1] == 'A':
        S_tail_a_head_b += 1
    elif s[0] == 'B':
        S_head_b += 1
    elif s[-1] == 'A':
        S_tail_a += 1

count_ab += min(S_tail_a, S_head_b)
if S_tail_a_head_b >= 1:
    count_ab += S_tail_a_head_b - 1
    if S_tail_a != S_head_b:
        count_ab += 2


print(count_ab)

Submission status: Wrong Answer

Solution code:
from sys import stdin
N=int(stdin.readline().rstrip())
 
data=[]
ans=0
A=0
B=0
ab=0
for i in range(N):
    S=input()
    ans+=S.count("AB")
    if S[0] == "B":
        B+=1
    if S[-1] == "A":
        A+=1
    if S[-1] == "A" and S[0] == "B":
        ab+=1
        
if A == B == ab:
    print(ans+ab-1)
else:
    print(ans+min(A,B))

Submission status: Wrong Answer

Solution code:
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    ans = 0
    ba = 0  # Num. of str that the first letter is B and the last one is A.
    xa = 0  # Num. of str that the last letter is A and the first one is not B.
    bx = 0  # Num. of str that the first letter is B and the last one is not A.
    for _ in range(N):
        s = str(input().strip())

        # Count the number of AB contained in the initial strings.
        for i in range(len(s)):
            if s[i:i+2] == 'AB':
                ans += 1

        if s[0] == 'B' and s[-1] == 'A':
            ba += 1
        elif s[-1] == 'A':
            xa += 1
        elif s[0] == 'B':
            bx += 1

    if max(xa, bx) == 0:
        # N of ba make N-1 new "AB".
        # BxxA + BxxA + ... + BxxA -> BxxABxxAB...ABxxA
        ans += ba - 1

    else:  # There is at least one bx or ax.
        # N of ba make N-1 new "AB" and a ba.
        #   ba + ba + ... + ba -> baba...ba = ba
        # The last remaining ba and bx/ax make a new "AB" and a new bx/ax.
        #   ba + bx -> bABx = bx,  xa + ba -> xABa = xa
        # Then, bx and xa make a new "AB".
        #   xa + bx -> xABx 
        ans += ba + min(xa, bx)

    return ans


if __name__ == '__main__':
    print(main())