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:
import sys
import numpy as np

N = int(input())

A = []

for n in range(N):
    chars = np.array(list(input()))
    A.append(chars)

#print(A[1][2])

A_cnt = 0
B_cnt = 0
AsBe = 0 # A start, B end
AB_cnt = 0

for n in range(N):
    if A[n][0] == "B":
        B_cnt += 1

    if A[n][-1] == "A":
        A_cnt += 1

    if A[n][-1] == "A" and A[n][0] == "B":
        AsBe += 1
        A_cnt -= 1
        B_cnt -= 1

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

if A_cnt > 0:
    a = 1
    A_cnt -= 1
else:
    a = 0

if B_cnt > 0:
    b = 1
    B_cnt -= 1
else:
    b = 0

print(AB_cnt + (AsBe-1) + a + b + min(A_cnt,B_cnt))

Submission status: Wrong Answer

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

cnt_ab_from_S = sum([s.count('AB') for s in S])
start_from_b = sum([(s[0] == 'B') for s in S])
end_at_a = sum([(s[-1] == 'A') for s in S])
start_from_b_and_end_at_a = sum([(s[0] == 'B') and (s[-1] == 'A') for s in S])

ans = (cnt_ab_from_S + min(start_from_b, end_at_a))
if (start_from_b * end_at_a * start_from_b_and_end_at_a == 1):
    ans -= min(start_from_b, end_at_a)
elif (start_from_b == end_at_a) and (end_at_a == start_from_b_and_end_at_a):
    ans -= 1
print(ans)

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 ab == 0:
    print(ans+min(A,B))
elif A == B == ab:
    print(ans+ab-1)

Submission status: Wrong Answer

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

cnt_ab_from_S = sum([s.count('AB') for s in S])
start_from_b = sum([(s[0] == 'B') for s in S])
end_at_a = sum([(s[-1] == 'A') for s in S])
start_from_b_and_end_at_a = sum([(s[0] == 'B') and (s[-1] == 'A') for s in S])

ans = (cnt_ab_from_S + min(start_from_b, end_at_a))
if (start_from_b * end_at_a * start_from_b_and_end_at_a == 1):
    pass
elif (start_from_b == end_at_a) and (end_at_a == start_from_b_and_end_at_a):
    ans -= 1
print(ans)

Submission status: Wrong Answer

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

cnt_ab_from_S = sum([s.count('AB') for s in S])
start_from_b = sum([(s[0] == 'B') for s in S])
end_at_a = sum([(s[-1] == 'A') for s in S])
start_from_b_and_end_at_a = sum([(s[0] == 'B') and (s[-1] == 'A') for s in S])

ans = (cnt_ab_from_S + min(start_from_b, end_at_a))
if (start_from_b == end_at_a) and (end_at_a == start_from_b_and_end_at_a):
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:


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


count=0


end_A=0
start_B=0
both=0
for i in range(N):
	count+=S[i].count("AB")
	if(S[i][-1]=="A"):
		end_A+=1
	if(S[i][0]=="B"):
		start_B+=1
#	if(not (S[i][-1]=="A" and (S[i][0]=="B"))):	
	if((S[i][-1]=="A" and not S[i][0]=="B") or (not S[i][-1]=="A" and S[i][0]=="B")):
		both+=1

#print(end_A,start_B,both,count)
if(both==0):
#if(min(end_A,start_B)<=both):
	print(max(count+min(end_A,start_B)-1,0))
else:
	print(count+min(end_A,start_B))

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)
count_ab += max(S_tail_a_head_b - 1, 0)

if S_tail_a_head_b >= 1 and S_tail_a != S_head_b:
    count_ab += 1

print(count_ab)

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)

count_ab += min(S_tail_a, S_head_b)
count_ab += max(S_tail_a_head_b - 1, 0)

if S_tail_a_head_b >= 1 and S_tail_a != S_head_b:
    count_ab += 1

print(count_ab)

Submission status: Wrong Answer

Solution code:

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


count=0


end_A=0
start_B=0
both=0
for i in range(N):
	count+=S[i].count("AB")
	if(S[i][-1]=="A"):
		end_A+=1
	if(S[i][0]=="B"):
		start_B+=1
#	if(not (S[i][-1]=="A" and (S[i][0]=="B"))):	
	if((S[i][-1]=="A" and not S[i][0]=="B") or (not S[i][-1]=="A" and S[i][0]=="B")):
		both+=1

#print(end_A,start_B,both,count)
if(both==0):
#if(min(end_A,start_B)<=both):
	print(count+min(end_A,start_B)-1)
else:
	print(count+min(end_A,start_B))

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
n = int(input())
s_list = []
xa_num = 0
bx_num = 0
ba_num = 0
cnt = 0
for _ in range(n):
    s_in = input()[:-1]
    if s_in[0] == 'B':
        if s_in[-1] == 'A':
            ba_num += 1
        else:
            bx_num += 1
    elif s_in[-1] == 'A':
        xa_num += 1
    flag = 0
    for s in s_in:
        if s == 'A':
            flag = 1
        elif s == 'B':
            if flag == 1:
                cnt += 1
            flag = 0
        else:
            flag = 0

if xa_num + bx_num == 0:
    print(cnt+ba_num-1)
else:
    print(cnt+ba_num+min(xa_num, bx_num))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for i in range(n)]
ans = 0
a = 0  # xA
b = 0  # Bx
c = 0  # BA

for i in s:
    if i.count('AB'):
        ans += 1
    if i[0] == 'B' and i[-1] == 'A':
        c += 1
    elif i[-1] == 'A':
        a += 1
    elif i[0] == 'B':
        b += 1

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, s: "List[str]"):
    x = 0
    a = 0
    b = 0
    for i in s:
        if(i[0] == 'B' and i[-1] == 'A'):
            x += 1
        elif(i[-1] == 'A'):
            a += 1
        elif(i[0] == 'B'):
            b += 1
    
    
    ####   AC1   #####    
    # if(x == 0):
    #     ans1 = min(a,b)
    # else:
    #     ans1 = max(0,x-1)+min(a,b)
    #     if(a > 0 or b > 0):
    #         ans1 += 1
    
    #####    AC2   ####
    ans1 = max(0,x-1) + min(a,b)
    if((a > 0 or b >0) and x > 0):
        ans1 += 1
    
    ####   WA    #############
    # ans1 = max(0,x-1) + min(a,b)
    # if(x != 0):
    #     ans1 += 1 
    

    ans1 = max(0,x-1) 
    if(a == b):
        ans1 += min(a,b)
    else:
        if(x == 0):
            ans1 += min(a,b)
        elif(a > 0 or b > 0):
            ans1 += min(a,b)+1 

    ans2 = 0
    for i in s:
        ans2 += i.count('AB')

    print(ans1+ans2)
    
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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())

count_ab = 0
S_tail_a_head_b = 0
S_tail_a = 0
S_head_b = 0
for _ in range(N):
    s = input()
    if s.find('AB') == -1 and s[0] != 'B' and s[-1] != 'A':
        continue

    count_ab += s.count('AB')

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

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

print(count_ab)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=list(input() for _ in range(n))
m,o,f,both=0,0,0,0
 
for i in range(n):
    m +=s[i].count("AB")
    if s[i][0]=="B":
        o +=1
for i in range(n):
    if s[i][-1]=="A":
        f +=1
for i in range(n):
    if s[i][0]=="B" and s[i][-1]=="A":
        both+=1
        o-=1
        f-=1
 
if both!=0:
    if i!=0 and f!=0:
        print(m+both+min(o,f))
    else:
        print(m+both+min(o,f)-1)
 
else:
    print(m+min(o,f))

Submission status: Wrong Answer

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

cnt_ab_from_S = sum([s.count('AB') for s in S])
start_from_b = sum([(s[0] == 'B') for s in S])
end_at_a = sum([(s[-1] == 'A') for s in S])
start_from_b_and_end_at_a = sum([(s[0] == 'B') and (s[-1] == 'A') for s in S])

ans = cnt_ab_from_S
if start_from_b * end_at_a * start_from_b_and_end_at_a != 1:
    ans += min(start_from_b, end_at_a)
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import math
import itertools


def makeIntMatrix(lines):
    intMatrix = []
    for a in makeStringMatrix(lines):
        intMatrix.append([int(b) for b in a])
    return intMatrix
 
 
def makeStringMatrix(lines):
    stringMatrix = [line.rstrip() for line in lines]
    return stringMatrix
 
 
def makeInt(line):
    return int(line.rstrip())
 
 
def makeMultiInteger(line):
    return [int(x) for x in line.rstrip().split()]


def solve(input_string):
    N = makeInt(input_string[0])
    S = makeStringMatrix(input_string[1:])
    #print(S)
    C_S_has_ab = []
    for s_index, s in enumerate(S):
        C_S_has_ab.append(0)
        for i in range(len(s)-1):
            if s[i] + s[i+1] == 'AB':
                C_S_has_ab[s_index] += 1
    #print(C_S_has_ab)
    _sum = 0
    _sum += sum(C_S_has_ab) 
    c_head_has_b = 0
    c_tail_has_a = 0
    c_head_tail_has_a_and_b = 0
    for s in S:
        """
        if s[0] == 'B':
            c_head_has_b += 1
        if s[-1] == 'A':
            c_tail_has_a += 1
        if s[0] == 'B' and s[-1] == 'A':
            c_head_tail_has_a_and_b += 1
        """
        if(s[0] == 'B' and s[-1] == 'A'):
            c_head_tail_has_a_and_b += 1
        elif(s[-1] == 'A'):
            c_tail_has_a += 1
        elif(s[0] == 'B'):
            c_head_has_b += 1
    print(c_head_has_b, c_tail_has_a, c_head_tail_has_a_and_b)
    if c_head_tail_has_a_and_b > 0:
        count_combi_a_b = max(0, c_head_tail_has_a_and_b-1) + min(c_head_has_b, c_tail_has_a)
        if c_head_has_b > 0 or c_tail_has_a > 0:
            count_combi_a_b += 1
    else:
        count_combi_a_b = min(c_head_has_b, c_tail_has_a)
    _sum += count_combi_a_b
    #print(_sum)
    return _sum


def main():
    input_lines = stdin.readlines()
    answer = solve(input_lines)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
s=list(input() for _ in range(n))
m,o,f,both=0,0,0,0

for i in range(n):
    m +=s[i].count("AB")
    if s[i][0]=="B":
        o +=1
for i in range(n):
    if s[i][-1]=="A":
        f +=1
for i in range(n):
    if s[i][0]=="B" and s[i][-1]=="A":
        both+=1
        o-=1
        f-=1

if both!=0:
    if i!=0 and f!=0:
        print(m+both+min(o,f))
    else:
        print(m+both+min(o,f)-1)

else:
    print(m+min(o,f))

Submission status: Wrong Answer

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

for i in range(n):
    s.append(list(input()))

x = 0    

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

p = 0
q = 0
r = 0
for i in range(n):
    if s[i][0]=="B" and s[i][-1]!= "A":
        p += 1
    if s[i][0]!="B" and s[i][-1]=="A":
        q += 1
    if s[i][0]== "B" and s[i][-1]=="A":
        r +=1

if p>0 and q==0 :
    print(x+min(p-1,q)+r)
elif p==0 and q>0:
    print(x+min(p,q-1)+r)
elif p==0 and q==0:
    print(x+r-1)
else:
    print(x+min(p,q)+r)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=list(input() for _ in range(n))
m,o,f,both=0,0,0,0

for i in range(n):
    m +=s[i].count("AB")
    if s[i][0]=="B":
        o +=1
for i in range(n):
    if s[i][-1]=="A":
        f +=1
for i in range(n):
    if s[i][0]=="B" and s[i][-1]=="A":
        both+=1
        o-=1
        f-=1

if both!=0:
    if i!=0 and f!=0:
        print(m+both+min(o,f))
    else:
        print(m+both+min(o,f)-1)

else:
    print(m+min(o,f))

a=[m,o,f,both]
print(a)

Submission status: Wrong Answer

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

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

counter = 0
#左端がb and 右端がa の数
ab = 0
#ab以外で右端がaの数
a = 0
#ab以外で左端がbの数
b = 0

for s in string_list:
    if 'AB' in s:
        counter += 1
    if s[0] == 'B' and s[-1] == 'A':
        ab += 1
    elif s[0] == 'B':
        b += 1
    elif s[-1] == 'A':
        a += 1

#ダメだった奴
'''
c = min(a,b)
d = max(a,b)
if d == 0 and ab > 0:
    counter += ab - 1
else:
    counter += c + ab
'''
if ab == 0:
    counter += min(a,b)
elif a + b == 0:
    counter += ab - 1
else:
    counter += min(a,b) + ab

print(counter)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=0
B=0
A=0
count1=0
count2=0
for i in range(N):
    S=input()
    a+=S.count("AB")
    if S[0]=="B"and S[len(S)-1]=="A":
        count1+=1
        if count1==1:
            A+=1
            B+=1
        else:
            a+=1
    elif S[0]=="B":
        B+=1
        count2+=1
    elif S[len(S)-1]=="A":
        A+=1
        count2+=1
if count2==0:
    a=a-1
C=min(A,B)
print(min(a+C,N-1))

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()
    if s.find('AB') == -1 and s[0] != 'B' and s[-1] != 'A':
        continue

    count_ab += s.count('AB')

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

count_ab += min(S_tail_a, S_head_b)
count_ab += max(S_tail_a_head_b - 1, 0)

if S_tail_a_head_b >= 1 and S_tail_a != S_head_b:
    count_ab += 1

print(count_ab)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import math
import itertools


def makeIntMatrix(lines):
    intMatrix = []
    for a in makeStringMatrix(lines):
        intMatrix.append([int(b) for b in a])
    return intMatrix
 
 
def makeStringMatrix(lines):
    stringMatrix = [line.rstrip() for line in lines]
    return stringMatrix
 
 
def makeInt(line):
    return int(line.rstrip())
 
 
def makeMultiInteger(line):
    return [int(x) for x in line.rstrip().split()]


def solve(input_string):
    N = makeInt(input_string[0])
    S = makeStringMatrix(input_string[1:])
    #print(S)
    C_S_has_ab = []
    for s_index, s in enumerate(S):
        C_S_has_ab.append(0)
        for i in range(len(s)-1):
            if s[i] + s[i+1] == 'AB':
                C_S_has_ab[s_index] += 1
    #print(C_S_has_ab)
    _sum = 0
    _sum += sum(C_S_has_ab) 
    c_head_has_b = 0
    c_tail_has_a = 0
    c_head_tail_has_a_and_b = 0
    for s in S:
        if s[0] == 'B':
            c_head_has_b += 1
        if s[-1] == 'A':
            c_tail_has_a += 1
        if s[0] == 'B' and s[-1] == 'A':
            c_head_tail_has_a_and_b += 1
    #print(c_head_has_b, c_tail_has_a, c_head_tail_has_a_and_b)
    if c_head_tail_has_a_and_b > 0:
        count_combi_a_b = max(0, c_head_tail_has_a_and_b-1) + min(c_head_has_b, c_tail_has_a)
        if c_head_has_b > 0 or c_tail_has_a > 0:
            count_combi_a_b += 1
    else:
        count_combi_a_b = min(c_head_has_b, c_tail_has_a)
    _sum += count_combi_a_b
    #print(_sum)
    return _sum


def main():
    input_lines = stdin.readlines()
    answer = solve(input_lines)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def sol(_inp=input):
    n=int(_inp())
    words=[_inp() for _ in range(n)]
    ab_in_words=sum(w.count('AB') for w in words)
    ends_A=sum(w.endswith('A') for w in words)
    starts_B=sum(w.startswith('B') for w in words)
    sands=sum((w.endswith('A') and w.startswith('B')) for w in words)
    if ends_A==sands and starts_B==sands:
        return str(ab_in_words+sands-1)
    return str(ab_in_words+min(ends_A,starts_B))
print(sol())

Submission status: Wrong Answer

Solution code:
n=int(input())
s=list(input() for _ in range(n))
m,o,f,both=0,0,0,0

for i in range(n):
    m +=s[i].count("AB")
    if s[i][0]=="B":
        o +=1
for i in range(n):
    if s[i][-1]=="A":
        f +=1
for i in range(n):
    if s[i][0]=="B" and s[i][-1]=="A":
        both+=1
        o-=1
        f-=1

if both!=0:
    if i!=0 and f!=0:
        print(m+both+min(o,f)-1)
    else:
        print(m+both+min(o,f))

else:
    print(m+min(o,f))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, s: "List[str]"):
    x = 0
    a = 0
    b = 0
    for i in s:
        if(i[0] == 'B' and i[-1] == 'A'):
            x += 1
        elif(i[-1] == 'A'):
            a += 1
        elif(i[0] == 'B'):
            b += 1
    
    
    ####   AC   #####    
    # if(x == 0):
    #     ans1 = min(a,b)
    # else:
    #     ans1 = max(0,x-1)+min(a,b)
    #     if(a > 0 or b > 0):
    #         ans1 += 1
    ans1 = max(0,x-1) + min(a,b)
    if(a == b):
        if(x != 0 and a > 0):
            ans1 += 1
    else:
        if(a > 0 or b > 0):
            ans1 += 1
             

    
    # ans1 = max(0,x-1) 
    # if(a == b):
    #     ans1 += min(a,b)
    # else:
    #     if(x == 0):
    #         ans1 += min(a,b)
    #     else:
    #         ans1 += min(a,b)+1 
    

    ans2 = 0
    for i in s:
        ans2 += i.count('AB')

    print(ans1+ans2)
    
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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:
import sys

input = sys.stdin.readline
n = int(input())
s_list = []
xa_num = 0
bx_num = 0
ba_num = 0
cnt = 0
for _ in range(n):
    s_in = input()[:-1]
    s_len = len(s_in)
    flag = 0
    check = 0
    for i in range(s_len):
        if s_in[i] == 'A':
            if i == s_len-1:
                xa_num += 1
                check += 1
            flag = 1
        elif s_in[i] == 'B':
            if i == 0:
                bx_num += 1
                check += 1
            if flag == 1:
                cnt += 1
            flag = 0
        else:
            flag = 0
    if check == 2:
        xa_num -= 1
        bx_num -= 1
        ba_num += 1
if xa_num + bx_num == 0:
    print(cnt+ba_num-1)
else:
    print(cnt+ba_num+min(xa_num, bx_num))

Submission status: Wrong Answer

Solution code:
from sys import stdin
import math
import itertools


def makeIntMatrix(lines):
    intMatrix = []
    for a in makeStringMatrix(lines):
        intMatrix.append([int(b) for b in a])
    return intMatrix
 
 
def makeStringMatrix(lines):
    stringMatrix = [line.rstrip() for line in lines]
    return stringMatrix
 
 
def makeInt(line):
    return int(line.rstrip())
 
 
def makeMultiInteger(line):
    return [int(x) for x in line.rstrip().split()]


def solve(input_string):
    N = makeInt(input_string[0])
    S = makeStringMatrix(input_string[1:])
    #print(S)
    C_S_has_ab = []
    for s_index, s in enumerate(S):
        C_S_has_ab.append(0)
        for i in range(len(s)-1):
            if s[i] + s[i+1] == 'AB':
                C_S_has_ab[s_index] += 1
    #print(C_S_has_ab)
    _sum = 0
    _sum += sum(C_S_has_ab) 
    c_head_has_b = 0
    c_tail_has_a = 0
    c_head_tail_has_a_and_b = 0
    for s in S:
        if s[0] == 'B':
            c_head_has_b += 1
        if s[-1] == 'A':
            c_tail_has_a += 1
        if s[0] == 'B' and s[-1] == 'A':
            c_head_tail_has_a_and_b += 1
    #print(c_head_has_b, c_tail_has_a, c_head_tail_has_a_and_b)
    if max(c_head_has_b, c_tail_has_a) == min(c_head_has_b, c_tail_has_a):
        if c_head_tail_has_a_and_b > 0:
            #count_combi_a_b = min(c_head_has_b, c_tail_has_a) - 1
            count_combi_a_b = max(0, c_head_tail_has_a_and_b-1) + min(c_head_has_b, c_tail_has_a)
            if max(c_head_has_b, c_tail_has_a) > 0:
                count_combi_a_b += 1
        else:
            count_combi_a_b = min(c_head_has_b, c_tail_has_a)
    else:
        count_combi_a_b = min(c_head_has_b, c_tail_has_a)
    _sum += count_combi_a_b
    #print(_sum)
    return _sum


def main():
    input_lines = stdin.readlines()
    answer = solve(input_lines)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
a=0
B=0
A=0
count1=0
count2=0
for i in range(N):
    S=input()
    a+=S.count("AB")
    if S[0]=="B"and S[len(S)-1]=="A":
        count1+=1
        if count1==1:
            A+=1
            B+=1
        else:
            a+=1
    elif S[0]=="B":
        B+=1
        count2+=1
    elif S[len(S)-1]=="A":
        A+=1
        count2+=1
if count2==0:
    a=a-1
C=min(A,B)
D=min(C,N-1)
print(a+D)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, s: "List[str]"):
    x = 0
    a = 0
    b = 0
    for i in s:
        if(i[0] == 'B' and i[-1] == 'A'):
            x += 1
        elif(i[-1] == 'A'):
            a += 1
        elif(i[0] == 'B'):
            b += 1
    
    
    ####   AC   #####    
    # if(x == 0):
    #     ans1 = min(a,b)
    # else:
    #     ans1 = max(0,x-1)+min(a,b)
    #     if(a > 0 or b > 0):
    #         ans1 += 1
    ans1 = max(0,x-1) + min(a,b)
    if(a == b):
        if(x != 0 and (a > 0 or b > 0)):
            ans1 += 1
    else:
        if(a > 0 or b > 0):
            ans1 += 1
             

    
    # ans1 = max(0,x-1) 
    # if(a == b):
    #     ans1 += min(a,b)
    # else:
    #     if(x == 0):
    #         ans1 += min(a,b)
    #     else:
    #         ans1 += min(a,b)+1 
    

    ans2 = 0
    for i in s:
        ans2 += i.count('AB')

    print(ans1+ans2)
    
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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


def solve(N: int, s: "List[str]"):
    x = 0
    a = 0
    b = 0
    for i in s:
        if(i[0] == 'B' and i[-1] == 'A'):
            x += 1
        elif(i[-1] == 'A'):
            a += 1
        elif(i[0] == 'B'):
            b += 1
    
    
    ####   AC   #####    
    # if(x == 0):
    #     ans1 = min(a,b)
    # else:
    #     ans1 = max(0,x-1)+min(a,b)
    #     if(a > 0 or b > 0):
    #         ans1 += 1
    ans1 = max(0,x-1) + min(a,b)
    if(a == b):
        if(x != 0):
            ans1 += 1
    else:
        if(a > 0 or b > 0):
            ans1 += 1
             

    
    # ans1 = max(0,x-1) 
    # if(a == b):
    #     ans1 += min(a,b)
    # else:
    #     if(x == 0):
    #         ans1 += min(a,b)
    #     else:
    #         ans1 += min(a,b)+1 
    

    ans2 = 0
    for i in s:
        ans2 += i.count('AB')

    print(ans1+ans2)
    
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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())

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

if Bxxx > 0 or xxxA > 0:
    # Bxxx と xxxA のペアの後ろに BxxA をくっつける
    print(AB + min(Bxxx, xxxA) + BxxA)
else:
    # BxxA だけでペアを作る
    print(AB + BxxA - 1)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=list(input() for _ in range(n))
m,i,f,both=0,0,0,0

for i in range(n):
    m +=s[i].count("AB")
    if s[i][0]=="B":
        i +=1
for i in range(n):
    if s[i][-1]=="A":
        f +=1
for i in range(n):
    if s[i][0]=="B" and s[i][1]=="A":
        both+=1
        i-=1
        f-=1

if both!=0:
    if i!=0 and f!=0:
        print(m+both+min(i,f)-1)
    else:
        print(m+both+min(i,f))

else:
    print(min(i,f))

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=0
flagA=[]
flagB=[]
fla=0
flb=0
for x in range(n):
    s = input()
    if 'AB' in s:
        ans += s.count('AB')
    if s[-1]=='A':
        flagA.append(x)
        fla+=1
    if s[0]=='B':
        flagB.append(x)
        flb+=1
#if flagA==flagB:
#    ans-=1
ans+=min(fla,flb)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
n = int(input())
s_list = []
a_end_num = 0
b_start_num = 0
cnt = 0
for _ in range(n):
    s_in = input()[:-1]
    s_len = len(s_in)
    flag = 0
    for i in range(s_len):
        if s_in[i] == 'A':
            if i == s_len-1:
                a_end_num += 1
            flag = 1
        elif s_in[i] == 'B':
            if i == 0:
                b_start_num += 1
            if flag == 1:
                cnt += 1
            flag = 0
        else:
            flag = 0
if b_start_num == n:
    b_start_num -= 1
print(cnt+min(a_end_num, b_start_num))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
s=list(input() for _ in range(n))
m,i,f,both=0,0,0,0

for i in range(n):
    m +=s[i].count("AB")
    if s[i][0]=="B":
        i +=1


if both!=0:
    if i!=0 and f!=0:
        print(m+both+min(i,f)-1)
    else:
        print(m+both+min(i,f))

else:
    print(min(i,f))

Submission status: Wrong Answer

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

ans = 0
head_b = 0
tail_a = 0
headAndTail = 0

for s in s_list:
    if 'AB' in s:
        ans += 1
    if s.startswith('B') and s.endswith('A'):
       headAndTail += 1
    elif s.startswith('B'):
        head_b += 1
    elif s.endswith('A'):
        tail_a += 1

ans += min([head_b, tail_a])

if head_b == 0 and tail_a == 0:
    ans += max([0,headAndTail-1])
else:
    ans += headAndTail

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=0;b=0;ab=0;res=0
for i in range(n):
    s=input()
    res+=s.count("AB")
    if s[0]=="B":
        if s[-1]=="A":
            ab+=1
        else:
            b+=1
    elif s[-1]=="A":
        a+=1
m=min(a,b)
res+=m
a-=m;b-=m
M=max(a,b)
Mm=min(M,ab)
res+=Mm
ab-=Mm
res+=ab//2
print(res)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=0
B=0
A=0
count1=0
count2=0
for i in range(N):
    S=input()
    a+=S.count("AB")
    if S[0]=="B"and S[len(S)-1]=="A":
        a+=1
        count1+=1
    elif S[0]=="B":
        B+=1
        count2+=1
    elif S[len(S)-1]=="A":
        A+=1
        count2+=1
if count1!=0:
    A+=1
    B+=1
if count2==0:
    a=a-1
C=min(A,B)
D=min(C,N-1)
print(a+D)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=0
flagA=[]
flagB=[]
fla=0
flb=0
for x in range(n):
    s = input()
    if 'AB' in s:
        ans += s.count('AB')
    if s[-1]=='A':
        flagA.append(x)
        fla+=1
    if s[0]=='B':
        flagB.append(x)
        flb+=1
if flagA==flagB:
    ans-=1
    if n==len(flagA):
        ans+=1
ans+=min(fla,flb)
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, s: "List[str]"):
    x = 0
    a = 0
    b = 0
    for i in s:
        if(i[0] == 'B' and i[-1] == 'A'):
            x += 1
        elif(i[-1] == 'A'):
            a += 1
        elif(i[0] == 'B'):
            b += 1

    # ans1 = max(0,x-1) + (min(a,b) if a==b else min(a,b)+1)    
    
    if(x == 0):
        ans1 = min(a,b)
    else:
        ans1 = max(0,x-1)+min(a,b)
        if(a != b):
            ans1 += 1

    
    # ans1 = max(0,x-1) 
    # if(a == b):
    #     ans1 += min(a,b)
    # else:
    #     if(x == 0):
    #         ans1 += min(a,b)
    #     else:
    #         ans1 += min(a,b)+1 
    

    ans2 = 0
    for i in s:
        ans2 += i.count('AB')

    print(ans1+ans2)
    
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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())
li = [input() for i in range(n)]

cnt = 0
xa = bx = ba = 0
for i in range(n):
  s = li[i]
  cnt += s.count("AB")
  if s.find("B")==0 and s.rfind("A")==len(s)-1:
    ba += 1
  elif s.find("B") == 0:
    bx += 1
  elif s.rfind("A") == len(s)-1:
    xa += 1

if xa == 0 and bx == 0:
  print("{}".format(cnt+ba-1))
elif xa == 0 or bx == 0:
  print("{}".format(cnt+ba))
else:
  print("{}".format(cnt+ba+1+min([xa-1,bx-1])))