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

count_ab = 0
count_b = 0
count_a = 0
count_a_b = 0
for i in range(n):
    a = input()
    count_ab += a.count('AB')

    if a[0]=='B' and a[len(a)-1]!='A':
        count_b += 1
    if a[len(a)-1] == 'A' and a[0]!='B':
        count_a += 1
    if a[len(a)-1] == 'A' and a[0]=='B':
        count_a_b += 1

if (count_a > 0 or count_b > 0) and count_a_b > 0:
    count_a += count_a_b
    count_b += count_a_b
    print(count_ab + min(count_a, count_b))
else:
    print(count_ab + min(count_a, count_b) + count_a_b - 1)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for i in range(N)]
ans = 0
BFirst = 0
ALast = 0
for i in range(len(S)):
    if not S[i].startswith("AB") or S[i].endswith("AB"):
        if S[i].startswith("B") or S[i].endswith("A"):
            if S[i].startswith("B"): BFirst+=1
            if S[i].endswith("A"): ALast += 1
    if "AB" in S[i]:
        tmpIndex = S[i].find("AB")
        while tmpIndex != -1:
            ans += 1
            tmpIndex = S[i].find("AB",tmpIndex+1)

print(ans + min(BFirst, ALast))

Submission status: Wrong Answer

Solution code:
n=int(input())
listn=[]
for i in range(n):
    listn.append(input())
counter=0
fcount=0
lcount=0
scount=0
F=[]
L=[]
for i in range(n):
    counter+=listn[i].count('AB')
    if listn[i][0]=='B':
        F.append(1)
        fcount+=1
    else:
        F.append(0)
    if listn[i][-1]=='A':
        L.append(1)
        lcount+=1
    else:
        L.append(0)
if fcount<lcount:
    counter+=fcount
elif lcount<fcount:
    counter+=lcount
else:
    for i in range(n):
        if F[i]==L[i] and F[i]==1:
            scount+=1
            lcount-=1
    counter+=lcount+scount-1
print(counter)

Submission status: Wrong Answer

Solution code:
# AB Substrings
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
    if ans < 0:
        ans = 0
    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())
count=countA=countB=countAB=0
for i in range(n):
    s=input()
    if "AB" in s:
        count+=1
    if s[-1]=="A":countA+=1
    if s[0]=="B":countB+=1
    if s[-1]=="A" and s[0]=="B":countAB=0
print(count+min(countA,countB,countAB))

Submission status: Wrong Answer

Solution code:
N = int(input())
s = [input() for i in range(N)]
count = 0
l = []
a = 0
b = 0
ab = 0
for i in s:
  count+=i.count("AB")
for i in s:
  if list(i)[0] == "B" and list(i)[-1] != "A":
    l.append(i)
    b += 1
  elif list(i)[-1] == "A" and list(i)[0] != "B":
    l.append(i)
    a += 1
  elif list(i)[-1] == "A" and list(i)[0] == "B":
    l.append(i)
    ab += 1
if a > b + ab:
  count += b + ab
elif b > a + ab:
  count += a + ab
elif ab > a + b:
  count += ab-1 + a + b
else:
  count += ab-1 + max(a, b)
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
v = []

for i in range(N):
    s = input()
    v.append(s)
    
ab = 0
sbea = 0
sb = 0
ea = 0

for i in range(N):
    s = v[i]
    
    for j in range(len(s)-1):
        if s[j] == 'A' and s[j+1] == 'B':
            ab += 1
            
    if s[0] == 'B' and s[len(s) - 1] == 'A':
        sbea += 1
    elif s[0] == 'B':
        sb += 1
    elif s[len(s)-1] == 'A':
        ea += 1
        
ans = ab
ans += min(sb, ea)
if sb == ea:
    ans += max(sbea-1, 0)
else:
    ans += sbea
    
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
ca = 0
cb = 0
cc = 0
for i in range(n):
    t = input()
    s.append(t)
    if t[-1] == "A":
        ca += 1
    if t[0] == "B":
        cb += 1
    if t[-1] == "A" and t[0] == "B":
        cc += 1
cnt = 0
for i in range(n):
    cnt += s[i].count("AB")
if cc >= min(ca, cb) >= n:
    cnt -= 1
print(cnt + min(ca, cb))

Submission status: Wrong Answer

Solution code:
def input_from_console():
    n = int(input())
    s_list = []
    for i in range(n):
        s_list.append(input())
    return n, s_list


def count_ab_in_string(s_list):
    return sum([item.count('AB') for item in s_list])


def count_start_with_b(s_list):
    return sum([1 if item.startswith('B') and not item.endswith('A') else 0 for item in s_list])


def count_end_with_a(s_list):
    return sum([1 if not item.startswith('B') and item.endswith('A') else 0 for item in s_list])


def count_end_with_a_and_start_with_b(s_list):
    return sum([1 if item.endswith('A') and item.startswith('B') else 0 for item in s_list])


def main():
    n, s_list = input_from_console()
    contains_count = count_ab_in_string(s_list)
    b = count_start_with_b(s_list)
    a = count_end_with_a(s_list)
    connect_count = min(b, a)  # ..a b...

    should_paired = count_end_with_a_and_start_with_b(s_list)
    must, residual = 0, 0
    if should_paired >= 2:  # b..a b..a
        must = should_paired - 1
        residual = should_paired - must
    elif should_paired > 0:
        residual = 1

    # print(contains_count, a, b, must, residual)
    if residual > 0:
        if a != b:
            print(sum([contains_count, connect_count, must, 1]))
        else:
            print(sum([contains_count, connect_count, must]))
    else:
        print(sum([contains_count, connect_count, must]))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())

count = 0
acount = 0
bcount = 0
for _ in range(N):
    st = input()
    count += st.count('AB')
    if st.startswith('B'):
        bcount += 1
    if st.endswith('A'):
        acount += 1

c = min(acount, bcount, N - 1)
print(count + c)

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 == 0 or b_count == 0:
    print(count)
elif 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:
import re

N = int(input())

startB = 0
stopA = 0
BtoA = 0
AB = 0

for i in range(N):
    s = input()
    if s.startswith("B") and s.endswith("A"):
        BtoA += 1
    elif s.startswith("B"):
        startB += 1
    elif s.endswith("A"):
        stopA += 1
    
    AB += len(re.findall("AB", s))



ans = AB + min(startB, stopA) + BtoA
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())

p = 0
count = 0
a = 0
b = 0

while(p < n):
    inp = str(input())
    if(('A' in inp) or ('B' in inp) == True):
        for i in range(0,len(inp)-1):
            if(inp[i] == "A" and inp[i + 1] == 'B'):
                count += 1
    
        if(inp[0] == 'B'):
            b += 1
        if(inp[len(inp) - 1] =='A'):
            a += 1
            
    p += 1
    
print(count + min(a,b))

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
if a==b and a!=ab and ab>1:
    k = -1
print(ans+min(a,b)+k)

Submission status: Wrong Answer

Solution code:
headA = 0
headB = 0
tailA = 0
tailB = 0
c = 0
for i in range(int(input())):
    t = input()
    if t[:2] == 'AB':
        c+=1
        temp = 'ZZ'+t[2:]
        if 'AB' in temp[-2:]:
            c += temp.count('AB')
        elif temp[-1] == 'A':
            tailA +=1
        c += temp.count('AB')
    elif 'AB' in t[-2:]:
        c +=1
        t = t[0:-2]+'ZZ'
        if t[0] == 'B':
            headB+=1
        c+= t.count('AB')
    else:
        if t[0]== 'B':
            headB +=1
        if t[-1] == 'A':
            tailA +=1
        c+=t.count('AB')
print(c+min(headB,tailA))

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    s = []
    for i in range(N):
        s.append(input())
    count = 0
    ab = 0
    a = 0
    b = 0
    for i in s:
        count += i.count('AB')
        if i[0] == 'B' and i[-1] == 'A':
            ab += 1
        elif i[-1] == 'A':
            a += 1
        elif i[0] == 'B':
            b += 1
    if ab > max(a,b):
        print(count + min(a, b) + min(max(a, b), ab) + ab - max(a, b))
    else:
        print(count + min(a, b) + min(max(a, b), ab))
main()

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()
    first_b = 0
    last_a = 0
    for _ in range(N):
        s = str(input().strip())
        for i in range(len(s)):
            if s[i:i+2] == 'AB':
                ans += 1

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

    while first_b * last_a != 0 and len(q) > 1:
        q_ = deque()
        s = q.popleft()
        while len(q) > 0:
            s1 = q.popleft()

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

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

    return ans


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

Submission status: Wrong Answer

Solution code:
N=int(input())
ls=[input() for i in range(N)]
org_num=0
XA_num=0
BA_num=0
BX_num=0

for i in ls:
    org_num+=i.count('AB')
    listed_word=list(i)
    if listed_word[0]=='B' and listed_word[-1]=='A':
        BA_num+=1
    elif listed_word[0]=='B':
        BX_num+=1
    elif listed_word[-1]=='A':
        XA_num+=1
#print("組み合わせ前のAB個数は",org_num,'個')
#print("X~Aは",XA_num,"個.B~Aは",BA_num,"個.B~Xは",BX_num,"個")

if BX_num:
    BX_num_addition=1
else:
    BX_num_addition=0

if XA_num:
    answer=org_num+BA_num+BX_num_addition
elif BA_num:
    answer=org_num+BA_num+BX_num_addition-1
else:
    answer=0

XA_num-=1
BX_num-=1

shiage=min(XA_num,BX_num)
if shiage<0:
    shiage=0

answer+=shiage
print(answer)

Submission status: Wrong Answer

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


def main():
    n = int(input())
    s = [input() for _ in range(n)]
    ans = 0
    back_a_count = 0
    front_b_count = 0
    front_b_back_a_count = 0

    for si in s:
        if si[0] == 'B':
            front_b_count += 1
        if si[-1] == 'A':
            back_a_count += 1
        if si[0] == 'B' and si[-1] == 'A':
            front_b_back_a_count += 1

        for j in range(len(si) - 1):
            if (si[j] == 'A' and si[j + 1] == 'B'):
                ans += 1

    ans += min(back_a_count, front_b_count)

    if front_b_count == back_a_count == front_b_back_a_count:
        ans -= 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def ab_substrings(N: int, S: list)->int:
    res = 0
    endA = 0
    startB = 0
    both = 0

    for s in S:
        res += s.count('AB')
        if s.endswith('A'):
            if s.startswith('B'):
                both += 1
            else:
                endA += 1
        else:
            startB += 1

    if both > 0:
        res += both - 1

        if endA > 0:
            res += 1
            endA -= 1

        if startB > 0:
            res += 1
            startB -= 1

    return res + min(endA, startB)


if __name__ == "__main__":
    N = int(input())
    S = [input() for _ in range(N)]

    ans = ab_substrings(N, S)
    print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

count = 0
count_a = 0
count_b = 0
count_ab = 0
for i in range(n):
    s = input()
    if 'AB' in s[0:-1]:
        count += 1

    if s[-1] == 'A' and s[0] == 'B':
        count_ab += 1
    elif s[-1] == 'A':
        count_a += 1
    elif s[0] == 'B':
        count_b += 1


#print('(count, A, B)=({}, {}, {})'.format(count, count_a, count_b))

count += count_ab - 1

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

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

count += min(count_a, count_b)

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())

count_ab = 0
count_b = 0
count_a = 0
count_a_b = 0
for i in range(n):
    a = input()
    count_ab += a.count('AB')

    if a[0]=='B' and a[len(a)-1]!='A':
        count_b += 1
    if a[len(a)-1] == 'A' and a[0]!='B':
        count_a += 1
    if a[len(a)-1] == 'A' and a[0]=='B':
        count_a_b += 1

if count_a > 0 or count_b > 0:
    count_a += count_a_b
    count_b += count_a_b
    print(count_ab + min(count_a, count_b))
else:
    print(count_ab + min(count_a, count_b) + count_a_b - 1)

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)
print(bx, ba, xa)
if bxa == n or max(bx, xa) == ba:
  bxa -= 1
print(cnt + bxa)

Submission status: Wrong Answer

Solution code:
a=input()
a=int(a)
count=0
ap=0
bp=0
abc=0
for j 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
        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:
n = int(input())
 
ans = 0
S = []
right_A = 0
left_B = 0
AB = 0
for _ in range(n):
    s = input()
    S.append(s)
    ans += s.count('AB')
    
    if s[-1] == 'A':
        right_A += 1 
    if s[0] == 'B':
        left_B += 1
    if s[-1] == 'A' and s[0] == 'B':
        AB += 1
    
        
ans += min(right_A, left_B)

if AB == right_A and AB == left_B:
    ans -= 1
    
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())

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

ABcount = [s for s in new_lst if 'AB' in s]

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

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

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

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
        cnt += a
    elif a < c:
        cnt += a * 2
        cnt += c
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())
B_=0
_A=0
AB = 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

tmp = min(B_,_A)
if B_==_A and B_A != 0:
     tmp = B_A-1

print(AB + tmp)

Submission status: Wrong Answer

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


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

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

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

    print(ans1+ans2)
    
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    s = [ next(tokens) for _ in range(N) ]  # type: "List[str]"
    solve(N, s)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
string_list=[input() for i in range(n)]
new_list = {}
target_chr = "AB"
count = 0
left_b_count = 0
right_a_count = 0
none_count = 0
for string in string_list:
    if string.count(target_chr) > 0 and (string[0] != "B" or string[-1] != "A"):
        count += string.count(target_chr)
        none_count += 1
        continue
    if string[0] == "B":
        left_b_count +=1
        count += string.count(target_chr)
        none_count += 1
        continue
    if string[-1] == "A":
        right_a_count += 1
        count += string.count(target_chr)
        none_count += 1
        continue

if left_b_count > right_a_count:
    if none_count == n:
        print(count + right_a_count)
    else:
        print(count + right_a_count + 1)
else:
    print(count + left_b_count)

Submission status: Wrong Answer

Solution code:
import sys

N = int(sys.stdin.readline())

s = []

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

count = 0

for i in range(N):
    count += s[i].count('AB')

pattern_1 = []
pattern_2 = []
pattern_3 = []
for i in range(N):
    if s[i][:1] == 'B' and s[i][-1:] == 'A':
        pattern_3.append(i)
    elif s[i][:1] == 'B':
        pattern_2.append(i)
    elif s[i][-1:] == 'A':
        pattern_1.append(i)

len_1 = len(pattern_1)
len_2 = len(pattern_2)
len_3 = len(pattern_3)

count += min(len_1, len_2)


if len_1 > len_2:
    count += len_3
elif len_2 > len_1:
    count += len_3
elif len_3 > 0:
    count += len_3 - 1

print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = 0
B = 0
AB=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 s[0]=="B" and s[-1]=="A":
        AB+=1

if A == N:
    A -= 1
if B == N:
    B -= 1
if AB>=N-1:
	if A>B:
		A=A-2+(N-AB)
	else:
		B=B-2+(N-AB)

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

print(ans)

Submission status: Wrong Answer

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

print("test")

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
S = []
for i in range(n):
    s = str(input())
    S.append(s)
    
ans = 0
countA = 0
countB = 0
countBA = 0
for s in S:
    if s[0] == 'B' and s[len(s)-1] == 'A':
        countBA += 1
        
    elif s[len(s)-1] == 'A':
        countA += 1
        
    elif s[0] == 'B':
        countB += 1
        
    for i in range(len(s) - 1):
        if s[i] == 'A' and s[i+1] == 'B':
            ans += 1
# print(countA, countB, countBA)
# print(ans)

if countA > countB:
    ans += countB
    countA -= countB
    
    if countA >= countBA:
        ans += countBA + 1
        
    else:
        ans += countA
        countBA -= countA
        ans += countBA // 2
        if countA != 0:
            ans += 1
        
else:
    ans += countA
    countB -= countA
    
    if countB >= countBA:
        ans += countBA + 1
        
    else:
        ans += countB
        countBA -= countB
        ans += countBA // 2
        if countB != 0:
            ans += 1
        
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
lef_only_ex = False
rig_only_ex = False
lef_only_ex_hold = False
rig_only_ex_hold = False
back_a = 0
forward_b = 0
inside = 0
for i in range(N):
    line = input()
    if line[-1] == 'A':
        back_a += 1
        rig_only_ex = True
    if line[0] == 'B':
        forward_b += 1
        lef_only_ex = True
    if line[0] == 'B' and line[-1] == 'A':
        lef_only_ex = False
        rig_only_ex = False
    if lef_only_ex and not lef_only_ex_hold:
        lef_only_ex_hold = True
    if rig_only_ex and not rig_only_ex_hold:
        rig_only_ex_hold = True

    for s in range(len(line) - 1):
        if line[s] == 'A' and line[s + 1] == 'B':
            inside += 1
print((min(back_a, forward_b) - 1 if not lef_only_ex_hold and not rig_only_ex_hold and back_a == forward_b else min(
    back_a, forward_b)) + inside)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
inside_str = 0
tail_a = 0
head_b = 0
except_Dcount = 0

for i in range(N):
  str = input()
  ab_inside_str = str.count('AB')

  inside_str = inside_str + ab_inside_str
  if (str[-1] == 'A'):
    tail_a = tail_a + 1
  if (str[0] == 'B'):
    head_b = head_b + 1
  if(str[-1] == 'A' and str[0] == 'B'):
    except_Dcount = except_Dcount + 1

if(tail_a == head_b and tail_a > 0):
	total_cand = inside_str + min(tail_a, head_b) - 1
else:
  	total_cand = inside_str + min(tail_a, head_b)

print(total_cand)

Submission status: Wrong Answer

Solution code:
n = int(input())

cnt = 0
a_cnt = 0
b_cnt = 0
ab_cnt = 0
for i in range(n):
  s = input()
  for l in range(len(s)-1):
    if s[l] == 'A' and s[l+1] == 'B':
      cnt = cnt +1
  if s[-1] == 'A':
    a_cnt = a_cnt +1
  if s[0] == 'B':
    b_cnt = b_cnt +1
  if s[-1] == 'A' and s[0] == 'B':
    ab_cnt = ab_cnt +1

#tmp = min(a_cnt, b_cnt) - ab_cnt
tmp = min(a_cnt, b_cnt)
print(cnt + tmp)

Submission status: Wrong Answer

Solution code:
n = int(input())

num___A = 0
num_B__ = 0
num_B_A = 0 
cnt = 0

for i in range(n):
  word = input()
  cnt += word.count("AB")
  if word[-1] =="A" and word[0]=="B":
    num_B_A += 1
  elif word[-1] == "A":
    num___A += 1
  elif word[0] == "B":
    num_B__ += 1

if num_B_A != 0:
  cnt += num_B_A - 1
  cnt += min(num___A, num_B__ )
  if num___A != num_B__:
    cnt += 1
else:
  cnt += min(num___A, num_B__ )

print(cnt)

Submission status: Wrong Answer

Solution code:
count = 0
a = 0
b = 0
N = int(input())
for x in range(N):
  s = input()
  if s[0] == 'B':
    a += 1
  if s[-1] == 'A':
    b += 1
  target = 'AB'
  index = -1
  while True:
    index = s.find(target, index + 1)
    if index == -1:
        break
    count += 1
if a > b :
  count += b
else:
  count += a
print(count)

Submission status: Wrong Answer

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

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

for i in S:
  c = i.count('AB')
  cnt += c

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

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

cnt += lA_fB + r + n

#print("fB, lA, BA", fB, lA, BA)
#print("lA_fB , r , n",lA_fB , r , n)
print(cnt)

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

count+=max(min(a+ba,b)+ba,min(a,b+ba)+ba)
print(count)

Submission status: Wrong Answer

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

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

ans = cnt
ans += min(cntA,cntB)
amari = max(cntA,cntB) - min(cntA,cntB)
ans += min( amari, cntAB)
if cntAB > amari:
        ans+= cntAB - amari

print(ans)

Submission status: Wrong Answer

Solution code:
a=input()
a=int(a)
count=0
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
        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:
N = int(input())
s = [0] * N

count = 0
a = 0
b = 0
both = 0

for i in range(N):
    s[i] = input()
    count += s[i].count("AB")
    if s[i][0] == "B" and s[i][-1] == "A":
        both += 1
    if s[i][0] == "B" and s[i][-1] != "A":
        b += 1
    if s[i][0] != "B" and s[i][-1] == "A":
        a += 1

if a >= both and b >= both:
    ans = count + both * 2 + min(a - both, b - both)
elif a < both and b >= both:
    ans = count + a * 2 + min(b, both)
elif a >= both and b < both:
    ans = count + b * 2 + min(a, both)
else:
    ans = count + min(a, b) * 2 + min(max(a, b) - min(a, b), both) + both - min(max(a, b) - min(a, b), both)

print(ans)

Submission status: Wrong Answer

Solution code:
# import random
#
# dist = ["#","."]
#
# f = open("in_sample.txt","w")
#
# f.write("1000 1000\n")
#
# for i in range(1000):
#     temp =[]
#     for j in range(1000):
#         temp.append(random.choice(dist))
#     f.writelines(temp)
#     f.write("\n")


n = int(input())
count = 0
enda = 0
startb = 0
both = 0
for i in range(n):
    s = input()
    flag = False
    for i in range(len(s)):
        if flag and s[i] == "B":
            count += 1
            flag = False
        else:
            flag = False
        if s[i] == "A":
            flag = True
    if s[0] == "B" and s[-1] == "A":
        both += 1
    if s[0] == "B":
        startb += 1
    if s[-1] == "A":
        enda += 1


startb = startb-both
enda = enda-both

# print(count)
# print(both)
# print(enda)
# print(startb)

mi = min(startb,enda)
ma = max(startb,enda)

ans = count + mi
# print(ans)
left = ma-mi
# print(left)
if both > left:
    ans += both
elif both == left:
    ans += both
else:
    ans += both

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
S = [input() for i in range(n)]
cnt = 0
a = 0
b = 0
ba = 0
for i in range(n):
    cnt += S[i].count("AB")
    if S[i].startswith("B") and S[i].endswith("A"):
        ba += 1
    elif S[i].startswith("B"):
        a += 1
    elif S[i].endswith("A"):
        b += 1
cnt += ba - 1
if a >= 1:
    cnt += 1
if b >= 1:
    cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in range(n)]
counter = 0
startWithB = 0
endWithA = 0
both = 0


for i in range(n):
    counter += s[i].count("AB")
    if s[i][0] == "B":
        if s[i][-1] == "A":
            both += 1
        else:
            startWithB += 1
    elif s[i][-1] == "A":
        endWithA += 1

if abs(startWithB - endWithA) > both:
    print(min(startWithB + both, endWithA + both)+counter)
else:
    print((startWithB + both + endWithA)//2+counter)