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())
topb = 0
taila = 0
both = 0
cnt = 0
for _ in range(N):
    s = input()
    cnt +=s.count("AB")

    if s[0] == "B" and s[-1] == "A":
        both += 1
    elif s[0] == "B":
        topb += 1
    elif s[-1] == "A":
        taila += 1

con_cnt_min,con_cnt_max = min(topb,taila),max(topb,taila)
if both>0:
    cnt += both -1
    if con_cnt_max - con_cnt_min >0:
        cnt += 1

print(cnt + con_cnt_min)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = [0]*N
num_ab_in = 0
num_hip_a = 0
num_head_b =0 
num_both = 0

for i in range(N):
    s[i] = input()
    slist= list(s[i])
    num_ab_in += s[i].count('AB')
    if slist[0] =='B':
        num_head_b +=1
    if slist[-1] =='A':
        num_hip_a +=1
    if slist[0] =='B' and slist[-1] =='A':
        num_both +=1

if num_both == num_head_b and num_both == num_hip_a:
    ans = num_ab_in + num_both-1
else:
    ans = num_ab_in + min(num_head_b,num_hip_a)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
ans = 0
a = 0
b = 0
ab = 0
for s in S:
    if s[0] == "B" and s[-1] == "A":
        ab += 1
    elif s[0] == "B":
        b += 1
    elif s[-1] == "A":
        a += 1
    for i in range(len(s) - 1):
        if s[i:i+2] == "AB":
            ans += 1
m = min(a, b)
ans += m
a -= m
b -= m
if ab == 1:
    if a > 0 or b >  0:
        ans += 1
elif ab >= 2:
    m = max(a, b)
    if m >= ab:
        ans += ab
    else:
        ans += ab
        #ab -= m
        #ans += ab
    """ans += max(ab-1, 0)
    if a > 0 and b > 0:
        ans += 2
        a -= 1
        b -= 1
    else:
        if a > 0:
            ans += 1
            a = 0
        else:
            ans += 1
            b = 0"""
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

g1 = []
g2 = []
g3 = []

cnt = 0

for i in range(n):
    s = input()
    cnt += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        g3.append(s)
    elif s[0] == "B":
        g1.append(s)
    elif s[-1] == "A":
        g2.append(s)
        
a = len(g1)
b = len(g2)
c = len(g3)

if a == b:
    if a == c:
        cnt += c * 2
    elif a > c:
        cnt += c * 2
        cnt += a - c
    elif a < c:
        cnt += a * 2
        cnt += c - a - 1
elif a > b:
    cnt += b
    cnt += c
elif b > a:
    cnt += a
    cnt += c

print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[]
for i in range(n):
  s.append(input())
cnt_a=0
cnt_b=0
cnt_ab=0
for j in range(n):
  if s[j][0]=='B' and s[j][len(s[j])-1]=='A':
    cnt_ab+=1
  elif s[j][0]=='B':
    cnt_b+=1
  elif s[j][len(s[j])-1]=='A':
    cnt_a+=1
cnt_a+=cnt_ab
cnt_b+=cnt_ab
cnt_2=0
for l in range(n):
  cnt_2+=s[l].count('AB')
if cnt_ab-cnt_a==0 and cnt_ab-cnt_b==0:
  print((cnt_ab+cnt_2)-1)
else:
  print(min(cnt_a,cnt_b)+cnt_2)

Submission status: Wrong Answer

Solution code:
N = int(input())

strings = []
for _ in range(N):
    strings.append(input())

cnt = 0
cnt_A = cnt_B = 0
flag = False
for s in strings:
    for c1, c2 in zip(s, s[1:]):
        if c1 == 'A' and c2 == 'B':
            cnt += 1

    if s[0] == 'B':
        cnt_B += 1
        if s[-1] != 'A':
            flag = True
    if s[-1] == 'A':
        cnt_A += 1
        if s[0] != 'B':
            flag = True

cnt += min(cnt_A, cnt_B)
print(cnt)

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/diverta2019/tasks/diverta2019_a
# -*- coding: utf-8 -*-

n = int(input())
s = [input() for i in range(n)]

cnt = 0
cntA = 0
cntB = 0
cntAB = 0

for i in s:
        if "AB" in i:
                cnt += 1
        if i[len(i)-1] == "A" and i[0] != "B":
                cntA += 1
        if i[len(i)-1] != "A" and i[0] == "B":
                cntB += 1
        if i[len(i)-1] == "A" and i[0] == "B":
                cntAB += 1

print(
        cnt + min(cntA,cntB) + 
        min( ( max(cntA,cntB) - min(cntA,cntB) ), cntAB)
        + cntAB // 2


)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)

def f(a,b,ba):
  if a>0 and b>0:
    return 1 + f(a-1,b-1,ba)
  elif a>0 and ba>0:
    return 1 + f(a-1,b+1,ba-1)
  elif ba>0 and b>0:
    return 1 + f(a+1,b-1,ba-1)

  return 0

N=int(input())
a=[]
b=[]

stA=0
stB=0
stBA=0

ans=0
for x in range(N):
  z=input()
  buf=z.count("AB")
  b.append((z[0],z[-1],buf))
  ans+=buf

for z in b:
  if z[0]=="B" and z[1]=="A":
    stBA+=1
  elif z[0]=="B":
    stB+=1
  elif z[1]=="A":
    stA+=1



ans=ans+f(stA,stB,stBA)
print(ans)

Submission status: Wrong Answer

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


total = 0
a = 0
b = 0
ba = 0

for _s in s:
    total += _s.count('AB')
    if _s[-1] == 'A':
        a += 1
    if _s[0] == 'B':
        b += 1
    if _s[-1] == 'A' and _s[0] == 'B':
        ba += 1


if a < b:
    ba -= (b - a)
    b = a
else:
    ba -= (a - b)
    a = b

if ba % 2 == 0:
    diff = ba // 2
    a -= diff
    b -= diff
else:
    diff = ba // 2
    a -= diff
    b -= diff
    if a < b:
        b -= 1
    else:
        a -= 1

total += max(a, b)
print(total)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import collections as co
N = int(input())
numA = 0
numB = 0
both = 0
ans = 0
for i in range(N):
    cand = co.deque(input())
    ind = 0
    delL = []
    while ind < len(cand):
        try:
            ind = cand.index('A', ind)
            if ind + 1 < len(cand):
                if cand[ind + 1] == 'B':
                    ans += 1
                    delL.append(ind)
            ind += 1
        except:
            break
    delL.sort(reverse=True)
    for n in delL:
        del cand[n]
        del cand[n]
    if cand[-1] == 'A':
        numA += 1
    if cand[0] == 'B':
        numB += 1
    if cand[-1] == 'A' and cand[0] == 'B':
        both += 1
#print(ans, numA, numB, both)
print(ans + min(numA, numB))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar  1 19:29:34 2019

@author: nakamuratakayuki
"""
N = int(input())
S = []
for i in range(N):
    S.append(input())

m = 0
f = 0
b = 0
t = 0
for s in S:
    if s.startswith('B'):
        m += 1
    if s.endswith('A'):
        f += 1
    if s.startswith('B')*s.endswith('A'):
        m -= 1
        f -= 1
        b += 1
    t += s.count('AB')
print(min(f,m)+b+t)

Submission status: Wrong Answer

Solution code:
# N < 10000
N = int(input())

# len(s) < 10

count = 0
a, b, ba = 0, 0, 0
for i in range(N):
    s = input().rstrip()
    count += s.count("AB")

    if s[-1] == 'A':
        if s[0] == 'B':
            ba += 1
        else:
            a += 1
    elif s[0] == 'B':
        b += 1

#e = int(input())
#print("*A=",a, ", B*=",b, "B*A=",ba, ",AB=", count, ",expected=", e)

if ba > 1:
    count += ba - 1
    ba = 1

c0 = 0
if ba == 1:
    c0 = 1 + min(a, b)
count+=max(min(a+ba,b),min (a,b+ba), c0)
print(count)

Submission status: Wrong Answer

Solution code:

n = int(input())
s = []

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

a = 0
b = 0
ab = 0
ba = 0
for i in range(n):
    if (s[i][-1].count("A") and s[i][0].count("B")):
        ba += 1
    else:
        a += s[i][-1].count("A")
        b += s[i][0].count("B")
    ab += s[i].count("AB")

if b == a:
    print(ab + a + max(ba - 1, 0))

elif b > a:
    print(ab + a + ba)

else:
    print(ab + b + ba)

Submission status: Wrong Answer

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

hd = 0
tl = 0
bt = 0
ans = 0
for s in S:
    for i in range(len(S)-1):
        if s[i:i+2] == 'AB':
            ans += 1
    if s[0] == 'B' and s[-1] == 'A':
        bt += 1
    elif s[0] == 'B':
        hd += 1
    elif s[-1] == 'A':
        tl += 1

if bt == 0:
    ans += min(hd, tl)
else:
    ans += bt - 1
    if hd > 0 and tl > 0:
        ans += 2 + min(hd-1, tl-1)
    else:
        ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
import math
n = int(input())

s=[]
res = 0
tmp = 0
eA=0
sB=0
eAsB=0
for i in range(n):
#	s.append(list(input()))
	s.append(input())
	tmp = s[i].count("AB")
	if tmp != 0:
		res += tmp
	if s[i][0]=="B" and s[i][-1]=="A":
		eAsB += 1
	elif s[i][-1]=="A":
		eA += 1
	elif s[i][0]=="B":
		sB += 1

if eAsB != 0:
	if eA < sB:
		res += eA+1
	else:
		res += sB+1
else:
	if eA < sB:
		res += eA
	else:
		res += sB

if eAsB > 1:	
	res += eAsB-1

print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = 0
B = 0
ans = 0

S = list()

for _ in range(N):
    S.append(input())

for s in S:
    ans += s.count("AB")
    if s[0] == "B":
        B += 1
    if s[-1] == "A":
        A += 1

if A == N:
    A -= 1
if B == N:
    B -= 1

if A > B:
    ans += B
else:
    ans += A

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = []
result = 0
firstB = 0
lastA = 0
BA = 0
for i in range(N):
    S.append(input())
    result += S[i].count("AB")
    if S[i][0] == "B" or S[i][len(S[i])-1] == "A":
        S[i] = S[i][0:len(S[i]):len(S[i])-1]
        if S[i] == "BA":
            firstB += 1
            lastA += 1
        elif S[i][0] == "B":
            firstB += 1
        else:
            lastA += 1
if lastA == firstB:
    result += lastA
elif  lastA < firstB:#B*があまる
    result += lastA 
elif lastA > firstB:#*Aがあまる
    result += firstB
print(result)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
cnt = 0
bx = 0
xa = 0
ba = 0

for i in range(n):
  si = input()
  flg = False
  for c in si:
    if c == 'A':
      flg = True
    elif c == 'B' and flg:
      cnt += 1
      flg = False
        
    else:
      flg = False
  
  if si[0] == 'B':
    bx += 1
  if si[-1] == 'A':
    xa += 1
  if si[0] == 'B' and si[-1] == 'A':
    ba += 1
    
bxa = min(bx, xa)
if bxa == n or bxa == ba:
  bxa -= 1
print(cnt + bxa)

Submission status: Wrong Answer

Solution code:
# AB Substrings
import itertools
def fusion(N, s):
    ans = 0
    B_ = 0
    _A = 0
    B_A = 0
    for st in s:
        ans += st.count("AB")
        B_ += st.startswith("B")
        _A += st.endswith("A")
        B_A += st.startswith("B") and st.endswith("A")
    ans += min(B_, _A)
    if B_ == B_A and _A == B_A:
        ans -= 1

    return ans
N =int(input())
s = [""] * N
for i in range(N):
    s[i] = str(input())

ans = fusion(N,s)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a=input()
a=int(a)
ll=[]
count=0
pcount=0
ll=[]
ap=0
bp=0
abc=0
for i in range(a):
    app=0
    bpp=0
    switch=0
    b=input()
    ofset=0
    for i in b:
        if i=="B" and ofset==0:
        
            bp+=1
            bpp=1
        elif i=="A":
            switch+=1
            ofset+=1
            continue
        elif i=="B" and switch==1:
            count+=1
            switch=0
            
        else:
            switch=0
        ofset+=1
    if switch==1:
        ap+=1
        app=1
    if app>=1 and bpp>=1:
        abc+=1
        ap-=1
        bp-=1
if ap>bp:
    count+=bp
    bp=0
    ap-=bp
else:
    count+=ap
    ap=0
    bp-=ap
if abc>0:
    count+=abc-1
    if ap>0 or bp>0:
        count+=1
print(count)

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
  s = int(input())
  Str = [0 for a in range(s)]
  for a in range(s):
    Str[a] = input()
  count = 0
  LastA = 0
  LastB = 0
  for a in range(s):
    count += Str[a].count("AB")
    if Str[a][len(Str[a])-1] == "A":
      LastA += 1
    elif Str[a][0] == "B":
      LastB += 1
  if LastA == s:
    LastA = LastA - 1
  if LastB == s:
    LastB = LastB - 1
    
  if LastA > LastB:
    print(count + LastB)
  else:
    print(count + LastA)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [str(0)]*n
i = 0
while(i < n):
    inp = str(input())
    if(('A' in inp) or ('B' in inp) == True):
        s[i] = inp
        
    i += 1

count = 0
for i in range(len(s)):
    word = s[i]
    for j in range(0,len(word)-1):
        if(word[j] == 'A' and word[j+1] == 'B'):
            count += 1


            
notab = 0
lasta = 0
firstb = 0

for i in range(len(s)):
    word = s[i]
    if(word[len(word)-1] == 'A'):
        lasta += 1
        
    if(word[0] == 'B'):
        firstb += 1

add = min(lasta,firstb)

print(count + add)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
inab = 0
topb = 0
taila = 0
topb_taila = 0
for _ in range(n):
    s = str(input())
    if (s[0] == "B")&(s[-1] == "A"):
        topb_taila += 1
    else:
        if (s[0] == "B"):
            topb += 1
        if (s[-1] == "A"):
            taila += 1
    
    bef =""
    for i in range(len(s)):
        if (bef=="A")&(s[i]=="B"):
            inab += 1
        bef = s[i]

ans = inab

topb += topb_taila
taila += topb_taila

if topb==taila:
    if topb==topb_taila:
        ans += topb-1
    else:
        ans += topb
else:
    ans += min(topb,taila)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = []
ans = 0
for i in range(N):
    si = input()
    s.append(si)

    ans += si.count("AB")




d = {}
d["A"] = 0
d["B"] = 0
d["all"] = 0

for i in range(N):
    if s[i][0] == "B" and s[i][-1] == "A":
        d["all"] += 1
    elif s[i][0] == "B":
        d["B"] += 1
    elif s[i][-1] == "A":
        d["A"] += 1

ans += (d["all"] - 1)
d["A"] += 1
d["B"] += 1
ans += min(d["A"],d["B"])
print(ans)

Submission status: Wrong Answer

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

contained = sum([s.count('AB') for s in ss])
B_hoge = sum([1 for s in ss if s[0]=='B'])
hoge_A = sum([1 for s in ss if s[-1]=='A'])
B_hoge_A = sum([1 for s in ss if s[0]=='B' and s[-1]=='A'])

B_hoge -= B_hoge_A
hoge_A -= B_hoge_A

ans = contained
while B_hoge_A>0:
    if B_hoge == hoge_A:
        B_hoge_A -= 1
        hoge_A += 1
        contained += 1
    else:
        B_hoge_A -= 1
        contained += 1
print(contained + min(B_hoge, hoge_A))

Submission status: Wrong Answer

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

count = 0
count_BA = 0
count_A = 0
count_B = 0
for i in range(N):
    count += s[i].count('AB')
    if s[i][0] == 'B' and s[i][-1] == 'A':
        count_BA += 1
    elif s[i][0] == 'B':
        count_B += 1
    elif s[i][-1] == 'A':
        count_A += 1

if count_B != 0 and count_A != 0:
    print(count + count_BA - 1 + 1 + 1 + min(count_A, count_B) - 1)
elif count_B != 0 or count_A != 0:
    print(count + count_BA - 1 + 1)
else:
    print(count + count_BA - 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
s = []
max_AB = 0
max_A = 0
max_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':
            max_AB += 1
    if s[len(s)-1] == 'A':
        max_A += 1
    if s[0] == 'B':
        max_B += 1
if max_A > max_B:
    max_AB += max_B
else: max_AB += max_A
print(max_AB)

Submission status: Wrong Answer

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

count = 0
a_count = 0
b_count = 0
for i in range(len(s_list)):
    count += s_list[i].count("AB")
    if s_list[i][-1] == "A":
        a_count += 1
    if s_list[i][0] == "B":
        b_count += 1

if a_count == len(s_list) and b_count == len(s_list) :
    print(count + a_count - 1)
elif a_count > b_count :
    print(count + b_count)
else :
    print(count + a_count)

Submission status: Wrong Answer

Solution code:
N = int(input())
count = 0
ca = 0
cb = 0
cab = 0
for i in range(N):
    si = input()
    for j in range(len(si)-1):
        if si[j] == "A" and si[j+1] == "B":
            count += 1
    if si[-1] == "A":
        ca += 1
    if si[0] == "B":
        cb += 1
    if si[-1] == "A" and si[0] == "B":
        cab += 1
        ca -= 1
        cb -= 1
    
#print(count)
if ca == 0 and cb == 0:
    count += cab - 1
elif (ca == 0 and cb != 0) or (ca != 0 and cb == 0):
    count += cab
else:
    count += cab + min(ca, cb)

#print(ca, cb, cab)

print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
ls = []
subst = 0
suba = 0
subb = 0
cnt = 0
for i in range(N):
    temp = input()
    subst += temp.count('AB')
    if temp[0] == 'B':
        suba += 1
    if temp[-1] == 'A':
        subb += 1
    if temp[0] == 'B' and  temp[-1] == 'A':
        cnt += 1
        subb -= 1
        suba -= 1

print(subst+min(subb,suba)+cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())

a = 0
b = 0
c = 0
co = 0
for i in range(n):
	l = input()
	co += l.count("AB")
	if l[0] == "B":
		b += 1
	if l[-1] == "A":
		a += 1
	if l[-1] == "A" and l[0] == "B":
		c += 1

k = min(a,b,c)

if c == k:
	co += 2 * c
	co += min(a-2*k,b-2*k)
elif a == k:
	co += a
	kk = min(c-a,b-a)
	co += kk * 2
	if kk == b:
		co += (c - k - kk) * 2
	else:
		co += a
else:
	co += b
	kk = min(c-b,a-b)
	co += kk * 2
	if kk == a:
		co += (c - k - kk) * 2
	else:
		co += a


print(co)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
mae_B = 0
ushiro_A = 0
docchimo = 0
naka_AB = 0
for s in S:
    for i in range(len(s)-1):
        if s[i] == 'A' and s[i+1] == 'B':
            naka_AB += 1
    if s[0] == 'B' and s[-1] == 'A':
        docchimo += 1
    elif s[0] == 'B':
        mae_B += 1
    elif s[-1] == 'A':
        ushiro_A += 1
ans = naka_AB
ans += max(0, docchimo-1)
if docchimo > 0:
    mae_B += 1
    ushiro_A += 1
ans += min(mae_B, ushiro_A)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
AtCoder C
"""

# N = int(input())
# A = list(map(int,input().split())) # N row 1 column
# A = int(input()) for _ in range(N) # 1 row N column

# S = str(input()) # str(input()) == input() -> 'abc'
# S = list(input()) # abc -> ['a','b','c']

def main():
    # A, B = list(map(int,input().split()))
    N = int(input())
    S = [input() for _ in range(N)]# 1 row N column
    tmp=0
    a=0
    b=0
    flag=False
    for s in S:
        tmp+=s.count('AB')
        if s[0]=="B":
            a+=1
            if s[-1]!="A":
                flag=True
        if s[-1]=="A":
            b+=1
            if s[0]!="B":
                flag=True

    if flag==False:
        a-=1
    print(tmp+min(a,b,N-1))
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N=INT()
S=['']*N
for i in range(N):
    S[i]=input()

enda=0
startb=0
both=0
cntab=0
for i in range(N):
    if S[i].startswith('B') and S[i].endswith('A'):
        both+=1
    elif S[i].startswith('B'):
        startb+=1
    elif S[i].endswith('A'):
        enda+=1
    cntab+=S[i].count('AB')

mnab=min(enda, startb)
cntab+=mnab
if both!=0:
    enda-=mnab
    startb-=mnab
    mxab=max(enda, startb)
    if mxab==0:
        cntab+=both-1
    else:
        if mxab<=both:
            cntab+=mxab
            both-=mxab
            if both<=mxab:
                cntab+=both
            else:
                cntab+=mxab
                both-=mxab
                cntab+=both-1
        else:
            cntab+=both
print(cntab)

Submission status: Wrong Answer

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

ab_count = 0
last_a_and_first_b_count = 0
last_a_count = 0
first_b_count = 0
for s in sn:
    ab_count += s.count('AB')
    if s[-1] == 'A' and s[0] == 'B':
        last_a_and_first_b_count += 1
    else:
        if s[-1] == 'A':
            last_a_count += 1
        if s[0] == 'B':
            first_b_count += 1

if last_a_count == 0 and first_b_count == 0:
    if last_a_and_first_b_count < 2:
        v = 0
    else:
        v = last_a_and_first_b_count - 1
elif (last_a_count > 0 and first_b_count == 0) or (last_a_count == 0 and first_b_count > 0):
    v = last_a_and_first_b_count
else:
    if last_a_and_first_b_count == 0:
        v = min(last_a_count, first_b_count)
    elif abs(last_a_count - first_b_count) > last_a_and_first_b_count:
        v = min(last_a_count, first_b_count) + last_a_and_first_b_count
    else:
        v = (last_a_count + first_b_count + last_a_and_first_b_count) // 2

print(ab_count + v)

Submission status: Wrong Answer

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

a = 0
b = 0
b_a = 0
ab = 0
for j in range(N):
    if s[j][0] == 'B':
        b += 1
    if s[j][-1] == 'A':
        a += 1
    
    if s[j][0] == 'B' and s[j][-1] == 'A':
        b_a += 1

    ab += s[j].count('AB')

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

Submission status: Wrong Answer

Solution code:
import re
N=int(input())
listA=[input() for i in range(N)]
firstB=0
endA=0
feAB=0
AB=0
for i in listA:
    AB+=len(re.findall("AB",i))
    
    if i[0]=="B":
        firstB+=1
    if i[-1]=="A":
        endA+=1
        if i[0]=="B":
            feAB+=1

AB+=min([endA,firstB])
if feAB>0:
    AB-=1
print(AB)

Submission status: Wrong Answer

Solution code:
from sys import stdin

def main():
    N = int(input())
    S = stdin.read().splitlines()

    s1 = {'la':0, 'lb':0, 'ba':0}
    ans = 0
    for s in S:
        ans += s.count('AB')
        if s[0] == 'B' and s[-1] == 'A':
            s1['ba'] += 1
        elif s[0] == 'B':
            s1['lb'] += 1
        elif s[-1] == 'A':
            s1['la'] += 1
    min_, max_ = min(s1['la'], s1['lb']), max(s1['la'], s1['lb'])
    if max_ > 0:
        s1['ba'] -= max_ - min_
        ans += max_ + s1['ba']
    else:
        ans += s1['ba'] // 2
    print(ans)

input = lambda: stdin.readline().rstrip()
main()

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":
        p += 1
    if s[i][-1]=="A":
        q += 1
    if s[i][0]== "B" and s[i][-1]=="A":
        r +=1

if p == q :
    print(x-1+min(p,q))
else:
    print(x+min(p,q))

Submission status: Wrong Answer

Solution code:
n = int(input())

cnt = 0
a_flag = 0
b_flag = 0
ab_flag = 0

s = [0]*n
for i in range(n):
    s[i] = input()

    for j in range(len(s[i])-1):
        if s[i][j:j+2] == 'AB':
            cnt += 1

    if s[i][0] == 'B' and s[i][-1] != 'A':
        b_flag += 1
    if s[i][-1] == 'A' and s[i][0] != 'B':
        a_flag += 1
    if s[i][0] == 'B' and s[i][-1] == 'A':
        ab_flag += 1

if ab_flag != 0:
    print(cnt + ab_flag + 1 + min(a_flag-1, b_flag-1))
else:
    print(cnt + min(a_flag, b_flag))

Submission status: Wrong Answer

Solution code:
n = int(input())
ab = a_end = b_top = b_a = 0
for i in range(n):
    s = input()
    if 'AB' in s:
        ab += 1
    if s[0] == 'B' and s[len(s)-1] == 'A':
        b_a += 1
    elif s[0] == 'B':
        b_top += 1
    elif s[len(s)-1] == 'A':
        a_end += 1
if b_a > 0:
    ab += b_a-1
    b_top += 1
    a_end += 1
ans = ab+min(a_end,b_top)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

def main():
    input = sys.stdin.readline
    N = int(input())
    ans = 0
    q = deque()
    for _ in range(N):
        s = str(input().strip())
        for i in range(len(s)):
            if s[i:i+2] == 'AB':
                ans += 1

        if not (s[0] == 'B' or s[-1] == 'A'):
            continue

        s = s[0] + s[-1]

        q_ = deque()
        while len(q) > 0:
            s1 = q.popleft()

            if s[-1] == 'A' and s1[0] == 'B':
                ans += 1
                s = s[0] + s1[-1]
            elif s[0] == 'B' and s1[-1] == 'A':
                ans += 1
                s = s1[0] + s[-1]
            else:
                q_.append(s1)

        if s[0] == 'B' or s[-1] == 'A':
            while len(q_) > 0:
                s1 = q_.popleft()

                if s[-1] == 'A' and s1[0] == 'B':
                    ans += 1
                    s = s[0] + s1[-1]
                elif s[0] == 'B' and s1[-1] == 'A':
                    ans += 1
                    s = s1[0] + s[-1]
                else:
                    q.append(s1)

            if s[0] == 'B' or s[-1] == 'A':
                q.append(s)

        else:
            q = q_

    return ans


if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
import math
N = int(input())
counter = 0
master_parts = 0
a_parts = 0
b_parts = 0
for i in range(N):
    temp = input()
    counter += temp.count('AB')
    if temp[-1] == 'A' and temp[0] == 'B':
        master_parts += 1
    elif temp[-1] == 'A':
        a_parts += 1
    elif temp[0] == 'B':
        b_parts += 1
if a_parts < b_parts:
    counter += (master_parts + a_parts)/2 + 1
elif b_parts < a_parts:
    counter += (master_parts + b_parts)/2 + 1
else:
    counter += (master_parts + a_parts)/2
print(math.ceil(counter))

Submission status: Wrong Answer

Solution code:
import collections

N = int(input())

count = 0
count_first = 0
count_last = 0
count_A_B = 0

for i in range(N):
    str = input()
    count += str.count("AB")
    if "B" == str[0]:
        count_last+=1
    if "A" == str[-1]:
        count_first+=1
    if "A" == str[-1] and "B" == str[0]:
        count_A_B+=1
if count_A_B != max(count_first,count_last):
    print(count+min(count_first, count_last))
else:
    print(count+min(count_first, count_last)-1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

N = int(input())

count_AB = 0
count_last_A = 0
count_first_B = 0
count_both = 0
for i in range(N):
    s = input().rstrip()
    
    count_AB += len(s.split('AB')) - 1
    
    if s[0] == 'B' and s[-1] == 'A':
        count_both += 1
    elif s[0] == 'B':
        count_first_B += 1
    elif s[-1] == 'A' :
        count_last_A += 1
        
temp = 0
if count_both > 0:
    temp += count_both - 1
    count_both = 1
    
    if count_last_A > 0 and count_last_A >= count_first_B:
        temp += 1
        count_both = 0
        count_first_B += 1
        count_last_A -= 1
    elif count_first_B > 0 and count_first_B >= count_last_A:
        temp += 1
        count_both = 0
        count_last_A += 1
        count_first_B -= 1
        
if count_both == 0:
    temp += min(count_last_A, count_first_B)
    
print(count_AB + temp)

Submission status: Wrong Answer

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

hoge = min(ba, a, b)
ans += 2*hoge

a -= hoge
b -= hoge
ba -= hoge

if ba > 0:
    ans += min(ba, a)
    ba -= min(ba, a)
    ans += min(ba, b)
    ba -= min(ba, b)
if a == 0 and b != 0:
    ans += min(ba, b)
    ba -= min(ba, b)
    b -= min(ba, b)
elif b == 0 and a != 0:
    ans += min(ba, a)
    ba -= min(ba, a)
    a -= min(ba, a)

ans += min(a, b)

print(ans)

Submission status: Wrong Answer

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