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:
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[0]=="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())
string=[input() for i in range(n)]

base_ab=0
last_a=0
first_b=0
all_ba=0
for s in string:
    base_ab+=s.count("AB")
    if s[-1]=="A":
        last_a+=1
    if s[0]=="B":
        first_b+=1
    if s[0]=="B" and s[-1]=="A":
        all_ba+=1
if last_a==0 and first_b==0 and all_ba>0:
    print(base_ab+min(last_a,first_b)-1)
else:    
    print(base_ab+min(last_a,first_b))

Submission status: Wrong Answer

Solution code:
n=int(input())
s=list(input() for _ in range(n))
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
        break
    if s[i][-1]=="A":
        f +=1
        break
    if s[i][0]=="B" and s[i][1]=="A":
        both+=1
        i-=1
        f-=1
        break

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

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
ans = 0
a=0
b=0
c=n-1

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

ans += min(n-1,a,b)
if a==b and b==c and c != 0:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
S = [input() for _ in range(n)]
ab = 0
ba = 0
bx = 0
xa = 0
for s in S:
    if s[0] == 'B' and s[-1] == 'A':
        ba += 1
    elif s[0] == 'B' and s[-1] != 'A':
        bx += 1
    elif s[0] != 'B' and s[-1] == 'A':
        xa += 1
    for i in range(len(s) - 1):
        if s[i] == 'A' and s[i + 1] == 'B':
            ab += 1
if xa == bx:
    ab += xa
    if ba > 1:
        ab += ba - 1
else:
    ab += min(xa, bx)
    ab += ba
print(ab)

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 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:
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
        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:
str_dict = [0, 0, 0] 
val1 = 0
for i in range(int(input())):
  line = input()
  val1 += line.count('AB')
  if line[0] == 'B' and line[-1] == 'A':
    str_dict[2] += 1
  elif line[0] == 'B':
    str_dict[0] += 1
  elif line[-1] == 'A':
    str_dict[1] += 1
# if str_dict[2] == 0:
#   print(val1+min(str_dict[0], str_dict[1]))
# else:
#   if str_dict[0]+str_dict[1] > 0:
#     print(val1+str_dict[2]+min(str_dict[0], str_dict[1]))
#   else:
#     print(val1+str_dict[2]-1)
val2 = min(str_dict[0], str_dict[1])
tmp = max(str_dict[0]-val2, str_dict[1]-val2)
val3 = min(tmp, str_dict[2])
val4 = str_dict[2]-val3
if val3 == 0 and val4 > 0:
  val4 -= 1
print(val1+val2+val3+val4)

Submission status: Wrong Answer

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


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

Bxxx = 0
xxxA = 0
BxxA = 0
xABx = 0
for i in range(N):
    s = input()
    xABx += s.count("AB")
    if (s[0] == "B") and (s[-1] == "A"):
        BxxA += 1
    else:
        if s[0] == "B":
            Bxxx += 1
        if s[-1] == "A":
            xxxA += 1
#print("Bxxx={},xxxA={},BxxA={},xABx={}".format(Bxxx,xxxA,BxxA,xABx))

min_Bx_xA = min(Bxxx, xxxA)
if BxxA == 0:
    print(xABx + min_Bx_xA)
elif min_Bx_xA == 0:
    print(xABx + BxxA - 1)
else:
    set3 = min(BxxA, Bxxx, xxxA)
    #print(xABx + set3 * 2 + BxxA - set3 + min(Bxxx, xxxA) - set3)
    print(xABx + BxxA + min(Bxxx, xxxA))

Submission status: Wrong Answer

Solution code:
N = int(input())

A=[]
for i in range(N):
    A.append(input())
    

def mojicount(s, target):
    index = -1
    c=0
    while True:
        index = s.find(target, index + 1)
        if index == -1:
            break
        
        c+=1
    return(c)

ans=0
a_number=0
b_number=0
double=0
for i in range(N):
    ans+=mojicount(A[i],'AB')
    
    if A[i][0]=='B':
        b_number+=1
    if A[i][-1]=='A':
        a_number+=1
        if A[i][0]=='B':
            double=+1


    
print(ans+min(a_number,b_number)-double)

Submission status: Wrong Answer

Solution code:
N = int(input())
 
a = 0
b = 0
c = 0
count = 0
for i in range(N):
    s = input()
    count += len([1 for j in range(len(s)) if s[j:j+2] == "AB"])
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1
    if s[0] == "B" and s[-1] == "A":
        c += 1

if a == b and b == c:
    print(count + c - 1)
else:
    print(count + min([a, b, N-1]))

Submission status: Wrong Answer

Solution code:
str_dict = [0, 0, 0] 
val1 = 0
for i in range(int(input())):
  line = input()
  val1 += line.count('AB')
  if line[0] == 'B' and line[-1] == 'A':
    str_dict[2] += 1
  elif line[0] == 'B':
    str_dict[0] += 1
  elif line[-1] == 'A':
    str_dict[1] += 1
# if str_dict[2] == 0:
#   print(val1+min(str_dict[0], str_dict[1]))
# else:
#   if str_dict[0]+str_dict[1] > 0:
#     print(val1+str_dict[2]+min(str_dict[0], str_dict[1]))
#   else:
#     print(val1+str_dict[2]-1)
val2 = min(str_dict[0], str_dict[1])
tmp = max(str_dict[0]-val2, str_dict[1]-val2)
val3 = min(tmp, str_dict[2])
val4 = str_dict[2]-val3
if val3 == 0:
  val4 -= 1
print(val1+val2+val3+val4)

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)    
    ans1 = max(0,x-1) 
    if(a == b):
        if(x != 0):
            ans1 += 1
        ans1 += min(a,b)
    else:
        if(x == 0):
            ans1 += min(a,b)
        else:
            ans1 += min(a,b)+1 
    # ans1 = min(a,b)
    # if(b == 0 and a == 0):
    #     ans1 += max(0,x-1)
    # else:
    #     ans1 += x


    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
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
ans = 0
a=0
b=0
c=n-1

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

ans += min(n-1,a,b)
if a==b and b==c and c != 0:
    ans -= 1
print(ans)

Submission status: Wrong Answer

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

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)
if p==0 and q>0:
    print(x+min(p,q-1)+r)
if p==0 and q==0:
    print(x+r-1)
else:
    print(x+min(p,q)+r)

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)
    #c_need_a_and_b = max(c_head_has_b, c_tail_has_a)- min(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
        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())
 
b = 0
a = 0
c = 0
count = 0
for i in range(N):
    s = input()
    count += len([1 for j in range(len(s)) if s[j:j + 2] == "AB"])
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1
    if s[0] == "B" and s[-1] == "A":
        c += 1

if a == b and b == c:
    print(count+c-1)
else:
    print(count + min([a, b, N-1]))

Submission status: Wrong Answer

Solution code:
'''
C - AB Substrings
'''

str_list = []
ab_num = 0
c1 = 0
c2 = 0
c3 = 0
for i in range(int(input())):
    str_list.append(input())
    if 'AB' in str_list[-1]:
        ab_num += 1
    if str_list[-1].endswith('A') and str_list[-1].startswith('B'):
        c1 += 1
    elif str_list[-1].startswith('B'):
        c2 += 1
    elif str_list[-1].endswith('A'):
        c3 += 1

if c1 == 0:
    print(ab_num + min(c2, c3))
elif c2 + c3 > 0:
    print(ab_num + c1 + min(c2, c3))
elif c2 + c3 == 0:
    print(ab_num + c1 - 1)

Submission status: Wrong Answer

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

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+r-1)
else:
    print(x+min(p,q)+r)

Submission status: Wrong Answer

Solution code:
N = int(input())
 
b = 0
a = 0
count = 0
check = -1
for i in range(N):
    s = input()
    count += len([1 for j in range(len(s)) if s[j:j + 2] == "AB"])
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1
    if s[0] == "B" and s[-1] == "A":
        check += 1

if a == b and b == check:
    print(count+b-1)
else:
    print(count + min([a, b, N-1]))

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

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

ans += min([head_b, tail_a])

print(ans)

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':
        Bxxx += 1
    elif s[-1] == 'A' and s[0] != 'B':
        xxxA += 1
    elif s[0] == 'B' and s[-1] == 'A':
        BxxA += 1

pair = min(Bxxx, xxxA)

if abs(Bxxx - xxxA) >= BxxA:
    # xxxA と Bxxx の少ない方に全フリ
    pair += BxxA
    BxxA = 0
else:
    # xxxA と Bxxx の差を埋める
    pair += abs(Bxxx - xxxA)
    BxxA -= abs(Bxxx - xxxA)

# 残った BxxA でペアを作る
pair += BxxA // 2
BxxA -= BxxA // 2

# すでにペアがあり、BxxAが残っている場合はさらにペアを作れる
if pair > 0 and BxxA > 0:
    pair += BxxA

print(AB + pair)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
Alist = [word for word in S if word[-1] == 'A']
Blist = [word for word in S if word[0] == 'B']
ABlist = [word for word in S if word[0] == 'B' and word[-1] == 'A']
Slist = [word.count('AB') for word in S]
C = min(len(Alist),len(Blist))
if len(ABlist) == len(Alist) and len(ABlist) == len(Blist):
    print(sum(Slist)+len(ABlist)-1)
else:
    print(sum(Slist)+min(C,N-1))

Submission status: Wrong Answer

Solution code:

def solve(Strs,n):
    count = 0
    end_A = 0
    start_B = 0
    both = 0
    for i in range(0,n):
        s = Strs[i]
        count += s.count("AB")
        if s[0] == "B" and s[-1] == "A":
            both += 1
        elif s[0] == "B":
            start_B += 1
        elif s[-1] == "A":
            end_A += 1

    tmp = 0
    if both > 0 and end_A > 0 and start_B > 0:
        tmp = both + 1
        end_A -= 1
        start_B -= 1
    elif end_A == 0 or start_B == 0:
        tmp = both
        if end_A == 0:
            start_B -= 1
        if start_B == 0:
            end_A -= 1
    elif end_A == 0 and start_B == 0:
        tmp = both

    return count + tmp + min(start_B,end_A)

Ss = []
N = int(input())
for i in range(0,N):
    Ss.append(input())

print(solve(Ss,len(Ss)))

Submission status: Wrong Answer

Solution code:
N = int(input())

ba = 0
_a = 0
b_ = 0
ans = 0
for _ in range(N):
    s = input()
    ans += s.count('AB')
    if s[-1] == 'A' and s[0] == 'B':
        ba += 1
    elif s[-1] == 'A':
        _a += 1
    elif s[0] == 'B':
        b_ += 1

if _a == b_ == 0 and ba > 0:
    ans = ba - 1
else:
    ans += min(ba+_a, ba+b_)

print(ans)

Submission status: Wrong Answer

Solution code:
'''
C - AB Substrings
'''

str_list = []
end_a = []
start_b = []
ab_num = 0
c1 = 0
c2 = 0
c3 = 0
for i in range(int(input())):
    str_list.append(input())
    if 'AB' in str_list[-1]:
        ab_num += 1
    if str_list[-1].endswith('A') and str_list[-1].startswith('B'):
        c1 += 1
    elif str_list[-1].startswith('B'):
        c2 += 1
    elif str_list[-1].endswith('A'):
        c3 += 1

if c1 == 0:
    print(ab_num + min(c2, c3))
elif c2 + c3 > 0:
    print(ab_num + c1 + min(c2, c3))
elif c2 + c3 == 0:
    print(ab_num + c1 - 1)

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':
        Bxxx += 1
    elif s[-1] == 'A' and s[0] != 'B':
        xxxA += 1
    elif s[0] == 'B' and s[-1] == 'A':
        BxxA += 1

pair = min(Bxxx, xxxA)

if abs(Bxxx - xxxA) > BxxA:
    # xxxA と Bxxx の少ない方に全フリ
    pair += BxxA
    BxxA = 0
else:
    # xxxA と Bxxx の差を埋める
    pair += abs(Bxxx - xxxA)
    BxxA -= abs(Bxxx - xxxA)

# 残った BxxA でペアを作る
pair += BxxA // 2
BxxA -= BxxA // 2

# すでにペアがあり、BxxAが残っている場合はさらにペアを作れる
if pair > 0 and BxxA > 0:
    pair += BxxA

print(AB + pair)

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':
        Bxxx += 1
    elif s[-1] == 'A' and s[0] != 'B':
        xxxA += 1
    elif s[0] == 'B' and s[-1] == 'A':
        BxxA += 1

pair = min(Bxxx, xxxA)

if abs(Bxxx - xxxA) > BxxA:
    # xxxA と Bxxx の少ない方に全フリ
    pair += BxxA
    BxxA = 0
else:
    # xxxA と Bxxx の差を埋める
    pair += abs(Bxxx - xxxA)
    BxxA -= abs(Bxxx - xxxA)

# 残った BxxA でペアを作る
if BxxA > 0:
    pair += BxxA // 2 + 1

print(AB + pair)

Submission status: Wrong Answer

Solution code:
n = int(input())
ab = 0
bx = 0
xa = 0
bxa = 0

for ln in range(n):
    line = input().rstrip()
    ab += line.count("AB")
    first , last = line[0], line[-1]
    if (first, last) == ('B', 'A'):
        bxa += 1
    elif first == 'B':
        bx += 1
    elif last == 'A':
        xa += 1

if bx + xa == 0:
    bxa -= 1

print(ab + bxa + min(bx, xa))

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 > min(A,B):
    print(ans+2*min(A,B)-ab)
else:
    print(ans+min(A,B)-ab)

Submission status: Wrong Answer

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

ans = 0
firstB = 0
lastA = 0
cnt = 0
for s in S:
    if s[0] == 'B':
        firstB += 1
    if s[-1:] == 'A':
        lastA += 1

    if s[0] == 'B' and s[-1:] == 'A':
        cnt += 1

    ans += s.count('AB')
    
if cnt == max([firstB,lastA]):
    print(ans + cnt -1)
else:
    print(ans+ min([firstB,lastA]))

Submission status: Wrong Answer

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

cnt = 0
BA = 0 # 'B~A'
lA = 0 # last 'A'
fB = 0 # first 'B'

for i in S:
  c = i.count('AB')
  # count 'B~*', 'B~A' '*~A'
  if i.find('B') == 0 and i.rfind('A') == len(i)-1:
    BA += 1
  elif i.find('B') == 0:
    fB += 1
  elif i.rfind('A') == len(i)-1:
    lA += 1
  cnt += c

# lA_fB = min(lA, fB)  
# r = min(abs(lA - fB), BA)
# n = max(0, (BA -r) // 2)

if BA > 0:
  cnt += BA -1
  if lA > 0:
    cnt += 1
    lA -= 1
  if fB > 0:
    cnt += 1
    fB -= 1

cnt += min(fB, lA)
print(cnt)

Submission status: Wrong Answer

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

if min(bcount,acount) <= bacount:
    count += min(bcount,acount)*2
    bacount -= min(bcount,acount)
    acount -= min(bcount,acount)
    bcount -= min(bcount,acount)
    if bacount > 0:
        count += (acount+bcount+bacount)
else:
    count += min(bcount,acount) + bacount


print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[]
ctr=0
ctr_a=0
ctr_b=0
ctr_ab=0
for i in range(n):
    s.append(str(input()))
    if "AB" in s[i]:
        ctr+=s[i].count("AB")
    if s[i][-1]=="A" and s[i][0]!="B":
        ctr_a+=1
    if s[i][0]=="B" and s[i][-1]!="A":
        ctr_b+=1
    if s[i][-1]=="A" and s[i][0]=="B":
        ctr_ab+=1
if ctr_a==ctr_b==0:
    print(max(ctr+ctr_ab-1,0))
else:
    print(ctr+min(ctr_a,ctr_b))

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

Submission status: Wrong Answer

Solution code:
N = int(input())

count_ab = 0
count_fb = 0
count_ea = 0
count_fbea = 0
for i in range(N):
    s = input()
    if "AB" in s:
        count_ab += 1
    if s[0] == "B" and s[len(s)-1] == "A":
        count_fbea += 1
    elif s[0] == "B":
        count_fb += 1
    elif s[len(s)-1] == "A":
        count_ea += 1

if count_fbea > 0:
    if count_ea + count_fb > 0:
        ans = count_fbea + min(count_fb, count_ea)
    else:
        ans = count_fbea - 1
else:
    ans = min(count_fb, count_ea)

ans += count_ab
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    word = []
    cnt = 0
    cnt_h = 0
    cnt_t = 0
    cnt_ht = 0
    total = 0
    for i in range(N):
        search = input()
        cnt += search.count('AB')
        if search[0] != 'B' and search[-1] !='A':
            continue
        elif search[0] =='B' and search[-1] =='A':
            cnt_ht +=1
        elif search[0] =='B':
            cnt_h +=1
        else:
            cnt_t +=1

    if abs(cnt_t - cnt_h) >=cnt_ht:
        print(cnt + min(cnt_t,cnt_h) + cnt_ht)
    elif cnt_h >= cnt_t:
        print(cnt + cnt_ht -1)
    else:
        print(cnt + cnt_ht)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
ls = 0
rs = 0
lr = 0
for _ in range(N):
    s = str(input())
    if "AB" in s:
        ans += 1
    if s[-1] == "A" and s[0] == "B":
        lr += 1
    elif s[-1] == "A":
        rs += 1
    elif s[0] == "B":
        ls += 1
if ls == 0 and rs == 0:
    ans += lr - 1 
elif ls == 0 and rs != 0:
    ans += lr
elif ls != 0 and rs == 0:
    ans += lr
else:
    lr += 1
    rs -= 1
    ls -= 1
    ans += lr
    ans += min(rs,ls)
#ans += lr
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

count_ab = 0
count_fb = 0
count_ea = 0
count_fbea = 0
for i in range(N):
    s = input()
    if "AB" in s:
        count_ab += 1
    if s[0] == "B" and s[len(s)-1] == "A":
        count_fbea += 1
    elif s[0] == "B":
        count_fb += 1
    elif s[len(s)-1] == "A":
        count_ea += 1

ans = count_ab
if count_fbea > 0:
    ans += count_fbea - 1
    if count_fb > 0:
        count_fb -= 1
        ans += 1
    if count_ea > 0:
        count_ea -= 1
        ans += 1
ans += min(count_fb, count_ea)
print(ans)

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  # AxB

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())

cnt = 0
cnt_BA = 0
cnt_A = 0
cnt_B = 0

for i in range(N) : 
    tmp = (input())
    cnt += tmp.count("AB")
    tmp = tmp[0] +tmp[-1]
    if tmp == "BA" : 
        cnt_BA += 1
    if tmp[-1] == "A" : 
        cnt_A += 1
    if tmp[0] == "B" : 
        cnt_B += 1
    
m = min(cnt_A, cnt_B)

if cnt_BA == cnt_A and cnt_BA == cnt_B : 
    print(m-1+cnt)
else : 
    print(m+cnt)

Submission status: Wrong Answer

Solution code:
N=int(input())

lst = []
for i in range(N):
    lst.append(input())
    
new_lst = [e for e in lst if 'A' in e or 'B' in e]

moji = ','.join(new_lst)
q = moji.count('AB')
# AB のこすうをかぞえる必要があった…
# print(q)

Bstart =[s for s in new_lst if s.startswith('B')]
Aend =[s for s in new_lst if s.endswith('A')]
BstartAend =[s for s in new_lst if s.startswith('B') and s.endswith('A')]

BS = len(Bstart)
AE = len(Aend)
BSAE = len(BstartAend)

p = q + min(BS, AE)
if BS == AE and AE == BSAE:
    p = BS-1
print(p)

Submission status: Wrong Answer

Solution code:
n = int(input())
list = []
for i in range(n):
    keep = input()
    list.append(keep)
double = 0
b = 0
a = 0
include = 0
for j in range(n):
    if list[j][0] == "B" and list[j][-1:] == "A":
        double += 1
    elif list[j][0] == "B":
        b += 1
    elif list[j][-1:] == "A":
        a += 1
for k in range(n):
    keep_len = len(list[k])
    for l in range(keep_len-1):
        if list[k][l] == "A" and list[k][l+1] == "B":
            include += 1
if b == 0 and a == 0:
    print(double-1 + include)
elif b == 0 or a == 0:
    print(double-1 + include + 1)
else:
    print(double-1 + include + min(b, a) + 1)

Submission status: Wrong Answer

Solution code:
N = int(input())
 
moji_list = []
A_count = 0
B_count = 0
AB_count = 0
B_A_count = 0

if N == 1:
    moji = input()
    print(moji.count('AB'))

else:
    for i in range(N):
        moji = input()
        if moji[-1] == 'A' and moji[0] == 'B':
            B_A_count += 1
        elif moji[0] == 'B':
            B_count += 1
        elif moji[-1] == 'A':
            A_count += 1
        
        AB_count += moji.count('AB')

    if B_A_count == 0:
        if A_count >= B_count:
            print(AB_count+B_count)
        else:
            print(AB_count+A_count)
    
    else:
        if A_count > B_count and A_count-B_count > B_A_count:
            print(AB_count+B_count+B_A_count)
        elif A_count > B_count and A_count-B_count <= B_A_count:
            print(AB_count+A_count+B_A_count-1)
        elif A_count < B_count and B_count-A_count > B_A_count:
            print(AB_count+A_count+B_A_count)
        elif A_count < B_count and B_count-A_count <= B_A_count:
            print(AB_count+B_count+B_A_count-1)

        else:
            print(AB_count+B_A_count-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
counter = 0
end_a = 0
start_b = 0
start_b_end_a = 0
for n in range(N):
    s = input()
    for i in range(len(s) - 1):
        if s[i:i + 2] == 'AB':
            counter += 1
    if s[-1] == 'A':
        end_a += 1
    if s[0] == 'B':
        start_b += 1
    if s[-1] == 'A' and s[0] == 'B':
        start_b_end_a += 1
if start_b_end_a == end_a and end_a == start_b:
    print(counter + min([end_a, start_b]) - 1)
else:
    print(counter + min([end_a, start_b]))