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())
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 max(a, b) - min(a, b) >= both:
    ans = count + min(a, b) + both
else:
    remain = both - max(a, b) + min(a, b)
    if remain % 2 == 0:
        ans = count + max(a, b) + remain // 2
    else:
        ans = count + max(a, b) + remain // 2 + 1

print(ans)

Submission status: Wrong Answer

Solution code:
import re

N = int(input())

startB = 0
stopA = 0
AB = 0

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

print(AB + min(startB, stopA))

Submission status: Wrong Answer

Solution code:
N=int(input())
A_list=0
B_list=0
both=0
count=0
for i in range(0,N):
    txt=input()
    if "AB" in txt:
        count+=1
    
    if(txt[0]=="B" and txt[-1]=="A"):
        both+=1
    elif (txt[0]=="B"):
        B_list+=1
    elif(txt[-1]=="A"):
        A_list+=1
count+=both-1
index=min(A_list,B_list)
count+=index
if(A_list!=B_list):
    count+=1
print(count)

Submission status: Wrong Answer

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

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

print(cnt1 + min(cnt2, cnt3))

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' and si[-1] == 'A':
            front_b_back_a_count += 1
        elif si[0] == 'B':
            front_b_count += 1
        elif si[-1] == 'A':
            back_a_count += 1

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

    if back_a_count >= front_b_count:
        ans += front_b_count
        back_a_count -= front_b_count
        front_b_count = 0
    else:
        ans += back_a_count
        front_b_count -= back_a_count
        back_a_count = 0

    ans += front_b_back_a_count // 2
    front_b_back_a_count %= 2

    if front_b_back_a_count == 1:
        if back_a_count >= 1:
            ans += 1
            back_a_count -= 1
        elif front_b_count >= 1:
            ans += 1
            front_b_count -= 1

    if back_a_count >= 1:
        ans += 1
    elif front_b_count >= 1:
        ans += 1

    print(ans)


if __name__ == '__main__':
    main()

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
            
if countA > countB:
    ans += countB
    countA -= countB
    
    if countA > countBA:
        ans += countBA
        
    else:
        ans += countA
        countBA -= countA
        ans += countBA // 2
        if countBA // 2 >= 1:
            ans += 1
        
else:
    ans += countA
    countB -= countA
    
    if countB > countBA:
        ans += countBA
        
    else:
        ans += countB
        countBA -= countB
        ans += countBA // 2 
        if countBA // 2 >= 1:
            ans += 1
        
print(ans)

Submission status: Wrong Answer

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

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

cnt = 0
both_ab = 0
last_a = 0
initial_b = 0
for i in range(N):
    cnt += s[i].count('AB')

    if s[i][-1] == 'A' and s[i][0] == 'B':
        both_ab += 1
    elif s[i][-1] == 'A':
        last_a += 1
    elif s[i][0] == 'B':
        initial_b += 1

min_a_b = min(initial_b, last_a)
cnt += min_a_b
diff_a_b = 0
if last_a - min_a_b == 0:
    diff_a_b = initial_b - min_a_b
else:
    diff_a_b = last_a - min_a_b
second = min(both_ab, diff_a_b)
cnt += second
if both_ab - second > 0:
    final = both_ab - second
    cnt += final


print(cnt)

Submission status: Wrong Answer

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

x = ','.join(S).count('AB')

A = [ s[-1] == "A" for s in S ].count(True)
B = [ s[0] == "B" for s in S ].count(True)
AandB = [ s[-1] == "A" and s[0] == "B" for s in S ].count(True)
y = min( A, B )
if A == AandB and B == AandB:
  y -= 1

print(x + y)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

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

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

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


print(ab + min(a,b))

Submission status: Wrong Answer

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

n = int(input())

AB = 0
A = 0
B = 0
B_A = 0
for i in range(n):
    s = input()
    if s[0] == 'B' and s[-1] != 'A':
        B = B+1
    if s[0] != 'B' and s[-1] == 'A':
        A = A+1
    if s[0] == 'B' and s[-1] == 'A':
        B_A = B_A+1

    for j in range(len(s)-1):
        if s[j] == 'A' and s[j+1] == 'B':
            AB = AB + 1
ans = 0
if A>0:
    ans = B_A
    if B>0:
        ans = ans + 1
        if A-1>0 and B-1>0:
            ans = ans+min(A-1, B-1)
else:
    ans = B_A-1
    if B>0:
        ans = ans + 1
        if A-1>0 and B-1>0:
            ans = ans+min(A-1, B-1)

ans = ans + AB
print(ans)

Submission status: Wrong Answer

Solution code:
import os, sys, re, math

strs = []
for i in range(int(input())):
    strs.append(input())

cnt = 0
lastA = 0
firstB = 0
for s in strs:
    cnt += s.count('AB')

    if s[-1] == 'A':
        lastA += 1
    if s[0] == 'B':
        firstB += 1

print(cnt+min(firstB, lastA))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

if ba == 1:
    if a < b:
        b -= 1
    else:
        a -= 1
print(n + min(a,b))

Submission status: Wrong Answer

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


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

    # if(min(a,b)+x < max(a,b)):
    #     ans1 = min(max(a,b),min(a,b)+x)
    # else:
    #     ans1 = (a+b+x)//2
    
    # # ans1 = min(min(a,b)+x,)

    # ans1 = max(0,x1-1) + max(0,x2-1) + (min(a,b) if a==b else min(a,b)+1)
    # ans1 = max(0,x1-1) + min(a,b)
    ans1 = max(0,x1-1) + (min(a,b) if a==b else 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())
s = [input() for i in range(N)]

enda = 0
startb = 0
btoa = 0

ans = 0

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

# print(enda, startb, btoa, ans)
if enda < startb:
    enda, startb = startb, enda
ans += startb
enda -= startb
startb = 0

ans += max(0, btoa-1)
if btoa > 0:
    btoa = 1
if enda > 0 and btoa > 0:
    ans += 1
print(ans)

Submission status: Wrong Answer

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

ans = 0
A = B = BA = 0
for s in S:
    for i in range(len(s) - 1):
        if s[i] + s[i + 1] == 'AB':
            ans += 1

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


if (A > 0) & (B > 0) & (BA > 0):
    ans += BA + 1
    A, B = A - 1, B - 1
    ans += min(A, B)

elif (A > 0) & (B > 0):
    ans += min(A, B)

elif A > 0:
    ans += BA

elif B > 0:
    ans += BA

print(ans)

Submission status: Wrong Answer

Solution code:
n,*s=open(0)
a=b=c=d=0
for t in s:
    if t[-2]=='A'and t[0]=='B':
        d+=1
    elif t[-2]=='A':
        a+=1
    elif t[0]=='B':
        b+=1
    c+=t.count('AB')
if all(i==0for i in(a,b)):
    print(c+d)
    exit()
elif a==b:
    print(a+c+d)
    exit()
ans=min(a,b)+c
dif=abs(a-b)
ans+=min(dif,d)
d-=min(dif,d)
print(ans+d)

Submission status: Wrong Answer

Solution code:
N = int(input())
numA = 0
numB = 0
both = 0
ans = 0
for i in range(N):
    cand = list(input())
    if cand[-1] == 'A':
        numA += 1
    if cand[0] == 'B':
        numB += 1
    if cand[-1] == 'A' and cand[0] == 'B':
        both += 1
    ind = 0
    while ind < len(cand):
        try:
            ind = cand.index('A', ind)
            if ind + 1 < len(cand):
                if cand[ind + 1] == 'B':
                    ans += 1
            ind += 1
        except:
            break
print(ans + min(numA, numB) - both)

Submission status: Wrong Answer

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

count=0
ca=0
cb=0
for i in range(N):
    if S[i][0]=="B":
        ca+=1
    if S[i][len(S[i])-1]=="A":
        cb+=1
    for x in range(len(S[i])-1):
        if S[i][x:x+2]=="AB":
            count+=1
print(count+min(ca,cb))

Submission status: Wrong Answer

Solution code:
N = int(input())
l=[]
cnt=a_cnt=b_cnt=ab_cnt=0
for n in range(N):
	s = input()
	if s[0] == 'B':
		b_cnt += 1
	if s[-1] == 'A':
		a_cnt += 1
	if s[0] == 'B' and s[-1] == 'A':
		ab_cnt += 1
	for i in range(len(s)-1):
		if s[i:i+2] == 'AB':
			cnt += 1
	l.append(s)

if ab_cnt == a_cnt and ab_cnt == b_cnt:
	print(ab_cnt-1+cnt)
else:
	m = min(a_cnt,b_cnt)
	print(m+cnt)

Submission status: Wrong Answer

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

top = [0 for _ in range(N)]
last = [0 for _ in range(N)]

res = 0
n = 0
for s in S:
    for i in range(len(s)-1):
        if s[i] == 'A' and s[i+1] == 'B':
            res += 1
            
    if s[0] == 'B':
        top[n] = 1
        
    if s[-1] == 'A':
        last[n] = 1
        
    n += 1
 
d = 0
t = 0
l = 0
for i in range(N):
    if top[i] == 1 and last[i] == 1:
        d += 1
    elif top[i] == 1:
        t += 1
    elif last[i] == 1:
        l += 1
        
res += d//2

res += min(t, l)

if d > 0 and max(t, l) - min(t, l) > 0:
    res += 1
    
    
print(res)

Submission status: Wrong Answer

Solution code:
n = int(input())
cnt = 0
ca = 0
cb = 0

for i in range(n):
    s = input()
    cnt += s.count('AB')
    if s[-1] == 'A':
        ca += 1
    if s[0] == 'B':        
        cb += 1

m = min(ca, cb)
if m == n:
    m -= 1
print(cnt+m)

Submission status: Wrong Answer

Solution code:
N = int(input())
numofAB = 0
numofLastA = 0
numofStartB = 0
numofLastAandStartB = 0
for _ in range(N):
    IN = input()
    numofAB += IN.count('AB')
    if IN[0] == 'B':
        numofStartB += 1
    if IN[-1] == 'A':
        numofLastA += 1
    if IN[0] == 'B' and IN[-1] == 'A':
        numofLastAandStartB += 1

numofLastA -= numofLastAandStartB
numofStartB -= numofLastAandStartB

if numofLastA > numofStartB:
    numofAB += numofStartB + numofLastAandStartB
else:
    numofAB += numofLastA + numofLastAandStartB
if numofLastA == 0 and numofStartB == 0:
    numofAB -= 1

print("{}".format(numofAB))

Submission status: Wrong Answer

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

import numpy as np

N = int(input())
S_list = [input().rstrip() for _ in range(N)]

S_ab_number = [i.count('AB') for i in S_list]
S_b_start_number = [1 if i[0] == 'B' else 0 for i in S_list]
S_a_end_number = [1 if i[-1] == 'A' else 0 for i in S_list]
S_both_number = [1 if i[-1] == 'A' and i[0] == 'B' else 0 for i in S_list]

if sum(S_both_number) == 0:
    print(sum(S_ab_number) +  min(sum(S_b_start_number),sum(S_a_end_number)))
else:
    print(sum(S_ab_number) + sum(S_both_number) - 1 + min(sum(S_b_start_number)-sum(S_both_number),sum(S_a_end_number)-sum(S_both_number)))

Submission status: Wrong Answer

Solution code:
n = int(input())
c = 0
xa = 0
bx = 0
ba = 0
for _ in range(n):
  si = str(input())
  l = len(si)
  s1 = si[0]
  s2 = si[-1]
  if s1=="B":
    if s2=="A":
      ba += 1
    else:
      bx += 1
  elif s2 == "A":
    xa += 1
  for i in range(l-1):
    if si[i]=="A" and si[i+1]=="B":
      c += 1
print(c,ba,bx,xa)
if ba == 0:
  print(c+min(bx,xa))
  exit()
if bx == 0:
  if  xa == 0:
    print(ba-1+c)
    exit()
  else:
    print(ba+c)
    exit()
if xa == 0:
  print(ba+c)
  exit()
print(min(bx,xa)+ba+c)

Submission status: Wrong Answer

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

lastA=0
startB=0
AB = 0
#sをloop

for i in range(N):
  if s[i][0]=='B':
    startB+=1
  if s[i][-1]=='A':
    lastA+=1
  AB += s[i].count('AB')
print(AB+min(startB,lastA))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = 0
b = 0
c = 0
v = 0

for i in range(N):
    S = input()
    for j in range(len(S)-1):
        if S[j:j+2] == 'AB':
            c += 1
    if S[0] == 'B' and S[-1] == 'A':
        v += 1
        continue
    if S[0] == 'B':
        b += 1
    if S[-1] == 'A':
        a += 1

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

Submission status: Wrong Answer

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

count_ab = sum([s.count('AB') for s in S])
count_top_b = len([1 for s in S if s[0] == 'B'])
count_last_a = len([1 for s in S if s[-1] == 'A'])
count_ba = len([1 for s in S if s[-1] == 'A' and s[0] == 'B'])

result = count_ab + min(count_top_b, count_last_a, n - 1)
if count_top_b == count_ba:
    result -= 1
print(result)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [list(input()) for i in range(n)]
a,b,c = 0,0,0
t = 0
for i in range(n):
        for j in range(len(s[i])-1):
                if s[i][j] =="A" and s[i][j+1] == "B":
                        c += 1
                else:
                        continue
        if s[i][0] == "B":
                b += 1
        if s[i][-1] == "A":
                a += 1
        if s[i][0] == "B" and s[i][-1] == "A":
                t += 1
 
if a > b:
        d = b
else:
        d = a

if  t == b and a == t:
        d -= 1

print(d + c)

Submission status: Wrong Answer

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

b,a = sorted([a,b])
ans += b
if ab > 0:
    ans += ab-1
    if a-b>0:
        ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

ss = [0] * N

a = 65
b = 66

count = 0
for i in range(N):
    s = input()
    ss[i] = s
    for k in range(len(s) - 1):
        if ord(s[k]) == a and ord(s[k + 1]) == b:
            count += 1
start_b = 0
end_a = 0
both = 0
for i in range(N):
    s = ss[i]
    if ord(s[0]) == b:
        start_b += 1
    if ord(s[-1]) == a:
        end_a += 1
    if ord(s[0]) == b and ord(s[-1]) == a:
        both += 1

result = count + min(start_b, end_a)
if start_b == end_a and start_b == both:
    result -= 1
print(result)

Submission status: Wrong Answer

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()

n = ni()
_anum = 0
b_num = 0
banum = 0
ct = 0
for i in range(n):
    st = input()
    ct += st.count('AB')
    if st.startswith('B'):
        if st.endswith('A'):
            banum += 1
        else:
            b_num += 1
    else:
        if st.endswith("A"):
            _anum += 1
if banum > 0:
    if _anum > 0 and b_num > 0:
        print(ct + min(_anum - 1, b_num - 1) + (banum + 1))
    else:
        print(ct + banum)
else:
    print(ct + min(_anum, b_num))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = 0
B = 0
ans = 0
case = 0
for i in range(N):
  S = input()
  for j in range(len(S)-1):
    if S[j] == 'A' and S[j+1] == 'B':
      ans += 1
  if S[0] == 'B' and S[len(S)-1] == 'A':
    case += 1
  else:
    if S[0] == 'B':
      B += 1
    if S[len(S)-1] == 'A':
      A += 1
if case == 0:
  print(ans+min(A,B))
else:
  if A > 0 and B > 0:
    print(ans+case+1+min(A-1,B-1))
  else:
    print(ans+case)

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':
        b_flag += 1
    if s[i][-1] == 'A':
        a_flag += 1
    if s[i][0] == 'B' and s[i][-1] == 'A':
        ab_flag += 1

ans = cnt + min(a_flag, b_flag)

if ab_flag%2 == 1 and a_flag==b_flag:
    print(ans - 1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math

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)

dif = len_1 - len_2
if dif > 0:
    hoge = len_2 + len_3
    hoge2 = hoge - len_1
    if hoge2 > 0:
        temp = min(len_1, hoge) + math.floor(hoge2)
    else:
        temp = min(len_1, hoge)
else:
    hoge = len_1 + len_3
    hoge2 = hoge - len_2
    if hoge2 > 0:
        temp = min(len_2, hoge) + math.floor(hoge2)
    else:
        temp = min(len_2, hoge)

ans = count + temp

print(ans)

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' and si[-1] == 'A':
            front_b_back_a_count += 1
        elif si[0] == 'B':
            front_b_count += 1
        elif si[-1] == 'A':
            back_a_count += 1

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

    if back_a_count >= front_b_count:
        ans += front_b_count
        back_a_count -= front_b_count
        front_b_count = 0
    else:
        ans += back_a_count
        front_b_count -= back_a_count
        back_a_count = 0

    ans += front_b_back_a_count // 2
    front_b_back_a_count %= 2

    if front_b_back_a_count == 1:
        if back_a_count >= 1:
            ans += 1
            back_a_count -= 1
        elif front_b_count >= 1:
            ans += 1
            front_b_count -= 1

    if back_a_count >= 1:
        ans += 1
    elif front_b_count >= 1:
        ans += 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

cnt = 0
judge = 0
for i in range(n):
    for j in range(len(s[i])):
        if j != len(s[i]) - 1:
            if s[i][j] == "A":
                judge = 1
        if judge == 1 and s[i][j] != "A":
            if s[i][j] == "B":
                cnt += 1
                judge = 0
            else:
                judge = 0
        if j == len(s[i]) - 1:
            judge = 0
cnt_a = 0
cnt_b = 0
cnt_ab = 0

for i in range(n):
    len_s = len(s[i])
    if s[i][len_s - 1] == "A":
        cnt_a += 1
    if s[i][0] == "B":
        cnt_b += 1
    if s[i][0] == "B" and s[i][len_s - 1] == "A":
        cnt_ab += 1

if cnt_a == cnt_ab and cnt_a > 0:
    print(cnt + min(cnt_a, cnt_b) - 1)
else:
    print(cnt + min(cnt_a, cnt_b))

Submission status: Wrong Answer

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

ans = 0
num_a = 0
num_b = 0
num_ba = 0
num_ab = 0
for i in s:
    if(i[-1]=='A' and i[0]=='B'):
        num_ba += 1
    elif(i[-1]=='A'):
        num_a+=1
    elif(i[0]=='B'):
        num_b+=1
    last = ''
    for k in i:
        if(k=='A'):
            last = 'A'
        elif(last == 'A' and k=='B'):
            num_ab += 1
            last = ''
        else:
            last = ''

ans += num_ab

num_conn = min(num_a,num_b)
if(num_a!=num_b and num_ba!=0):
    print(num_ab+num_ba+num_conn)
elif(num_ba==0):
    print(num_ab+num_conn)
else:
    print(num_ab+num_ba+num_conn-1)

Submission status: Wrong Answer

Solution code:
import re

N = int(input())

cnt = 0
tail_A = 0
head_B = 0
both = 0

for i in range(N):
    s = input()
    if s[0] == 'B':
        head_B += 1
    if s[-1] == 'A':
        tail_A += 1
    if s[0] == 'B' and s[-1] == 'A':
        both += 1
    matchedList = re.findall(r"AB", s)
    cnt += len(matchedList)

if tail_A == both and head_B == both:
    cnt += min(tail_A, head_B) - 1
else:
    cnt += min(tail_A, head_B)


print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
tmp = ''
a=0
b=0
n = int(input())
arr = []
for i in range(n):
    tmp = ''
    tmp = input()
    arr.append(tmp)
l = 0
sum = 0
for i in range(n):
    tmp = arr[int(i)]
    l = len(tmp)
    if tmp[0] == 'B':
        b = b+1
    if tmp[l-1] == 'A':
        a = a+1
    
    tmpchar = ''
    for char in tmp:
        if char == 'B' and tmpchar == 'A':
            sum = sum +1
        tmpchar = char

if a==n:
    a =a-1
if a>b:
    print(sum+b)
else: 
    print(sum + a)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = []
for i in range(N):
    S.append(input())
cnt = 0
cntA = 0
cntB = 0
cntAB = 0
for s in S:
    cnt += s.count('AB')
for s in S:
    if s[0] == 'B':
        cntB += 1
    if s[-1] == 'A':
        cntA += 1
    if s[0]=='B' and s[-1] =='A':
        cntAB += 1
if cntA == cntB == cntAB:
    print(cnt + cntAB - 1)
else:
    print(cnt + min(cntA, cntB))

Submission status: Wrong Answer

Solution code:

# coding: utf-8

# In[5]:

import sys
input = sys.stdin.readline
N = int(input())
S = [input() for i in range(N)]


# In[12]:

b_cnt = 0
a_cnt = 0
ab_cnt = 0
for i in range(N):
    if S[i][0]=='B'and S[i][-1]=='A':
        ab_cnt += 1
    elif S[i][0]=='B':
        b_cnt += 1
    elif S[i][-1]=='A':
        a_cnt += 1
#print(b_cnt, a_cnt)
#print(ab_cnt, a_cnt, b_cnt, '/'.join(S).count('AB') )

#if abs(a_cnt-b_cnt) >= 1 and ab_cnt > 0:

if max(a_cnt, b_cnt) == 0 and ab_cnt > 0:
    ab_cnt -= 1
#elif abs(a_cnt-b_cnt)==0 and ab_cnt > 0:
    
#if ab_cnt < 0:
#    ab_cnt = 0
#ab_cnt = ab_cnt-1 if ab_cnt!=0 else ab_cnt
print('/'.join(S).count('AB') + min(a_cnt, b_cnt, N-1) + ab_cnt)


# In[ ]:

Submission status: Wrong Answer

Solution code:
n,*s=open(0)
a=b=c=d=0
for t in s:
    if t[-2]=='A'and t[0]=='B':
        d+=1
    elif t[-2]=='A':
        a+=1
    elif t[0]=='B':
        b+=1
    c+=t.count('AB')
if all(i==0for i in(a,b)):
    print(c+d-1)
    exit()
elif a==b:
    print(a+c+d)
    exit()
ans=min(a,b)+c
dif=abs(a-b)
ans+=min(dif,d)
d-=min(dif,d)
print(ans+d)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

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

print(count_ab + min(count_a, count_b))

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/diverta2019/tasks/diverta2019_c

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

flag = False
count = 0
for i in range(len(s)):
    for j in range(len(s[i])):
        if flag == True and s[i][j] == 'B':
            count += 1
            flag = False
        elif s[i][j] == 'A':
            flag = True
        else:
            flag = False
    flag = False

startcnt = [0] * N
endcnt = [0] * N
for i in range(len(s)):
    if s[i][0] == 'B':
        startcnt[i] = 1
    if s[i][len(s[i])-1] == 'A':
        endcnt[i] = 1

cntpls = [0,0,0] #始め、終わり、両方
for i in range(N):
    if startcnt[i] + endcnt[i] == 1:
        if startcnt[i] == 1:
            cntpls[0]+= 1
        else:
            cntpls[1] += 1
    elif startcnt[i] + endcnt[i] == 2:
        cntpls[2] += 1

if cntpls[1] >= 1:
    count += 1
    cntpls[1] -= 1

if cntpls[0] >= 1:
    count += 1
    cntpls[0] -= 1

print(count+cntpls[2] - 1+min(cntpls[0],cntpls[1]))

Submission status: Wrong Answer

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

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

print(ab + min(a,b))

Submission status: Wrong Answer

Solution code:
n=int(input())
li=[]
t=0
for i in range (0,n):
  m=input()
  t=t+m.count("AB")
  li.append(list(m))
las=0
sta=0
q=0
for i in range (0,n):
  p=0
  le=len(li[i])-1
  if li[i][0]=="B":
    sta=sta+1
    p=p+1
  if li[i][le]=="A":
    las=las+1
    p=p+1
  if p==2:
    q=q+1
if sta==las==q:
  print(t+n-1)
elif las==q:
  print(t+sta)
elif sta==q:
  print(t+las)
elif sta<las:
  print(t+sta)
elif sta>las:
  print(t+las)
else:
  print(t+sta)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
list = []
cnt = 0
cnta = 0
cntb = 0
for i in range(n):
  list.append(input())
  if("AB" in list[i]):
    cnt += 1
  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:
import re

N = int(input())
string_list = [input() for i in range(N)]
count = 0
A_count = 0
B_count = 0
AB_count = 0
for i in string_list:
    # if 'AB' in i:
    #     count += 1
    # print(len(re.findall('AB', i)))
    count += len(re.findall('AB', i))
    if i[-1] == 'A':
        A_count += 1
    if i[0] == 'B':
        B_count += 1
    # if i[-1] == 'A' and i[0] == 'B':
    #     A_count -= 1
# A_sentou_test = A_count * B_count
count += min(A_count, B_count)
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 max(a, b) - min(a, b) >= both:
    ans = count + min(a, b) + both
else:
    remain = both - max(a, b) - min(a, b)
    if remain % 2 == 0:
        ans = count + max(a, b) + remain // 2
    else:
        ans = count + max(a, b) + remain // 2 + 1

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
    
def calc(s):
    lastA = 0
    firstB = 0
    both = 0
    interAB = 0
    for _s in s:
        if _s[0] == 'B' and _s[-1] == 'A':
            both += 1
        else:
            if _s[0] == 'B':
                firstB += 1
            if _s[-1] == 'A':
                lastA += 1
        
        if 'AB' in _s:
            interAB += 1

    if lastA == 0 and firstB == 0:
        both = max(both-1,0)
    
    return min(lastA + both, firstB + both) + interAB

def main():
    n = stdin.readline().rstrip()
    s = [x.rstrip() for x in stdin.readlines()]
    ans = calc(s)
    print(ans)
    
if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
#N=1 は未対応
counter = 0
S = []
for s in range(N):
  s = input()
  S.append(s)

S0 = S[0]

a0 = S0.count('A')
b0 = S0.count('B')

counter += min(a0,b0)
a_counter = 0
if a0 > b0:
  a_counter +=1
  
for i in range(N-1):
  ST = S[i+1]
  a = ST.count('A')
  b = ST.count('B')
  if (a_counter == 1) and (b > 1):
    counter +=1
    b = b -1
  counter += min(a,b)
  if a > b:
    a_couter = 1
  else:
    a_counter = 0
  
print(counter)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=0
b=0
c=0
d=0

for i in range(N):
    S=str(input())
    s=len(S)
    for i in range(s-1):
        if S[i]=="A" and S[i+1]=="B":
            d+=1
    if S[0]=="B" and S[s-1]=="A":
        a+=1
    elif S[0]=="B":
        b+=1
    elif S[s-1]=="A":
        c+=1

if b==0 or c==0:
    if a!=0:
        print(a+d)
    else:
        print(d)
elif b==c :
    print(a+b+d)
else:

    b=max(b,c)
    c=min(b,c)

    e=b-c

    if e>=a:
        print(a+c+d)
    else:
        print(a-1+c+d)