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 python
# -*- coding: utf-8 -*-

from collections import deque
import heapq
import math
import bisect


def main():
    N = int(input())
    S = [input() for i in range(N)]

    a = b = ab = 0
    ans = 0
    for s in S:
        n = len(s)
        for j in range(n-1):
            if s[j] == "A" and s[j+1] == "B":
                ans += 1
        if s[0] == "B" and s[-1] == "A":
            ab += 1
        elif s[0] == "B":
            b += 1
        elif s[-1] == "A":
            a += 1

    if a == 0 and b == 0:
        print(ans+ab-1)
    else:
        print(ans+ab+min(a, b))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())

count = 0
ba = 0
b = 0
a = 0

for _ in range(N):
    s = input()
    if s[0] == "B":
        if s[-1] == "A":
            ba += 1
        else:
            b += 1
    elif s[-1] == "A":
        a += 1

    n = len(s)
    for i in range(n-1):
        if s[i] == "A" and s[i+1] == "B":
            count += 1

# BA
count += max(ba-1, 0)

#if a >= 1 and b >= 1:
count += min(a, b)        

if ba >= 1 and a != b:
    count += 1


print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())

count = 0
ba = 0
b = 0
a = 0

for _ in range(N):
    s = input()
    if s[0] == "B":
        if s[-1] == "A":
            ba += 1
        else:
            b += 1
    elif s[-1] == "A":
        a += 1

    n = len(s)
    for i in range(n-1):
        if s[i] == "A" and s[i+1] == "B":
            count += 1

# BA
count += max(ba-1, 0)

if a >= 1 and b >= 1:
    count += min(a, b)        

if ba >= 1 and a != b:
    count += 1


print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
s=[]
for i in range(N):
	s.append(input())
count=0
for i in range(N):
	index = s[i].find("AB")
	if index != -1:
		count += 1
a=0
b=0
ba=0


for i in range(N):
	if s[i][-1]=="A" and s[i][0]=="B":
		ba+=1
	elif s[i][-1] == "A" and s[i][0] != "B":
		a+=1
	elif s[i][-1] != "A" and s[i][0] == "B":
		b+=1
# print("a",a)
# print(b)
# print(ba)
"""
one=min(a,b)
two=0
if ba>0:
	two=ba-1

	if abs(a-b)>0:
		two=ba-1+1
print(one+two+count)
"""

if ba==0:
	count+=min(a,b)
else:
	if a==0 and b==0:
		count += (ba - 1)
	elif a == 0 or b == 0:
		count += ba
	else:
		count +=(ba + min(a,b))
		# min(a, b) : -A,B-同士が結合する数
		# B-AはB-A同士で結合する 数：ba-1
		# 結合したB-Aの最初はB，最後はAになっているので，
		# そのどちらかに-A,B-が結合したものがくっつくき，また結合が1つできる 数：ba-1+1 = ba
# if a >= 1 and b >= 1:
# 	count += ba + min(a, b)
# elif a >= 1 or b >= 1:
# 	count += ba
# else:
# 	count += max(0, ba - 1)

print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
s=[]
for i in range(N):
	s.append(input())
count=0
for i in range(N):
	index = s[i].find("AB")
	if index != -1:
		count += 1
a=0
b=0
ba=0


for i in range(N):
	if s[i][-1]=="A" and s[i][0]=="B":
		ba+=1
	elif s[i][-1] == "A" and s[i][0] != "B":
		a+=1
	elif s[i][-1] != "A" and s[i][0] == "B":
		b+=1
# print("a",a)
# print(b)
# print(ba)
"""
one=min(a,b)
two=0
if ba>0:
	two=ba-1

	if abs(a-b)>0:
		two=ba-1+1
print(one+two+count)
"""

if ba==0:
	count+=min(a,b)
elif a==0 and b==0:
	count += (ba - 1)
#	elif a == 0 or b == 0:
#		count += ba
else:
	count +=(ba + min(a,b))
		# min(a, b) : -A,B-同士が結合する数
		# B-AはB-A同士で結合する 数：ba-1
		# 結合したB-Aの最初はB，最後はAになっているので，
		# そのどちらかに-A,B-が結合したものがくっつくき，また結合が1つできる 数：ba-1+1 = ba

print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
s=[]
for i in range(N):
	s.append(input())
count=0
for i in range(N):
	index = s[i].find("AB")
	if index != -1:
		count += 1
a=0
b=0
ba=0


for i in range(N):
	if s[i][-1]=="A" and s[i][0]=="B":
		ba+=1
	elif s[i][-1] == "A":
		a+=1
	elif s[i][0] == "B":
		b+=1
# print("a",a)
# print(b)
# print(ba)
"""
one=min(a,b)
two=0
if ba>0:
	two=ba-1

	if abs(a-b)>0:
		two=ba-1+1
print(one+two+count)
"""

if ba==0:
	count+=min(a,b)
elif a==0 and b==0:
	count += (ba - 1)
#	elif a == 0 or b == 0:
#		count += ba
else:
	count +=(ba + min(a,b))
		# min(a, b) : -A,B-同士が結合する数
		# B-AはB-A同士で結合する 数：ba-1
		# 結合したB-Aの最初はB，最後はAになっているので，
		# そのどちらかに-A,B-が結合したものがくっつくき，また結合が1つできる 数：ba-1+1 = ba

print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=['' for i in range(n)]
for i in range(n):
  s[i]=input()
#print(n,s)
cnt=0
cnta=0
cntb=0
cntab=0
ans=0
for i in range(n):
    cnt+=s[i].count('AB')
    if s[i][-1]=='A' and s[i][0]=='B':
        cntab+=1
    elif s[i][-1]=='A':
        cnta+=1
    elif s[i][0]=='B':
        cntb+=1

if cntab==0:
  ans=cnt+min(cnta,cntb)
elif cnta+cntb > 0:
  ans=cnt+cntab+min(cnta,cntb)

if cnta==0 and cntb==0:
  ans=cnt+cntab-1
  
print(ans)

Submission status: Wrong Answer

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

# 先頭がBである／ではない、末尾がAである／ではない文字列の個数を数え上げる
a = sum(t[0] != 'B' and t[-1] == 'A' for t in s)
b = sum(t[0] == 'B' and t[-1] != 'A' for t in s)
ba = sum(t[0] == 'B' and t[-1] == 'A' for t in s)

# 最初に、先頭文字がBではない、かつ、末尾文字がAの文字列を先頭におく（そのような文字列が存在すれば）
# 次に、先頭文字がB、かつ、末尾文字がAの文字列を可能な限り連結する
# 次に、（先頭文字がB、かつ、末尾文字がAでない文字列）と（先頭文字がBではない、かつ、末尾文字がAの文字列）を交互に連結する
# 最後に、余っている文字列を適当に連結する
ans = sum(t.count('AB') for t in s) + \
    (ba if a > 0 else max(0, ba - 1)) + \
    (min(a, b) if (a > 0) ^ (ba > 0) else min(max(0, a - 1), b))

# 出力
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=['' for i in range(n)]
for i in range(n):
  s[i]=input()
#print(n,s)
cnt=0
cnta=0
cntb=0
cntab=0
ans=0
for i in range(n):
    cnt+=s[i].count('AB')
    if s[i][-1]=='A' and s[i][0]=='B':
        cntab+=1
    elif s[i][-1]=='A':
        cnta+=1
    elif s[i][0]=='B':
        cntb+=1

if cntab==0:
  ans=cnt+min(cnta,cntb)
else:
  ans=cnt+cntab+min(cnta,cntb)

if cnta==0 and cntb==0:
  ans=cnt+cntab-1
  
print(ans)

Submission status: Wrong Answer

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

# 先頭がBである／ではない、末尾がAである／ではない文字列の個数を数え上げる
a = sum(t[0] != 'B' and t[-1] == 'A' for t in s)
b = sum(t[0] == 'B' and t[-1] != 'A' for t in s)
ba = sum(t[0] == 'B' and t[-1] == 'A' for t in s)

# 最初に、先頭文字がBではない、かつ、末尾文字がAの文字列を先頭におく（そのような文字列が存在すれば）
# 次に、先頭文字がB、かつ、末尾文字がAの文字列を可能な限り連結する
# 次に、（先頭文字がB、かつ、末尾文字がAでない文字列）と（先頭文字がBではない、かつ、末尾文字がAの文字列）を交互に連結する
# 最後に、余っている文字列を適当に連結する
ans = sum(t.count('AB') for t in s) + \
    (ba if a > 0 else max(0, ba - 1)) + \
    (min(a + 1, b) if (a > 0) ^ (ba > 0) else min(a, b))

# 出力
print(ans)

Submission status: Wrong Answer

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

count_AB = 0
count_A = set()
count_B = set()

for i in range(N):
    _s = s[i]
    if _s[0] == "B":
        count_B.add(i)
    if _s[len(_s)-1] == "A":
        count_A.add(i)
    count_AB += len(re.findall("AB", _s))


inter = (count_A & count_B)
count_max = max(len(count_A), len(count_B))
cnt = 0
if len(inter) == count_max:
    cnt = len(inter) - 1
else:
    cnt = min(len(count_A), len(count_B))


print(count_AB + 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):
    cnt -= 1
if cntna == 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):
    cnt -= 1
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
nums = [0, 0, 0]
ans = 0
for i in range(N):
    s = input()
    ans += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        nums[0] += 1
    elif s[0] == 'B':
        nums[1] += 1
    elif s[-1] == 'A':
        nums[2] += 1
if nums[0] > 0:
    ans += nums[0]-1
if nums[1] != nums[2] and nums[0] > 0:
    ans += 1
print(ans+min(nums[1:]))

Submission status: Wrong Answer

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

count_AB = 0
count_A = 0
count_B = 0

for _s in s:
    if _s[0] == "B":
        count_B += 1
    if _s[len(_s)-1] == "A":
        count_A += 1
    count_AB += len(re.findall("AB", _s))

print(count_AB + min(count_A, count_B))

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) == cntna:
    cnt -= 1
print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
S=[list(input())for i in range(n)]
A_cnt=0
B_cnt=0
AB_cnt=0
for s in S:
        if S[0]=="B" and S[-1]=="A":
                AB_cnt+=1
        elif s[-1]=="A":
                A_cnt+=1
        elif s[0]=="B":
                B_cnt+=1

cnt=0
for i in range(n):
        S[i].append("T")
        for j in range(len(S[i])):
                if S[i][j]=="A" and S[i][j+1]=="B":
                        cnt+=1
if A_cnt==B_cnt==0 and 0<AB_cnt:
        print(AB_cnt-1+cnt)
else:
        print(cnt+min(A_cnt+AB_cnt,B_cnt+AB_cnt))

Submission status: Wrong Answer

Solution code:
n=int(input())
s=['' for i in range(n)]
for i in range(n):
  s[i]=input()
#print(n,s)
cnt=0
cnta=0
cntb=0
cntab=0
ans=0
for i in range(n):
    cnt+=s[i].count('AB')
    if s[i][-1]=='A' and s[i][0]=='B':
        cntab+=1
    elif s[i][-1]=='A':
        cnta+=1
    elif s[i][0]=='B':
        cntb+=1

ans=cnt+min(cnta+cntab,cntb+cntab)

if cnta==0 and cntb==0:
  ans=cnt+cntab-1
  
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
S=[list(input())for i in range(n)]
A_cnt=0
B_cnt=0
AB_cnt=0
for s in S:
        if S[0]=="B" and S[-1]=="A":
                AB_cnt+=1
        elif s[-1]=="A":
                A_cnt+=1
        elif s[0]=="B":
                B_cnt+=1

cnt=0
for i in range(n):
        S[i].append("T")
        for j in range(len(S[i])):
                if S[i][j]=="A" and S[i][j+1]=="B":
                        cnt+=1
if A_cnt==B_cnt==0 and 0<AB_cnt:
        print(AB_cnt-1+cnt)
else:
        print(cnt+min(A_cnt,B_cnt))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun May 12 12:43:59 2019

@author: Yamazaki Kenichi
"""

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

ans = 0
firstB, lastA, u = 0, 0, 0
for s in S:
    flg = False
    for c in s:
        if flg and c == "B":
            ans += 1
        flg = False
        if c == 'A':
            flg = True
    if s[0] == 'B':
        firstB += 1
    if s[-1] == 'A':
        lastA += 1
    if s[0] == 'B' and s[-1] == 'A':
        u += 1
ans += min(firstB,lastA)
ans -= 1 if u == firstB and u == lastA else 0
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
S=[list(input())for i in range(n)]
A_cnt=0
B_cnt=0
for s in S:
        if s[-1]=="A":
                A_cnt+=1
        if s[0]=="B":
                B_cnt+=1

cnt=0
for i in range(n):
        S[i].append("T")
        for j in range(len(S[i])):
                if S[i][j]=="A" and S[i][j+1]=="B":
                        cnt+=1
print(cnt+min(A_cnt,B_cnt))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun May 12 12:43:59 2019

@author: Yamazaki Kenichi
"""

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

ans = 0
firstB, lastA, u = 0, 0, 0
for s in S:
    flg = False
    for c in s:
        if flg and c == "B":
            ans += 1
        flg = False
        if c == 'A':
            flg = True
    if s[0] == 'B':
        firstB += 1
    if s[-1] == 'A':
        lastA += 1
    if s[0] == 'B' and s[-1] == 'A':
        u += 1
ans += min(firstB,lastA)
ans -= 1 if u == min(firstB,lastA) else 0
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun May 12 12:43:59 2019

@author: Yamazaki Kenichi
"""

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

ans = 0
firstB, lastA, u = 0, 0, 0
for s in S:
    flg = False
    for c in s:
        if flg and c == "B":
            ans += 1
        flg = False
        if c == 'A':
            flg = True
    if s[0] == 'B':
        firstB += 1
    if s[-1] == 'A':
        lastA += 1
    if s[0] == 'B' and s[-1] == 'A':
        u += 1
ans += min(firstB,lastA,u-1)
print(ans)

Submission status: Wrong Answer

Solution code:
check_AB = [0, 0, 0]

def count_ab_string(s):
    count = 0
    for i in range(len(s)-1):
        if s[i:i+2] == 'AB':
            count += 1
    return count

def check(s):
    global check_AB
    if s[-1] == 'A' and s[0] == 'B':
        check_AB[0] += 1 
    elif s[-1] == 'A':
        check_AB[1] += 1 
    elif s[0] == 'B':
        check_AB[2] += 1 

n = int(input())
S = [input() for i in range(n)]
AB_count = 0
for s in S:
    AB_count += count_ab_string(s)
    check(s)
    
AB_count += min(check_AB[0], check_AB[1])
AB_count += min(check_AB[0], check_AB[2])
AB_count += max(0, min(check_AB[1]-check_AB[0], check_AB[2]-check_AB[0]))
if check_AB[0]-check_AB[1] > 0 and check_AB[0]-check_AB[2]:
    AB_count += min(check_AB[0]-check_AB[1], check_AB[0]-check_AB[2])

print(AB_count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
ans = 0
ba = b = a = 0
for s in S:
    ans += s.count('AB')
    if s[0] + s[-1] == 'BA':
        ba += 1
    elif s[0] == 'B':
        b += 1
    elif s[-1] == 'A':
        a += 1
m = min([a, b, ba])
a -= m
b -= m
ba -= m
ans += m * 2
if a and b:
    ans += min([a, b])
elif a or b:
    ans += ba
else:
    ans += ba
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=['' for i in range(n)]
for i in range(n):
  s[i]=input()
#print(n,s)
cnt=0
cnta=0
cntb=0
cntab=0
ans=0
for i in range(n):
    cnt+=s[i].count('AB')
    if s[i][-1]=='A':
        cnta+=1
        if s[i][0]=='B':
            cntab+=1
    if s[i][0]=='B':
        cntb+=1
    

ans=cnt+min(cnta,cntb)

if cnta==cntab and cntb==cntab:
  ans=cnt+cntab-1
  
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=input()

ans=0
r_a_cnt=0
l_b_cnt=0
both=0

for _ in range(N):
	s=raw_input()
	if s[0]=="B" and s[-1]=="A": both+=1
	elif s[0]=="B": l_b_cnt+=1
	elif s[-1]=="A": r_a_cnt+=1

	len_s=len(s)
	for i in range(len_s-1):
		if s[i:i+2]=="AB":
			ans+=1

if l_b_cnt==r_a_cnt==0:
	ans+=both-1
else:
	ans+=min( l_b_cnt+both , r_a_cnt+both)

print ans

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
s=[]
for i in range(N):
	s.append(input())
count=0
for i in range(N):
	index = s[i].find("AB")
	if index != -1:
		count += 1

a=0
b=0
ba=0


for i in range(N):
	if s[i][-1]=="A" and s[i][0]=="B":
		ba+=1
	elif s[i][-1] == "A":
		a+=1
	elif s[i][0] == "B":
		b+=1
"""
one=min(a,b)
two=0
if ba>0:
	two=ba-1

	if abs(a-b)>0:
		two=ba-1+1
print(one+two+count)
"""

if ba==0:
	count+=min(a,b)
else:#ba aru
	if a == 0 and b == 0:
		count = count + ba -1
	elif a == 0 or b == 0:
		count += ba
	else:
		count = count + ba + min(a,b)
		# min(a, b) : -A,B-同士が結合する数
		# B-AはB-A同士で結合する 数：ba-1
		# 結合したB-Aの最初はB，最後はAになっているので，
		# そのどちらかに-A,B-が結合したものがくっつくき，また結合が1つできる 数：ba-1+1 = ba

print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=['' for i in range(n)]
for i in range(n):
  s[i]=input()
#print(n,s)
cnt=0
cnta=0
cntb=0
cntab=0
ans1=0
ans2=0
for i in range(n):
    cnt+=s[i].count('AB')
    if s[i][-1]=='A':
        cnta+=1
        if s[i][0]=='B':
            cntab+=1
    if s[i][0]=='B':
        cntb+=1
    

ans1=cnt+min(cnta+cntab,cntb)
ans2=cnt+min(cnta,cntb+cntab)
print(max(ans1,ans2))

Submission status: Wrong Answer

Solution code:
N=int(input())
s=[]
for i in range(N):
	s.append(input())
count=0
for i in range(N):
	if "AB" in s[i]:
		count+=1

a=0
b=0
ba=0
for i in range(N):
	if s[i][-1]=="A" and s[i][0]=="B":
		ba+=1
	elif s[i][-1] == "A":
		a+=1
	elif s[i][0] == "B":
		b+=1
"""
one=min(a,b)
two=0
if ba>0:
	two=ba-1

	if abs(a-b)>0:
		two=ba-1+1
print(one+two+count)
"""

if ba==0:
	count+=min(a,b)
else:#ba aru
	if a == 0 and b == 0:
		count = count + ba -1
	elif a == 0 or b == 0:
		count += ba - 1 + 1
	else:
		count = count + ba + min(a,b)
		# min(a, b) : -A,B-同士が結合する数
		# B-AはB-A同士で結合する 数：ba-1
		# 結合したB-Aの最初はB，最後はAになっているので，
		# そのどちらかに-A,B-が結合したものがくっつくき，また結合が1つできる 数：ba-1+1 = ba

print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
s=[]
for i in range(N):
	s.append(input())
count=0
for i in range(N):
	if "AB" in s[i]:
		count+=1

a=0
b=0
ba=0
for i in range(N):
	if s[i][-1]=="A" and s[i][0]=="B":
		ba+=1
	elif s[i][-1] == "A":
		a+=1
	elif s[i][0] == "B":
		b+=1
"""
one=min(a,b)
two=0
if ba>0:
	two=ba-1

	if abs(a-b)>0:
		two=ba-1+1
print(one+two+count)
"""

if ba==0:
	count+=min(a,b)
else:#ba aru
	if a+b>0:
		count = count + ba + min(a,b)
		# min(a, b) : -A,B-同士が結合する数
		# B-AはB-A同士で結合する 数：ba-1
		# 結合したB-Aの最初はB，最後はAになっているので，
		# そのどちらかに-A,B-が結合したものがくっつくき，また結合が1つできる 数：ba-1+1 = ba
	elif a+b==0:
		count = count + ba -1
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
s=[]
for _ in range(N):
    s.append(input())
def countAB(string):
    cnt=0
    for i in range(len(string)-1):
        if string[i]+string[i+1]=="AB":
            cnt+=1
    return cnt
def endA(string):
    if string[-1]=="A" and string[0]!="B":
        return 1
    else: return 0
def initB(string):
    if string[0]=="B" and string[-1]!="A":
        return 1
    else: return 0
def endAinitB(string):
    if string[0]=="B" and string[-1]=="A":
        return 1
    else: return 0
simple_AB=0
Aend=0
Binit=0
eAiB=0
for s_tmp in s:
    simple_AB+=countAB(s_tmp)
    Aend+=endA(s_tmp)
    Binit+=initB(s_tmp)
    eAiB+=endAinitB(s_tmp)
if Aend+Binit>0:
    print(simple_AB+min(Aend, Binit)+eAiB)
elif Aend+Binit==0:
    print(simple_AB+eAiB-1)

Submission status: Wrong Answer

Solution code:
N=int(input())
s=[]
for _ in range(N):
    s.append(input())
def countAB(string):
    cnt=0
    for i in range(len(string)-1):
        if string[i]+string[i+1]=="AB":
            cnt+=1
    return cnt
def endA(string):
    if string[-1]=="A":
        return 1
    else: return 0
def initB(string):
    if string[0]=="B":
        return 1
    else: return 0
def endAinitB(string):
    if string[0]=="B" and string[-1]=="A":
        return 1
    else: return 0
simple_AB=0
Aend=0
Binit=0
eAiB=0
for s_tmp in s:
    simple_AB+=countAB(s_tmp)
    Aend+=endA(s_tmp)
    Binit+=initB(s_tmp)
    eAiB+=endAinitB(s_tmp)
if eAiB!=N:
    print(simple_AB+min(Aend, Binit))
else:
    print(simple_AB+min(Aend, Binit)-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
res = 0
both = 0
h = 0
b = 0
for i in range(N):
  inp = input()
  if 'AB' in inp:
    res += 1
  if 'B'==inp[0] and 'A'==inp[-1]:
    both+=1
  elif 'B'==inp[0]:
    h+=1
  elif 'A'==inp[-1]:
    b += 1
if both !=0:
  if (h+b)!=0:
    res += (both+min(h,b))
  else:
  	res += (both-1)
else:
  res += min(h,b)
print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
s=[]
for _ in range(N):
    s.append(input())
def countAB(string):
    cnt=0
    for i in range(len(string)-1):
        if string[i]+string[i+1]=="AB":
            cnt+=1
    return cnt
def endA(string):
    if string[-1]=="A":
        return 1
    else: return 0
def initB(string):
    if string[0]=="B":
        return 1
    else: return 0
simple_AB=0
Aend=0
Binit=0
for s_tmp in s:
    simple_AB+=countAB(s_tmp)
    Aend+=endA(s_tmp)
    Binit+=initB(s_tmp)
print(simple_AB+min(Aend, Binit))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())

cnt = 0
list = [0, 0, 0]

for i in range(N):
    s = input()
    cnt += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        list[0] += 1
    elif s[-1] == 'A':
        list[1] += 1
    elif s[0] == 'B':
        list[2] += 1

min_ = min(list[0], list[1], list[2])

if min_ >= 1:
    cnt += 2 * min_
    list[0] -= min_
    list[1] -= min_
    list[2] -= min_

if list[0] >= 2:
    cnt += 2 * (list[0] // 2)
    list[0] -= 2 * (list[0] // 2)

if list[1] == list[2]:
    cnt += list[1]
elif list[1] > list[2]:
    cnt += list[2] + min(list[0], list[1] - list[2])
elif list[1] < list[2]:
    cnt += list[1] + min(list[0], list[2] - list[1])

print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
 
cnt = 0
list = [0,0,0]
 
for i in range(N):
    s = input()
    cnt += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        list[0] += 1
    elif s[-1] == 'A':
        list[1] += 1
    elif s[0] == 'B':
        list[2] += 1
 
min_ = min(list[0],list[1],list[2])
 
if min_ >= 1:
    cnt += 2*min_
    list[0] -= min_
    list[1] -= min_
    list[2] -= min_
 
if list[0] >= 2:
    cnt += 2*list[0]//2
    list[0] -= 2*(list[0]//2)
 
if list[1] == list[2]:
    cnt += list[1]
elif list[1] > list[2]:
    cnt += list[2] + min(list[0],list[1]-list[2])
elif list[1] < list[2]:
    cnt += list[1] + min(list[0],list[2]-list[1])
 
print(cnt)

Submission status: Wrong Answer

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

min_ab = min([b_top, a_end])
if b_top == a_end and b_top == ab_and:
  min_ab -= 1
print(ab_count + min_ab)

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):
        ABcount += s.count("AB")
        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 == 0:
    #     ABcount += min(Aend, Bstart)
    # else:
    #     if Aend + Bstart > 0:
    #         ABcount += min(Aend, Bstart) + AendBstart
    #     else:
    #         ABcount += AendBstart - 1
    # print(ABcount)
    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" and s[i][-1] == "A":
            AendBstart += 1
        elif s[i][0] == "B":
            Bstart += 1
        elif s[i][-1] == "A":
            Aend += 1

    if AendBstart == 0:
        ABcount += min(Aend, Bstart)
    else:
        if Aend + Bstart > 0:
            ABcount += min(Aend, Bstart) + AendBstart
        else:
            ABcount += AendBstart - 1
    print(ABcount)
    # 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()