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

b = 0
a = 0
ab = 0
ex = 0

for i in range(n) :
    tmp = input()
    ab += tmp.count('AB')
    if tmp[0] == 'B' :
        b += 1
    if tmp[-1] == 'A' :
        a += 1
    if tmp[0] == 'B' and tmp[-1] == 'A' :
        ex += 1

if max(a, b) > ex :
    print(ab + min(a, b))
else :
    print(ab + min(a, b) - 1)

Submission status: Wrong Answer

Solution code:
n = int(input())

head_b = 0
last_a = 0
ab = 0
ex = 0

for i in range(n) :
    tmp = input()
    ab += tmp.count('AB')
    if tmp[0] == 'B' :
        head_b += 1
    if tmp[len(tmp) - 1] == 'A' :
        last_a += 1
    if tmp[0] == 'B' and tmp[len(tmp) - 1] == 'A' :
        ex = 1

print(ab + min(head_b, last_a) - ex)

Submission status: Wrong Answer

Solution code:
n = int(input())

head_b = 0
last_a = 0
ab = 0
ex = -1

for i in range(n) :
    tmp = input()
    ab += tmp.count('AB')
    if tmp[0] == 'B' :
        head_b += 1
    if tmp[len(tmp) - 1] == 'A' :
        last_a += 1
    if tmp[0] == 'B' and tmp[len(tmp) - 1] == 'A' :
        ex += 1

print(ab + min(head_b, last_a) - ex)

Submission status: Wrong Answer

Solution code:
n = int(input())

head_b = 0
last_a = 0
ab = 0

for i in range(n) :
    tmp = input()
    ab += tmp.count('AB')
    if tmp[0] == 'B' :
        head_b += 1
    if tmp[len(tmp) - 1] == 'A' :
        last_a += 1

if head_b == last_a :
    ex = head_b - 1
else :
    ex = min(head_b, last_a)

print(ab + ex)

Submission status: Wrong Answer

Solution code:
n = int(input())

head_b = 0
last_a = 0
ab = 0

for i in range(n) :
    tmp = input()
    ab += tmp.count('AB')
    if tmp[0] == 'B' :
        head_b += 1
    if tmp[len(tmp) - 1] == 'A' :
        last_a += 1

print(ab + min(head_b, last_a))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
a =0
b = 0
ans = 0
cnt = 0
for i in range(N):
  s = input()
  if s[0]=="B" and s[-1]=="A":
    cnt+=1
    if a>0 and b>0:
      ans+=2
      a-=1
      b-=1
    elif a>0:
      ans+=1
    elif b>0:
      ans+=1
    else:
      a+=1
      b+=1
  elif s[0]=="B":
    if a>0:
      ans+=1
      a-=1
    else:
      b+=1
  elif s[1]=="A":
    if b>0:
      ans+=1
      b-=1
    else:
      a+=1
  ans+=s.count("AB")
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())

B_ = 0
_A = 0
BA = 0
inner = 0

for i in range(N):
    s = list(input())
    for j in range(1,len(s)):
        if s[j-1] == "A" and s[j] == "B":
            inner += 1
    if s[0] == "B" and s[-1] == "A":
        BA += 1
    elif s[0] == "B":
        B_ += 1
    elif s[-1] == "A":
        _A += 1

print(BA,B_,_A)

if BA > 0:
    print(BA + 1 + min(B_-1,_A-1,B_+_A-2) + inner)
else:
    print(min(B_,_A)+inner)

Submission status: Wrong Answer

Solution code:
N = int(input())

B_ = 0
_A = 0
BA = 0
inner = 0

for i in range(N):
    s = list(input())
    for j in range(1,len(s)):
        if s[j-1] == "A" and s[j] == "B":
            inner += 1
    if s[0] == "B" and s[-1] == "A":
        BA += 1
    elif s[0] == "B":
        B_ += 1
    elif s[-1] == "A":
        _A += 1

if BA > 0:
    print(BA + 1 + min(B_-1,_A-1) + inner)
else:
    print(min(B_,_A)+inner)

Submission status: Wrong Answer

Solution code:
N = int(input())

b_start = 0
a_end = 0
inner = 0

for i in range(N):
    s = list(input())
    for j in range(1,len(s)):
        if s[j-1] == "A" and s[j] == "B":
            inner += 1
    if s[0] == "B":
        b_start += 1
    if s[-1] == "A":
        a_end += 1

print(inner + min(N-1,a_end,b_start))

Submission status: Wrong Answer

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

na=0
a=0
b=0
ry=0
for s in S:
  na+=s.count("AB")
  if s[0]=="B" and s[-1]=="A":
    ry+=1
  elif s[0]=="B":
    b+=1
  elif s[-1]=="A":
    a+=1
    
if a==0 and b==0:
  print(na+ry-1)
else:
  print(na+ry+1+min(a-1,b-1))

Submission status: Wrong Answer

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

na=0
a=0
b=0
ry=0
for s in S:
  na+=s.count("AB")
  if s[0]=="B" and s[-1]=="A":
    ry+=1
  elif s[0]=="B":
    b+=1
  elif s[-1]=="A":
    a+=1
  
print(na+ry+1+min(a-1,b-1))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N = int(input())
S = [input()[:-1] for _ in range(N)]
ans = 0

end_A = 0
start_B = 0
for s in S:
    if s[0] == "B":
        start_B += 1
    if s[-1] == "A":
        end_A += 1
    for a, b in zip(s, s[1:]):
        if a == "A" and b == "B":
            ans += 1

end_A = min(end_A, N-1)
start_B = min(start_B, N-1)
print(ans+min(end_A, start_B))

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N=int(input())
a,A,B,C=0,0,0,0
for i in range(N):
    s=input()
    for j in range(len(s)-1):
        if s[j]=="A" and s[j+1]=="B":
            a+=1
    if s[0]=="B":
        A+=1
    if s[-1]=="A":
        B+=1
        if s[0]=="B":
            C+=1
if A==B==C:
    a+=C-1
else:
    a+=min(A,B)
print(a)

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N=int(input())
a,A,B=0,0,0
for i in range(N):
    s=input()
    for j in range(len(s)-1):
        if s[j]=="A" and s[j+1]=="B":
            a+=1
    if s[0]=="B":
        A+=1
    if s[-1]=="A":
        B+=1
print(a+min(A,B,N-1))

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N=int(input())
a,A,B=0,0,0
for i in range(N):
    s=input()
    for j in range(len(s)-1):
        if s[j]=="A" and s[j+1]=="B":
            a+=1
    if s[0]=="B":
        A+=1
    if s[-1]=="A":
        B+=1
print(a+min(A,B))

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    ls = [input() for _ in range(n)]
    acount = 0
    bcount = 0
    abcount = 0
    bacount = 0
    for s in ls:
        abcount += s.count("AB")
        if s[0] =="B" and s[-1] =="A":bacount += 1
        elif s[0] =="B":bcount += 1
        elif s[-1] =="A":acount += 1

    if acount == 0 and bcount == 0:
        print(abcount+bacount-1)
    elif acount == 0 or bcount == 0:
        print(abcount+bacount)
    else:
        print(abcount+bacount+min(acount,bcount))

main()

Submission status: Wrong Answer

Solution code:
n = int(input())
ls = [input() for _ in range(n)]
acount = 0
bcount = 0
abcount = 0
for s in ls:
    abcount += s.count("AB")
    if s[0] =="B":bcount += 1
    if s[-1] =="A":acount += 1

aplusb = min(acount,bcount)
if aplusb == n:
    aplusb = n-1

print(abcount+aplusb)

Submission status: Wrong Answer

Solution code:
n = int(input())
ls = [input() for _ in range(n)]
acount = 0
bcount = 0
abcount = 0
for s in ls:
    abcount += s.count("AB")
    if s[0] =="B":bcount += 1
    if s[-1] =="A":acount += 1

aplusb = min(acount,bcount)
if aplusb == n:
    aplusb == n-1

print(abcount+aplusb)

Submission status: Wrong Answer

Solution code:
# 2019/10/14
import sys
input=sys.stdin.readline

n=int(input())
cnt=0
b_a=0
a=0
b=0
for i in range(n):
    s=input().rstrip('\n')
    cnt+=s.count('AB')
    is_b=s[0]=='B'
    is_a=s[-1]=='A'
    if is_b or is_a:
        if is_b and is_a:b_a+=1
        elif is_b:b+=1
        else:a+=1
    

cnt+=min(a,b)+max(b_a-1,0)
if abs(a-b)>0 and b_a>0:
    cnt+=1

print(cnt)

Submission status: Wrong Answer

Solution code:
# 2019/10/14
import sys
input=sys.stdin.readline

n=int(input())
cnt=0
b_a=0
a=0
b=0
for i in range(n):
    s=input().rstrip('\n')
    cnt+=s.count('AB')
    is_b=s[0]=='B'
    is_a=s[-1]=='A'
    if is_b or is_a:
        if is_b and is_a:b_a+=1
        elif is_b:b+=1
        else:a+=1
    

cnt+=min(a,b)+max(b_a-1,0)
if abs(a-b)>0:
    cnt+=1

print(cnt)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  ans=0

  _ba=0
  _b=0
  _a=0
  for _ in range(n):
    s=S()
    ans+=s.count('AB')

    if s[0]=='B':
      if s[-1]=='A':
        _ba+=1
      else:
        _b+=1
    else:
      if s[-1]=='A':
        _a+=1

  ans+=max(0,_ba-1)

  # if _ba>0:
  #   if _b>0:
  #     ans+=1
  #     _b-=1
  #   if _a>0:
  #     ans+=1
  #     _a-=1

  if _ba>0:
    _b+=1
    _a+=1

  if _b>0 and _a>0:
    ans+=min(_b,_a)

  return ans

# main()
print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  ans=0

  _ba=0
  _b=0
  _a=0
  for _ in range(n):
    s=S()
    ans+=s.count('AB')

    if s[0]=='B':
      if s[-1]=='A':
        _ba+=1
      else:
        _b+=1
    else:
      if s[-1]=='A':
        _a+=1

  ans+=max(0,_ba-1)

  if _ba>0:
    _b+=1
    _a+=1

  ans+=min(_b,_a)

  return ans

# main()
print(main())

Submission status: Wrong Answer

Solution code:
N=int(input())

if N==1:
    print(input().count("AB"))
    exit()

BA=0
B_=0
_A=0
cnt=0

for i in range(N):
    s=input()
    L=len(s)

    if s[0] == "B" and s[-1] == "A":
        BA += 1
    elif s[0] == "B" and s[-1] != "A":
        B_ += 1
    elif s[-1] != "B" and s[-1] == "A":
        _A += 1
    cnt += s.count("AB")


if _A==0 and B_==0:
    ans=BA-1+cnt
elif (_A!=0 and B_==0) or (_A==0 and B_!=0):
    ans=BA+cnt

else:
    ans=BA+1+min(_A-1,B_-1)+cnt

print(ans)

Submission status: Wrong Answer

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

tailA = 0
headB = 0
same = 0
inside = 0
for s in S:
    inside += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        same += 1
    else:
        if s[0] == 'B':
            headB += 1
        if s[-1] == 'A':
            tailA += 1

ans = 0
if tailA >= 1:
    ans += same
    tailA -= 1
else:
    ans += max(same - 1, 0)

if headB >= 1 and same >= 1:
    ans += 1
    headB -= 1

ans += max(min(tailA, headB), 0)
print(ans + inside)

Submission status: Wrong Answer

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

tailA = 0
headB = 0
same = 0
inside = 0
for s in S:
    inside += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        same += 1
    elif s[0] == 'B':
        headB += 1
    elif s[-1] == 'A':
        tailA += 1

ans = 0
if tailA >= 1:
    ans += same
    tailA -= 1
else:
    ans += max(same - 1, 0)

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

ans += max(min(tailA, headB), 0)
print(ans + inside)

Submission status: Wrong Answer

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

tailA = 0
headB = 0
same = 0
inside = 0
for s in S:
    inside += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        same += 1
    elif s[0] == 'B':
        headB += 1
    elif s[-1] == 'A':
        tailA += 1

ans = 0
if tailA >= 1:
    ans += same
    tailA -= 1
else:
    ans += max(same - 1, 0)

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

ans += min(tailA, headB)
print(ans + inside)

Submission status: Wrong Answer

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

tailA = 0
headB = 0
same = 0
inside = 0
for s in S:
    inside += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        same += 1
    elif s[0] == 'B':
        headB += 1
    elif s[-1] == 'A':
        tailA += 1

ans = 0
if tailA >= 1:
    ans += same
    tailA -= 1

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

ans += min(tailA, headB)
print(ans + inside)

Submission status: Wrong Answer

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

tailA = 0
headB = 0
same = 0
inside = 0
for s in S:
    inside += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        same += 1
    elif s[0] == 'B':
        headB += 1
    elif s[-1] == 'A':
        tailA += 1

ans = min(tailA, headB)
tailA -= ans
headB -= ans
K = min(max(tailA, headB), same)
ans += K
same -= K
ans += max(same - 1, 0)

if tailA >= 1:
    ans += 1

print(ans + inside)

Submission status: Wrong Answer

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

tailA = 0
headB = 0
same = 0
inside = 0
for s in S:
    inside += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        same += 1
    elif s[0] == 'B':
        headB += 1
    elif s[-1] == 'A':
        tailA += 1

ans = min(tailA, headB)
tailA -= ans
headB -= ans
K = min(max(tailA, headB), same)
ans += K
same -= K
ans += same // 2

if tailA >= 1:
    ans += 1

print(ans + inside)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=0
b=0
malti=0
origin=0
for i in range(n):
    s=input()
    if s[0]=="B" and s[-1]=="A":
        malti+=1
    elif s[0]=="B" and s[-1]!="A":
        b+=1
    elif s[0]!="B" and s[-1]=="A":
        a+=1
    for i in range(len(s)-1):
        if s[i]=="A" and s[i+1]=="B":
            origin+=1
if a>=1 and b>=1:
    print(origin+malti+1 +min(a-1,b-1))
elif a==0 and b>=1:
    print(origin+malti)
elif a>=1 and b==0:
    print(origin+malti)
elif a==0 and b==0:
    print(origin+min(malti-1,0))

Submission status: Wrong Answer

Solution code:
N = int(input())
listS = ["" for _ in range(N)]
cntAB = 0
for i in range(N):
    listS[i] = input()
    for j in range(len(listS[i])-1):
        if listS[i][j] == "A" and listS[i][j+1] == "B":
            cntAB += 1
cntA = 0
cntB = 0
BcntA = 0
for s in listS:
    if s[0] == "B" and s[-1] != "A":
        cntB += 1
    if s[0] != "B" and s[-1] == "A":
        cntA += 1
    if s[0] == "B" and s[-1] == "A":
        BcntA += 1
connect = 0
if cntA + cntB == 0:
    connect = BcntA - 1
else:
    connect = min(cntA,cntB) + BcntA
print(connect+cntAB)

Submission status: Wrong Answer

Solution code:
N = int(input())
listS = ["" for _ in range(N)]
cntAB = 0
for i in range(N):
    listS[i] = input()
    for j in range(len(listS[i])-1):
        if listS[i][j] == "A" and listS[i][j+1] == "B":
            cntAB += 1
cntA = 0
cntB = 0
BcntA = 0
for s in listS:
    if s[0] == "B" and s[-1] != "A":
        cntB += 1
    if s[-1] == "A" and s[0] != "B":
        cntA += 1
    if s[0] == "B" and s[-1] == "A":
        BcntA += 1
connect = 0
if min(cntA,cntB) == 0:
    connect = BcntA - 1
else:
    connect = min(cntA,cntB) + BcntA
print(connect+cntAB)

Submission status: Wrong Answer

Solution code:
N = int(input())
listS = ["" for _ in range(N)]
cntAB = 0
for i in range(N):
    listS[i] = input()
    for j in range(len(listS[i])-1):
        if listS[i][j] == "A" and listS[i][j+1] == "B":
            cntAB += 1
cntA = 0
cntB = 0
BcntA = 0
for s in listS:
    if s[0] == "B":
        cntB += 1
    if s[-1] == "A":
        cntA += 1
    if s[0] == "B" and s[-1] == "A":
        BcntA += 1
connect = 0
if min(cntA,cntB) == BcntA:
    connect = BcntA-1
else:
    connect = min(cntA,cntB)
print(connect+cntAB)

Submission status: Wrong Answer

Solution code:
N = int(input())
listS = ["" for _ in range(N)]
cntAB = 0
for i in range(N):
    listS[i] = input()
    for j in range(len(listS[i])-1):
        if listS[i][j] == "A" and listS[i][j+1] == "B":
            cntAB += 1
cntA = 0
cntB = 0
for s in listS:
    if s[0] == "B":
        cntB += 1
    if s[-1] == "A":
        cntA += 1
connect = 0
if min(cntA,cntB) == N:
    connect = N-1
else:
    connect = min(cntA,cntB)
print(connect+cntAB)

Submission status: Wrong Answer

Solution code:
n=int(input())
S=[input() for i in range(n)]
a,b,c=0,0,0
cnt=0
for i in S:
    if i[0]=="B" and i[-1]=="A":c +=1
    elif i[0]=="B":a +=1
    elif i[-1]=="A":b +=1
    for j in range(len(i)-1):
        if i[j]+i[j+1]=="AB":cnt +=1

AB=min(a,b)
a,b=a-AB,b-AB
cnt +=AB
if c!=0:
    cnt +=c
    if a==0 and b==0:
        cnt -=1

print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
S=[input() for i in range(n)]
a,b,c=0,0,0
cnt=0
for i in S:
    if i[0]=="B" and i[-1]=="A":c +=1
    elif i[0]=="B":a +=1
    elif i[-1]=="A":b +=1
    for j in range(len(i)-1):
        if i[j]+i[j+1]=="AB":cnt +=1

AB=min(a,b)
a,b=a-AB,b-AB
cnt +=AB
if not c==0:
    cnt +=c-1
    if a!=0 or b!=0:
        cnt +=1
print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
S=[input() for i in range(n)]
a,b,c=0,0,0
cnt=0
for i in S:
    if i[0]=="B" and i[-1]=="A":c +=1
    elif i[0]=="B":a +=1
    elif i[-1]=="A":b +=1
    for j in range(len(i)-1):
        if i[j]+i[j+1]=="AB":cnt +=1

AB=min(a,b)
a,b=a-AB,b-AB
if not c==0:
    cnt +=c-1
    if a!=0 or b!=0:
        cnt +=1
print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
S=[input() for i in range(n)]
a,b,c=0,0,0
cnt=0
for i in S:
    if i[0]=="B" and i[-1]=="A":c +=1
    elif i[0]=="B":a +=1
    elif i[-1]=="A":b +=1
    for j in range(len(i)-1):
        if i[j]+i[j+1]=="AB":cnt +=1

if not c==0:
    cnt +=c-1
    a +=1
    b +=1
cnt +=min(a,b)
print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
S=[input() for i in range(n)]
a,b,c=0,0,0
cnt=0
for i in S:
    if i[0]=="B" and i[-1]=="A":c +=1
    elif i[0]=="B":a +=1
    elif i[-1]=="A":b +=1
    for j in range(len(i)-1):
        if i[j]+i[j+1]=="AB":cnt +=1

fir=min(a,b)
sec=min(c,max(a-fir,b-fir))
cnt +=fir
cnt +=sec
cnt +=c-sec
print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
ba,a,b,ab=0,0,0,0
for i in range(n):
    s=input()
    if s[0]=='B' and s[-1]=='A':
        ba+=1
    elif s[0]=='B':
        b+=1
    elif s[-1]=='A':
        a+=1
    for i in range(len(s)-1):
        if s[i]=='A' and s[i+1]=='B':
            ab+=1

print(min(a,b)+ba+ab)

Submission status: Wrong Answer

Solution code:
n=int(input())
ba,a,b,ab=0,0,0,0
for i in range(n):
    s=input()
    if s[0]=='B' and s[-1]=='A':
        ba+=1
    elif s[0]=='B':
        b+=1
    elif s[-1]=='A':
        a+=1
    for i in range(len(s)-1):
        if s[i]=='A' and s[i+1]=='B':
            ab+=1
x=ba
if a==b:
    x-=1
if ba==0:
    x=0
print(min(a,b)+x+ab)

Submission status: Wrong Answer

Solution code:
n=int(input())
x,y,z,ans=0,0,0,0
for i in range(n):
    s=input()
    if s[0]=='B' and s[-1]=='A':
        x+=1
    elif s[0]=='B':
        y+=1
    elif s[-1]=='A':
        z+=1
    for i in range(len(s)-1):
        if s[i]=='A' and s[i+1]=='B':
            ans+=1
x1=x
if y==z:
    x1=x-1
if x==0:
    x1=0
print(min(y,z)+x1+ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
x,y,z,ans=0,0,0,0
for i in range(n):
    s=input()
    if s[0]=='B' and s[-1]=='A':
        x+=1
    elif s[0]=='B':
        y+=1
    elif s[-1]=='A':
        z+=1
    for i in range(len(s)-1):
        if s[i]=='A' and s[i+1]=='B':
            ans+=1
w=1
x1=x-1
if y==z:
    w=0
if x==0:
    x1=0
print(min(y,z)+x1+w+ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
x,y,z,ans=0,0,0,0
for i in range(n):
    s=input()
    if s[0]=='B' and s[-1]=='A':
        x+=1
    elif s[0]=='B':
        y+=1
    elif s[-1]=='A':
        z+=1
    for i in range(len(s)-1):
        if s[i]=='A' and s[i+1]=='B':
            ans+=1
w=1
x1=x-1
if y==z:
    w=0
if x==0:
    x1==0
print(min(y,z)+x1+w+ans)

Submission status: Wrong Answer

Solution code:
#%%
n = int(input())
s = []
a = 0
b = 0
for i in range(n):
    s.append(input())
    if s[i][0] == 'B':
        b += 1
    if s[i][-1] == 'A':
        a += 1

ans = 0
for i in range(n):
    tmp = list(s[i])
    for j in range(len(tmp)-1):
        if tmp[j] == 'A' and tmp[j+1] == 'B':
            ans += 1

ans += min(a, b)
print(ans)

Submission status: Wrong Answer

Solution code:
# div2019C - AB Substrings
def main():
    N, *S = open(0).read().split()
    ans, ab, start_b, end_a = 0, 0, 0, 0
    for s in S:
        if s[0] == "B" and s[-1] == "A":
            ab += 1
        elif s[0] == "B":
            start_b += 1
        elif s[-1] == "A":
            end_a += 1
        ans += s.count("AB")
    if start_b + end_a == 0:
        ans += ab - 1
    else:
        ans += ab + min(start_b, end_a)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def resolve():
    n = int(input())
    s = list(input() for _ in range(n))
    ans = 0
    for si in s:
        ans += si.count("AB")
    ends_with_a = len([si for si in s if not si.startswith("B") and si.endswith("A")])
    starts_with_b = len([si for si in s if si.startswith("B") and not si.endswith("A")])
    ab = len([si for si in s if si.startswith("B") and si.endswith("A")])
    if ab:
        ans += ab + min(ends_with_a, starts_with_b)
    else:
        ans += min(ends_with_a, starts_with_b)
    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
_A = 0
B_ = 0
B_A = 0

for _ in range(N):
    s = input()
    ans += s.count('AB')

    if s[-1] == 'A':
        _A += 1

    if s[0] == 'B':
        B_ += 1

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

ans += min(_A, B_)
if _A == B_A and B_ == B_A:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
_A = 0
B_ = 0
B_A = 0

for _ in range(N):
    s = input()
    ans += s.count('AB')

    if s[-1] == 'A':
        _A += 1

    if s[0] == 'B':
        B_ += 1

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

ans += min(_A, B_)
# if _A == B_ == B_A:
#     ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
_A = 0
B_ = 0
B_A = 0

for _ 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':
        B_A += 1

ans += min(_A, B_)
if _A == B_ == B_A:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
r = 0
nBA = 0
nA = 0
nB = 0
for i in range(N):
  s = input()
  r += s.count("AB")
  if s[0] == "B" and s[-1] == "A":
    nBA += 1
  elif s[0] == "B":
    nB += 1
  elif s[-1] == "A":
    nA += 1
r += min(nA, nB)
if nA > 0 or nB > 0:
  r += nBA
else:
  r += nBA-1
print(r)

Submission status: Wrong Answer

Solution code:
N = int(input())
r = 0
nBA = 0
nA = 0
nB = 0
for i in range(N):
  s = input()
  r += s.count("AB")
  if s[0] == "B" and s[-1] == "A":
    nBA += 1
  elif s[0] == "B":
    nB += 1
  elif s[-1] == "A":
    nA += 1
r += min(nA, nB)
if nA > 0 or nB > 0:
  r += nBA
print(r)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n = int(input())
s = [input() for _ in range(n)]
ans = 0
xa = 0
bx = 0
bxa = 0
for c in s:
    ans += sum(c[i:i + 2] == 'AB' for i in range(len(c) - 1))
    if c[0] == 'B':
        bx += 1
    if c[-1] == 'A':
        xa += 1
    if c[0] == 'B' and c[-1] == 'A':
        bxa += 1
xa -= bxa
bx -= bxa
ans += min(xa, bxa)
xa = max(0, xa - bxa)
ans += min(bxa, bx)
bx = max(0, bx - bxa)
ans += min(xa, bx)
bxa = max(0, bxa - xa)
bxa = max(0, bxa - bx)
ans += max(0, bxa - 1)
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n = int(input())
s = [input() for _ in range(n)]
ans = 0
xa = 0
bx = 0
bxa = 0
for c in s:
    ans += sum(c[i:i + 2] == 'AB' for i in range(len(c) - 1))
    if c[0] == 'B':
        bx += 1
    if c[-1] == 'A':
        xa += 1
    if c[0] == 'B' and c[-1] == 'A':
        bxa += 1
xa -= bxa
bx -= bxa
ans += min(xa, bxa)
xa = max(0, xa - bxa)
ans += min(bxa, bx)
bx = max(0, bx - bxa)
ans += min(xa, bx)
ans += max(0, bxa - 1)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
cnt=0
a=0
b=0
ab=0
for i in range(n):
	s=input()
	for j in range(len(s)-1):
		if s[j]=="A" and s[j+1]=="B":
			cnt+=1
	if s[0]=="B":
		b+=1
	if s[-1]=="A":
		a+=1
	if s[0]=="B" and s[-1]=="A":
		ab+=1
		a-=1
		b-=1
x=min(a,b)
if a!=0 or b!=0:
	print(cnt+x+ab)
else:
	print(cnt+ab-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
cnt=0
a=0
b=0
k=0
for i in range(n):
	s=input()
	for j in range(len(s)-1):
		if s[j]=="A" and s[j+1]=="B":
			cnt+=1
	if s[0]=="B":
		b+=1
	if s[-1]=="A":
		a+=1
	if s[0]=="B" and s[-1]=="A" and k==0:
		a-=1
		b-=1
		k+=1
x=min(a,b)
print(cnt+x)

Submission status: Wrong Answer

Solution code:
n=int(input())
cnt=0
a=0
b=0
for i in range(n):
	s=input()
	for j in range(len(s)-1):
		if s[j]=="A" and s[j+1]=="B":
			cnt+=1
	if s[0]=="B":
		b+=1
	if s[-1]=="A":
		a+=1
x=min(a,b)
if x==n:
	x-=1
print(cnt+x)

Submission status: Wrong Answer

Solution code:
n = int(input())
string_list = [ input() for i in range(n) ]
ans = 0
ab_list = [0, 0, 0]

for i in string_list:
    for j in range(len(i)-1):
        if i[j] == "A" and i[j+1] == "B":
            ans += 1
    if i[0] == "B" and i[-1] == "A":
        ab_list[0] += 1
    elif i[0] == "B":
        ab_list[1] += 1
    elif i[-1] == "A":
        ab_list[2] += 1

if ab_list[0] == 0:
    ans += min(ab_list[1], ab_list[2])
else:
    ans += ab_list[0] - 1
    if ab_list[1] > 0:
        ans += 1
        ab_list[1] -= 1
    elif ab_list[2] > 0:
        ans += 1
        ab_list[2] -= 1
    ans += min(ab_list[1], ab_list[2])
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

count = 0
both = 0
forward = 0
backward = 0
for i in range(N):
    tmp = list(input())
    if tmp[0] == 'B' and tmp[-1] == 'A':
        both += 1
    elif tmp[0] == 'B' and tmp[-1] != 'A':
        forward += 1
    elif tmp[0] != 'B' and tmp[-1] == 'A':
        backward += 1
    for i in range(len(tmp) - 1):
        if tmp[i] == 'A' and tmp[i + 1] == 'B':
            count += 1

count += max(0, both - 1)
if both >= 1:
    forward += 1
    backward += 1
count += min(forward, backward)
print (count)

Submission status: Wrong Answer

Solution code:
n = int(input())
string_list = [ input() for i in range(n) ]


def reshape(s):
    t = 0
    s = list(s)
    for i in range(len(s)-1):
        if s[i] == "A" and s[i+1] == "B":
            s[i], s[i+1] = "X", "X"
            t += 1
    
    return "".join(s), t

string_list_2 = [ reshape(i)[0] for i in string_list if reshape(i)[0] != "" ]
ans = 0
for i in string_list:
    ans += reshape(i)[1]


ab_list = [0, 0, 0]
for i in string_list_2:
    if i[0] == "B":
        ab_list[0] += 1
    if i[-1] == "A":
        ab_list[1] += 1

ans += min(ab_list[0],ab_list[1])

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#diverta2019 C

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7

n = int(input())
_A = []
B_ = []
BA = []
cnt = 0
for _ in range(n):
    s = input()
    for i in range(len(s)-1):
        if s[i] == 'A' and s[i+1] == 'B':
            cnt += 1
    if s[0] != 'B' and s[-1] == 'A':
        _A.append(s)
    elif s[0] == 'B' and s[-1] != 'A':
        B_.append(s)
    elif s[0] == 'B' and s[-1] == 'A':
        BA.append(s)
a = len(_A)
b = len(B_)
ba = len(BA)
ans = cnt
if ba >= 1:
    if a != b:
        ans += (ba-1) + min(a,b) + 1
    else:
        ans += (ba-1) + a
else:
    ans += min(a,b)
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#diverta2019 C

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7

n = int(input())
_A = []
B_ = []
BA = []
cnt = 0
for _ in range(n):
    s = input()
    for i in range(len(s)-1):
        if s[i] == 'A' and s[i+1] == 'B':
            cnt += 1
    if s[0] != 'B' and s[-1] == 'A':
        _A.append(s)
    elif s[0] == 'B' and s[-1] != 'A':
        B_.append(s)
    elif s[0] == 'B' and s[-1] == 'A':
        BA.append(s)
a = len(_A)
b = len(B_)
ba = len(BA)
ans = cnt
if ba >= 1:
    if max(a,b) - min(a,b) > 0:
        ans += (ba-1) + min(a,b) + 1
    else:
        ans += (ba-1) + min(a,b)
else:
    ans += min(a,b)
print(ans)

Submission status: Wrong Answer

Solution code:
class diverta2019C:
    def __init__(self):
        self.N = int(input())
        self.s = [input() for i in range(self.N)]
        self.ans = 0
    
    def solve(self):
        a, b = 0, 0
        for i in range(self.N):
            if(self.s[i].count('AB')):
                self.ans += self.s[i].count('AB')
            if(self.s[i][-1] == 'A'):
                a += 1
            if(self.s[i][0] == 'B'):
                b += 1
        self.ans += min(a, b)
        print(self.ans)

if __name__ == '__main__':
    problem = diverta2019C()
    problem.solve()

Submission status: Wrong Answer

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

ans = 0
BA = 0
BX = 0
XA = 0
for s in S:
    ans += s.count("AB")
    if s[0]=="B" and s[-1]=="A":BA+=1
    elif s[0]=="B":BX+=1
    elif s[-1]=="A":XA+=1
ans += min(BX,XA)
if BA>0: ans += BA-1 + (BX!=XA)
print(ans)