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
from collections import defaultdict, deque
import bisect
from heapq import *
from math import factorial, ceil, floor
from collections import defaultdict
sys.setrecursionlimit(200000)
input = sys.stdin.readline

# N, M, = map(int, input().split())
# N = int(input())
# L = [int(v) for v in input().split()]
# L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
# S = input().strip()

N, = map(int, input().split())
L = [list(input().strip())  for _ in range(N)]

b = defaultdict(list)
a = defaultdict(list)
c = defaultdict(list)
for v in L:
    if v[0] == "B":
        if v[-1] == "A":
            c[v[0]].append("".join(v))
        else:
            b[v[0]].append("".join(v))
    else:
        a[v[-1]].append("".join(v))

s = ""
for v in a["A"]:
    s += v

    if len(c["B"]) > 0:
        p = c["B"][0]
        s += p
        c["B"].remove(p)

    if len(b["B"]) > 0:
        p = b["B"][0]
        s += p
        b["B"].remove(p)

for v in b["B"]:
    s += v

for v in c["B"]:
    s += v

for k in a:
    if k == "A":
        continue
    for v in a[k]:
        s += v

print(s.count("AB"))

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
from math import factorial, ceil, floor
from collections import defaultdict
sys.setrecursionlimit(200000)
input = sys.stdin.readline

# N, M, = map(int, input().split())
# N = int(input())
# L = [int(v) for v in input().split()]
# L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
# S = input().strip()

N, = map(int, input().split())
L = [list(input().strip())  for _ in range(N)]

b = defaultdict(set)
a = defaultdict(set)
c = defaultdict(set)
for v in L:
    if v[0] == "B":
        if v[-1] == "A":
            c[v[0]].add("".join(v))
        else:
            b[v[0]].add("".join(v))
    else:
        a[v[-1]].add("".join(v))

s = ""
for v in a["A"]:
    s += v

    if len(c["B"]) > 0:
        p = list(c["B"])[0]
        s += p
        c["B"].remove(p)

    if len(b["B"]) > 0:
        p = list(b["B"])[0]
        s += p
        b["B"].remove(p)

for v in b["B"]:
    s += v

for v in c["B"]:
    s += v

for k in a:
    if k == "A":
        continue
    for v in a[k]:
        s += v

print(s.count("AB"))

Submission status: Wrong Answer

Solution code:
N = int(input())

ans = 0
num_A = 0
num_B = 0
num_BA = 0
for _ in range(N):
  s = input()
  ans += s.count('AB')
  num_A += s.endswith('A')
  num_B += s.startswith('B')
  num_BA += s.startswith('B') * s.endswith('A')

con = min(num_A, num_B)
con -= (num_A == num_B == num_BA)
ans += con
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

ans = 0
num_A = 0
num_B = 0
num_BA = 0
for _ in range(N):
  s = input()
  ans += s.count('AB')
  num_A += s.endswith('A')
  num_B += s.startswith('B')
  num_BA += s.startswith('B') * s.endswith('A')

con = min(num_A, num_B)
ans += con
ans -+ (con == num_BA)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

ans = 0
num_A = 0
num_B = 0
for _ in range(N):
  s = input()
  ans += s.count('AB')
  num_A += s.endswith('A')
  num_B += s.startswith('B')

con = min(num_A, num_B)
ans += con
if con == N:
  ans += -1
print(ans)

Submission status: Wrong Answer

Solution code:
from _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor
# https://atcoder.jp/contests/arc067/tasks/arc067_a
# C - Factors of Factorial
"""
need to consider the case that ticket is not enough to lower everything
"""

N = gi()
ans = 0
cnt = [0] * 3
for i in range(N):
    s = gw()
    if s[0] == 'B':
        if s[-1] == 'A':
            cnt[0] += 1
        else:
            cnt[1] += 1
    elif s[-1] == 'A':
        cnt[2] += 1
    for j in range(len(s) - 1):
        if s[j] == 'A' and s[j + 1] == 'B':
            ans += 1
d = abs(cnt[1] - cnt[2])
ans += min(cnt[1], cnt[2])
ans += min(cnt[0], d)
ans += max(cnt[0] - d, 0)

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
s=[input() for _ in range(N)]
cnt=0

A_count=0
B_count=0
BA_count=0
for u in s:
    cnt+=u.count("AB")
    if u[0]=="B":
        B_count+=1
    if u[-1]=="A":
        A_count+=1
    if u[0]=="B" and u[-1]=="A":
        BA_count+=1
if A_count==B_count==BA_count:
    print(cnt+A_count-1)
else:
    print(cnt+min(A_count,B_count))

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
aend = 0
bstart = 0
aendbstart = 0
for _ in range(n):
   sl = list(input())
   for i in range(len(sl)-1):
      if sl[i] == "A" and sl[i+1] == "B":
         ans += 1
   if sl[0] == "B" and sl[-1] == "A":
      aendbstart += 1
   elif sl[0] == "B":
      bstart += 1
   elif sl[-1] == "A":
      aend += 1

if aend > 0:
   aend -= 1
   ans += aendbstart
   aendbstart = 0
   if bstart > 0:
      ans += 1
      bstart -= 1 
   ans += min(aend, bstart)
else:
   ans += aendbstart - 1
   if bstart > 0:
      ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

N=one_int()

strs=[]
for i in range(N):
    strs.append(one_str())

count = 0
head_B=0
tail_A=0
dual_num=0
for s in strs:
    count +=s.count("AB")
    head_B += 0 if s[0]!="B" else 1
    tail_A += 0 if s[-1]!="A" else 1 
    
    if s[0]=="B" and s[-1]=="A":
        dual_num+=1
    

if dual_num==head_B==tail_A:
    print(min(head_B, tail_A) -1 +count)
else:
    print(min(head_B, tail_A)+count)

Submission status: Wrong Answer

Solution code:
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

N=one_int()

strs=[]
for i in range(N):
    strs.append(one_str())

count = 0
head_B=0
tail_A=0
dual_flg=True
for s in strs:
    count +=s.count("AB")
    head_B += 0 if s[0]!="B" else 1
    tail_A += 0 if s[-1]!="A" else 1 
    
    if s[0]=="B" or s[-1]=="A":
        dual_flg = dual_flg and s[0]=="B" and s[-1]=="A"
    

if dual_flg:
    print(min(head_B, tail_A) -1 +count)
else:
    print(min(head_B, tail_A)+count)

Submission status: Wrong Answer

Solution code:
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

N=one_int()

strs=[]
for i in range(N):
    strs.append(one_str())

count = 0
head_B=0
tail_A=0
dual_flg=True
for s in strs:
    count +=s.count("AB")
    head_B += 0 if s[0]!="B" else 1
    tail_A += 0 if s[-1]!="A" else 1 
    
    if s[0]=="B" or s[-1]=="A":
        if not(s[0]=="B" and s[-1]=="A"):
            dual_flg = False
    

if dual_flg:
    print(min(head_B,tail_A) -1 +count)
else:
    print(min(head_B, tail_A)+count)

Submission status: Wrong Answer

Solution code:
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

N=one_int()

strs=[]
for i in range(N):
    strs.append(one_str())

count = 0
head_B=0
tail_A=0
dual_flg=True
for s in strs:
    count +=s.count("AB")
    head_B += 0 if s[0]!="B" else 1
    tail_A += 0 if s[-1]!="A" else 1 
    
    if s[0]=="B" or s[-1]=="A":
        if not(s[0]=="B" and s[-1]=="A"):
            dual_flg = False
    

if dual_flg:
    print(tail_A -1 +count)
else:
    print(min(head_B, tail_A)+count)

Submission status: Wrong Answer

Solution code:
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

N=one_int()

strs=[]
for i in range(N):
    strs.append(one_str())
count = 0
head_B=0
tail_A=0
dual_flg=True
for s in strs:
    count +=s.count("AB")
    head_B += 0 if s[0]!="B" else 1
    tail_A += 0 if s[-1]!="A" else 1 
    
    if not(s[0]=="B" and s[-1]=="A"):
        dual_flg = False
    

if dual_flg:
    print(tail_A -1 +count)
else:
    print(min(head_B, tail_A)+count)

Submission status: Wrong Answer

Solution code:
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

N=one_int()

strs=[]
for i in range(N):
    strs.append(one_str())

count = 0
head_B=0
tail_A=0
dual_flg=False
for s in strs:
    count +=s.count("AB")
    head_B += 0 if s[0]!="B" else 1
    tail_A += 0 if s[-1]!="A" else 1 


if head_B==tail_A:
    print(tail_A -1 +count)
else:
    print(min(head_B, tail_A)+count)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30

def main():
    n = int(input().strip())
    v = []
    ra = 0
    rb = 0
    both = 0
    ans = 0
    for i in range(n):
        s = input().strip()
        for j in range(len(s)-1):
            if s[j] == 'A' and s[j+1] == 'B':
                ans += 1
        if s[0] != 'B' and s[-1] == 'A':
            ra += 1
        elif s[0] == 'B' and s[-1] != 'A':
            rb += 1
        elif s[0] == 'B' and s[-1] == 'A':
            both += 1

    if ra + rb == 0:
        ans += both - 1
    else:
        ans += min(ra+both, rb+both)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30

def main():
    n = int(input().strip())
    v = []
    ra = 0
    lb = 0
    both = 0
    ans = 0
    for i in range(n):
        s = input().strip()
        for j in range(len(s)-1):
            if s[j] == 'A' and s[j+1] == 'B':
                ans += 1
        if s[0] != 'B' and s[-1] == 'A':
            ra += 1
        elif s[0] == 'B' and s[-1] != 'A':
            lb += 1
        elif s[0] == 'B' and s[-1] == 'A':
            both += 1
    ans += min(ra, lb)
    if abs(ra - lb) < both:
        ans += abs(ra - lb) + both//2
    else:
        ans += both
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

cntA = 0
cntB = 0
cntT = 0
ans = 0
for i in s:
    if i[0] == 'B' and i[-1] == 'A':
        cntT += 1
    else:
        if i[0] == 'B':
            cntB += 1
        if i[-1] == 'A':
            cntA += 1
    ans += i.count('AB')

if cntT > 1:
    ans += cntT - 1
    cntT = 1

if cntA > 0 and cntB > 0:
    ans += 2
    cntT -= 1
    cntA -= 1
    cntB -= 1

if cntT > 0:
    if cntA > 0 and cntA >= cntB:
        cntB += 1
    elif cntB > 0:
        cntA += 1

print(ans + min(cntA, cntB))

Submission status: Wrong Answer

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

cntA = 0
cntB = 0
cntT = 0
ans = 0
for i in s:
    if i[0] == 'B' and i[-1] == 'A':
        cntT += 1
    else:
        if i[0] == 'B':
            cntB += 1
        if i[-1] == 'A':
            cntA += 1
    ans += i.count('AB')

if cntT > 1:
    ans += cntT - 1
    cntT = 1
if cntT > 0:
    if cntA > 0 and cntA >= cntB:
        cntB += 1
    elif cntB > 0:
        cntA += 1

print(ans + min(cntA, cntB))

Submission status: Wrong Answer

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

cntA = 0
cntB = 0
cntT = 0
ans = 0
for i in s:
    if i[0] == 'B' and i[-1] == 'A':
        cntT += 1
    else:
        if i[0] == 'B':
            cntB += 1
        if i[-1] == 'A':
            cntA += 1
    ans += i.count('AB')

if cntT > 1:
    ans += cntT - 1
    cntT = 1

if cntA >= cntB:
    cntB += 1
else:
    cntA += 1

print(ans + min(cntA, cntB))

Submission status: Wrong Answer

Solution code:
N = int(input())

ABcount = 0
Acount = 0
Bcount = 0
Aset = set()
Bset = set()
for i in range(N):
    s = input()

    if s[0] == "B":
        Bcount += 1
        Bset.add(s)

    if s[-1] == "A":
        Acount += 1
        Aset.add(s)
    
    buf = ""
    for char in s:
        if buf + char == "AB":
            ABcount += 1
        buf = char
    
if Acount > Bcount:
    print(ABcount + Bcount)
elif Acount < Bcount:
    print(ABcount + Acount)
else:
    if Aset == Bset:
        print(ABcount + Acount - 1)
    else:
        print(ABcount + Acount)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in range(n)]
cnt_a = 0
cnt_b = 0
ans = 0
for i in range(n):
    if s[i][0] == 'B':
        cnt_b += 1
    if s[i][-1] == 'A':
        cnt_a += 1
    ans += s[i].count('AB')
ans += min(min(cnt_a, cnt_b), n - 1)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in range(n)]
cnt_a = 0
cnt_b = 0
ans = 0
for i in range(n):
    if s[i][0] == 'B':
        cnt_b += 1
    if s[i][-1] == 'A':
        cnt_a += 1
    ans += s[i].count('AB')
ans += min(cnt_a, cnt_b)
print(min(ans, n - 1))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in range(n)]
cnt_a = 0
cnt_b = 0
ans = 0
for i in range(n):
    if s[i][0] == 'B':
        cnt_b += 1
    if s[i][-1] == 'A':
        cnt_a += 1
    ans += s[i].count('AB')
ans += min(cnt_a, cnt_b)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

ans = 0
a = b = b_a = 0

for i in range(n):
    s = input()
    ans += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        b_a += 1
    elif s[0] == "B":
        b += 1
    elif s[-1] == "A":
        a += 1

if b_a > 0:
    if a > 0 and b > 0:
        ans += b_a + 1
        ans += min(a-1, b-1)
    else:
        ans += b_a
elif a > 0 and b > 0:
    ans += min(a, b)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
res = 0
a = 0
b = 0
ab = 0
for _ in range(N):
  s = input()[: -1]
  res += s.count("AB")
  a += s[-1] == "A"
  b += s[0] == "B"
  ab += (s[-1] == "A" and (s[0] == "B"))
ab = ab > 0 and (a == b)

print(res + min(N - 1, a - ab, b - ab))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
res = 0
a = 0
b = 0
for _ in range(N):
  s = input()[: -1]
  res += s.count("AB")
  a += s[-1] == "A"
  b += s[0] == "B"
print(res + min(N - 1, a, b))

Submission status: Wrong Answer

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

for i in range(n):
    si = input()
    s.append(si)
    c = 0
    if si[0] == "B":
        b_start.append(i)
    if si[len(si)-1] == "A":
        a_end.append(i)
    for j in range(1,len(si)):
        if si[j]== "B" and si[j-1] == "A":
            c += 1
    AB_in.append(c)

if len(a_end) == n:
    print(sum(AB_in) + min(n-1,len(b_start)))
else:
    print(sum(AB_in) + min(len(a_end),len(b_start)))

Submission status: Wrong Answer

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

for i in range(n):
    si = input()
    s.append(si)
    c = 0
    if si[0] == "B":
        b_start.append(i)
    if si[len(si)-1] == "A":
        a_end.append(i)
    for j in range(1,len(si)):
        if si[j]== "B" and si[j-1] == "A":
            c += 1
    AB_in.append(c)

print(sum(AB_in) + min(len(a_end),len(b_start)))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = []
ans = 0
a = 0
b = 0
for _ in range(N):
    s = input()
    ans += s.count('AB')
    if s[0] == 'B':
        b += 1
    if s[-1] == 'A':
        a += 1

print(ans + min(a, b))

Submission status: Wrong Answer

Solution code:

def main():
    N = int(input())
    cnt = 0
    ab,a,b = 0,0,0
    for _ in range(N):
        s = input()
        for i in range(len(s)-1):
            if s[i:i+2]=="AB":
                cnt += 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+b == 0:
        print(cnt+ab-1)
    else:
        print(cnt+ab+min(a,b))
        
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:

def main():
    N = int(input())
    cnt = 0
    a,b = 0,0
    for _ in range(N):
        s = input()
        for i in range(len(s)-1):
            if s[i:i+2]=="AB":
                cnt += 1
        if s[0]=="B":
            b +=1
        if s[-1]=="A":
            a +=1
    print(cnt+min(a,b))
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
S=[input() for c in range(N)]
ans=0
xa=0;bx=0;bxa=0;
for s in S:
  ans+=s.count("AB")
  xa+=(s[0]!="B" and s[-1]=="A")
  bx+=(s[0]=="B" and s[-1]!="A")
  bxa+=(s[0]=="B" and s[-1]=="A")
  
print(ans+(bxa-1 if bxa>0 else 0)+(xa>0)+(bx>0) +min(xa-1,bx-1))

Submission status: Wrong Answer

Solution code:
N=int(input())
S=[input() for c in range(N)]
ans=0
xa=0;bx=0;bxa=0;
for s in S:
  ans+=s.count("AB")
  xa+=(s[0]!="B" and s[-1]=="A")
  bx+=(s[0]=="B" and s[-1]!="A")
  bxa+=(s[0]=="B" and s[-1]=="A")
  
print(ans+(bxa-1 if bxa>0 else 0)+(xa>0)+(bx>0) +max(min(xa-1,bx-1),0))

Submission status: Wrong Answer

Solution code:
N=int(input())
S=[input() for c in range(N)]
ans=0
xa=0;bx=0;bxa=0;
for s in S:
  ans+=s.count("AB")
  xa+=(s[0]!="B" and s[-1]=="A")
  bx+=(s[0]=="B" and s[-1]!="A")
  bxa+=(s[0]=="B" and s[-1]=="A")
  
print(ans+max(bxa-1,0)+(xa>0)+(bx>0) +max(min(xa-1,bx-1),0))

Submission status: Wrong Answer

Solution code:
N=int(input())
S=[input() for c in range(N)]
ans=0
xa=0;bx=0;bxa=0;
for s in S:
  ans+=s.count("AB")
  xa+=(s[0]!="B" and s[-1]=="A")
  bx+=(s[0]=="B" and s[-1]!="A")
  bxa+=(s[0]=="B" and s[-1]=="A")
  
print(ans+(bxa-1)+(xa>0)+(bx>0) +max(min(xa-1,bx-1),0))

Submission status: Wrong Answer

Solution code:
N=int(input())
S=[input() for c in range(N)]
ans=0
xa=0;bx=0;bxa=0;
for s in S:
  ans+=s.count("AB")
  xa+=(s[0]!="B" and s[-1]=="A")
  bx+=(s[0]=="B" and s[-1]!="A")
  bxa+=(s[0]=="B" and s[-1]=="A")
  
print(ans+(bxa-1)+(xa>0)+(bx>0) +min(min(xa-1,bx-1),0))

Submission status: Wrong Answer

Solution code:
N = int(input())
cntAB = 0
A, B = [], []
for i in range(N):
  s = input()
  cntAB += s.count('AB')
  if s[0] == 'B': B.append(i)
  if s[-1] == 'A': A.append(i)

setAB = set(A).intersection(B)
if len(setAB) > 0 and len(A)==len(B):
    print(cntAB - 1 + len(A))
else:
  print(cntAB+min(len(A),len(B),N-1))

Submission status: Wrong Answer

Solution code:
N = int(input())
cntAB = 0
A, B = [], []
for i in range(N):
  s = input()
  cntAB += s.count('AB')
  if s[0] == 'B': B.append(i)
  if s[-1] == 'A': A.append(i)

setAB = set(A).intersection(B)
len_setAB = len(setAB)
if len_setAB > 0:
  if len(A)==len(B):
    print(cntAB + len_setAB - 1 + min(len(A),len(B))-len_setAB)
  else:
    print(cntAB + len_setAB + min(len(A),len(B))-len_setAB)
else:
  print(cntAB+min(len(A),len(B),N-1))

Submission status: Wrong Answer

Solution code:
N = int(input())
cntAB = 0
A, B = [], []
for i in range(N):
  s = input()
  cntAB += s.count('AB')
  if s[0] == 'B': B.append(i)
  if s[-1] == 'A': A.append(i)

setAB = set(A).intersection(B)
len_setAB = len(setAB)
if len_setAB > 0:
  print(cntAB + len_setAB - 1 + min(len(A)+1-len_setAB,len(B)+1-len_setAB))
else:
  print(cntAB+min(len(A),len(B),N-1))

Submission status: Wrong Answer

Solution code:
N = int(input())
cntAB = 0
A, B = [], []
for i in range(N):
  s = input()
  cntAB += s.count('AB')
  if s[0] == 'B': B.append(i)
  if s[-1] == 'A': A.append(i)

if len(A) == 1 and len(B) == 1 and A[0] and B[0]:
  print(cntAB)
else:
  print(cntAB+min([len(A),len(B),N-1]))

Submission status: Wrong Answer

Solution code:
N = int(input())
cntAB = 0
cntA = 0
cntB = 0
for _ in range(N):
  s = input()
  cntAB += s.count('AB')
  if s[0] == 'B': cntB += 1
  if s[-1] == 'A': cntA += 1

print(cntAB+min([cntA,cntB,N-1]))

Submission status: Wrong Answer

Solution code:
N = int(input())
cntAB = 0
cntA = 0
cntB = 0
for _ in range(N):
  s = input()
  cntAB += s.count('AB')
  if s[0] == 'B': cntB += 1
  if s[-1] == 'A': cntA += 1

print(cntAB+min(cntA,cntB))

Submission status: Wrong Answer

Solution code:
import re

n = int(input())
ss = [input() for _ in range(n)]

count_ab = 0
b_a = 0
__a = 0
b__ = 0

for si in ss:
  count_ab += len(re.findall('AB',si))
  if si[0]=='B' and si[-1]=='A':
    b_a += 1
  elif si[0]=='B':
    b__ += 1
  elif si[-1]=='A':
    __a += 1
  else:
    pass

count_ab += min(__a, b__)
if abs(__a - b__) > 0 and b_a > 0:
  count_ab +=1

if b_a > 0:  
  count_ab += b_a - 1

print(count_ab)

Submission status: Wrong Answer

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


def main():
    N = int(input())
    ba = []
    ca = []
    bc = []
    cc = []
    for _ in range(N):
        s = input().strip()
        b = (s[0] == "B")
        a = (s[-1] == "A")
        if b and a:
            ba.append(s)
        elif not b and a:
            ca.append(s)
        elif b and not a:
            bc.append(s)
        else:
            cc.append(s)
    S = ""
    while ba or ca or bc:
        if ca:
            S += ca.pop()
        if ba:
            S += ba.pop()
        if bc:
            S += bc.pop()
    for s in cc:
        S += s
    print(S.count("AB"))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
ba=bx=xa=0
ans=0
for i in range(N):
    s=input()
    if 'AB' in s:
        ans+=1
    if s.startswith('B') and s.endswith('A'):
        ba+=1
    elif s.startswith('B'):
        bx+=1
    elif s.endswith('A'):
        xa+=1

if ba==0:
    ans+=min(bx,xa)
else:
    if bx+xa>0:
        ans+=ba+min(bx,xa)
    else:
        ans+=ba-1

print(ans)

Submission status: Wrong Answer

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

ret = 0
a, b, c = 0, 0, 0
for i in range(N) :
  ret += s[i].count('AB')
  if s[i][-1] == 'A' and s[i][0] == 'B' :
    c += 1
  elif s[i][-1] == 'A' :
    a += 1
  elif s[i][0] == 'B' :
    b += 1
    
if c >= 2 :
  ret += c - 1
  a += 1
  b += 1
  print(ret + min(a, b))
  
else :
  if a == b :
    print(ret + a + c)
  else :
    print(ret + max(min(a + c, b), min(a, b + c)))

Submission status: Wrong Answer

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

ret = 0
a, b, c = 0, 0, 0
for i in range(N) :
  ret += s[i].count('AB')
  if s[i][-1] == 'A' and s[i][0] == 'B' :
    c += 1
  elif s[i][-1] == 'A' :
    a += 1
  elif s[i][0] == 'B' :
    b += 1
    
if c >= 2 :
  ret += c - 1
  a += 1
  b += 1
  print(ret + min(a, b))
  
else :
  print(ret + max(min(a + c, b), min(a, b + c)))

Submission status: Wrong Answer

Solution code:
N=int(input())
S=[input() for c in range(N)]
ans=0
xa=0;bx=0;bxa=0;
for s in S:
  ans+=s.count("AB")
  xa+=(s[0]!="B" and s[-1]=="A")
  bx+=(s[0]=="B" and s[-1]!="A")
  bxa+=(s[0]=="B" and s[-1]=="A")
  
print(ans+max(0,bxa-1)+min(xa,bx)+(xa!=bx and bxa>0))

Submission status: Wrong Answer

Solution code:
import sys
import re
n = int(input())
a = 0
b = 0
a_b = 0
cnt = 0
for i in range(n):
    s = sys.stdin.readline().strip()
    cnt += len(re.findall("AB", s))
    a += s[0] == "B"
    b += s[-1] == "A"
    a_b += (s[0] == "B" and s[-1] == "A")
plus = 0
if a == b and a_b == a:
    plus -= 1
plus += min(a, b)
print(cnt + plus)

Submission status: Wrong Answer

Solution code:
import sys
import re
n = int(input())
a = 0
b = 0
a_b = 0
cnt = 0
for i in range(n):
    s = sys.stdin.readline().strip()
    cnt += len(re.findall("AB", s))
    a += s[0] == "B"
    b += s[-1] == "A"
    a_b += s[0] == "B" and  s[-1] == "A"
plus = 0
if a == b and a_b == a:
    plus -= 1
plus += min(a, b)
print(cnt + plus)

Submission status: Wrong Answer

Solution code:
n = int(input())
word_list = []
count = 0
top_b = 0
bottom_a = 0

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

for i in word_list:
    if i[0] == 'B':
        top_b += 1
    if i[-1] == 'A':
        bottom_a += 1
    for j in range(len(i)-1):
        if i[j] == 'A' and i[j+1] == 'B':
            count += 1

count += min(top_b, bottom_a, n-1)

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
word_list = []
count = 0
top_b = 0
bottom_a = 0

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

for i in word_list:
    if i[0] == 'B':
        top_b += 1
    if i[-1] == 'A':
        bottom_a += 1
    if 'AB' in i:
        count += 1

count += min(top_b, bottom_a)

print(count)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read

N, *s = map(str, read().split())
answer = 0
first = 0
last = 0
BA = 0
for i in s:
    answer += i.count('AB')
    if i[0] == 'B' and i[-1] == 'A':
        BA += 1
        continue

    if i[0] == 'B':
        first += 1
    if i[-1] == 'A':
        last += 1

answer += BA - 1
if first:
    answer += 1
    first -= 1
if last:
    answer += 1
    last -= 1

answer += min(first, last)
print(answer)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read

N, *s = map(str, read().split())
answer = 0
first = 0
last = 0
BA = 0
for i in s:
    answer += i.count('AB')
    if i[0] == 'B' and i[-1] == 'A':
        BA += 1
        continue

    if i[0] == 'B':
        first += 1
    if i[-1] == 'A':
        last += 1

answer += BA + min(first, last)
print(answer)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read

N, *s = map(str, read().split())
answer = 0
first = 0
last = 0
for i in s:
    answer += i.count('AB')
    if i[0] == 'B':
        first += 1
    if i[-1] == 'A':
        last += 1

if first == last == N:
    print(answer + first - 1)
else:
    print(answer + min(first, last))

Submission status: Wrong Answer

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

for _ in range(n):
    S.append(input())
sum_=0
a=0
b=0
c=0
for s in S:
    sum_ += s.count("AB")
    if s[0] =="B" and s[-1] =="A":
        c+=1
    elif s[0] =="B" and s[-1] !="A":
        b+=1
    elif s[-1] =="A" and s[0] !="B":
        a+=1
if a+b ==0:
    print(sum_+ c-1)
else:
    print(sum_+ c+min(a,b))

Submission status: Wrong Answer

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

for _ in range(n):
    S.append(input())
sum_=0
a=0
b=0
for s in S:
    sum_ += s.count("AB")
    if s[0] =="B":
        b+=1
    if s[-1] =="A":
        a+=1
print(sum_+min(min(a,b),n-1))

Submission status: Wrong Answer

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

for _ in range(n):
    S.append(input())
sum_=0
a=0
b=0
for s in S:
    sum_ += s.count("AB")
    if s[0] =="B":
        b+=1
    if s[-1] =="A":
        a+=1
print(sum_+min(a,b))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = 0
b = 0
ab = 0
for i in range(n):
  s = input()
  if s[0] == 'B':
    b += 1
  if s[-1] == 'A':
    a += 1
  cur = s[0]
  for i in s[1:]:
    pre,cur = cur,i
    if pre+cur == 'AB':
      ab += 1
print(ab+min(n-1,a,b))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = 0
b = 0
ab = 0
for i in range(n):
  s = input()
  if s[0] == 'B':
    b += 1
  if s[-1] == 'A':
    a += 1
  cur = s[0]
  for i in s[1:]:
    pre,cur = cur,i
    if pre+cur == 'AB':
      ab += 1
print(ab+min(n,a,b))

Submission status: Wrong Answer

Solution code:
N = int(input())
AB = 0
B_ = 0
_A = 0
B_A = 0
for i in range(N):
    s = input()
    AB += s.count('AB')
    if s[0]=='B':
        B_ += 1
    if s[-1]=='A':
        _A += 1
    if s[0]=='B' and s[-1]=='A':
        B_A += 1
if B_==_A==B_A:
    print(AB+B_A-1)
else:
    print(AB+min(B_,_A))

Submission status: Wrong Answer

Solution code:
n = int(input())

inAB = 0
B_A = 0
x_A = 0
B_x = 0

for _ in range(n):
    s = input()
    if s[0] == "B" and s[-1] == "A":
        B_A += 1
    elif s[0] == "B":
        B_x += 1
    elif s[-1] == "A":
        x_A += 1
    inAB += s.count("AB")

ans = inAB
ans += B_A
ans += min(x_A, B_x)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=0
aend=0
bstart=0
aendbstart=0
for i in range(n):
  s=input()
  ans+=s.count("AB")
  if s[0]=="B" and s[-1]=="A":
    aendbstart+=1
  elif s[0]=="B":
    bstart+=1
  elif s[-1]=="A":
    aend+=1
if aend>0 and bstart>0:
  aendbstart+=min(aend,bstart)
  aend-=min(aend,bstart)
  bstart-=min(aend,bstart)
ans+=(aendbstart-1+(max(aend,bstart)>0))
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = 0
B = 0
AB = 0
ans = 0
S = [ input() for i in range(n)]
Bs = []
As = []
ABs = []
#print()
for i in range(n):
    s = S[i]
    for i in range(len(s)-1):
        if s[i] == "A" and s[i+1] == "B":
            ans += 1
            #print(s)
    if s[0] == "B" and s[-1] == "A":
        AB += 1
        ABs.append(s)
    elif s[0] == "B":
        B += 1
        Bs.append(s)
    elif s[-1] == "A":
        A += 1
        As.append(s)
#print(ABs)
#print(As)
#print(Bs)
#print(ans)
ans += AB
#print(ans)
if AB > 0:
    if A > 1 and B > 1:
        ans += min(A-1,B-1) + 1
    elif A > 1 and B == 0:
        ans += 1
    elif B > 1 and A == 0:
        ans += 1
else:
    ans += min(A,B)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
cnt = 0
starts_B = 0
B_and_A = 0
ends_A = 0
for _ in range(N):
    S = input()
    for i in range(1, len(S)):
        if S[i - 1] == 'A' and S[i] == 'B':
            cnt += 1
    if S[0] == 'B' and S[-1] == 'A':
        B_and_A += 1
    elif S[0] == 'B':
        starts_B += 1
    elif S[-1] == 'A':
        ends_A += 1

if B_and_A > 0:
    cnt += B_and_A - 1
if starts_B == ends_A:
    cnt += starts_B
else:
    cnt += min(starts_B, ends_A)
    if abs(starts_B - ends_A) > 0 and B_and_A > 0:
        cnt += 1

print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
cnt = 0
starts_B = 0
B_and_A = 0
ends_A = 0
for _ in range(N):
    S = input()
    for i in range(1, len(S)):
        if S[i - 1] == 'A' and S[i] == 'B':
            cnt += 1
    if S[0] == 'B' and S[-1] == 'A':
        B_and_A += 1
    elif S[0] == 'B':
        starts_B += 1
    elif S[-1] == 'A':
        ends_A += 1

if B_and_A > 0:
    cnt += B_and_A - 1
if starts_B == ends_A:
    cnt += starts_B
else:
    cnt += min(starts_B, ends_A)
    if B_and_A > 0:
        cnt += 1

print(cnt)