Problem:


Score : 400 points

Problem Statement

Snuke has N strings. The i-th string is s_i.

Let us concatenate these strings into one string after arranging them in some order.
Find the maximum possible number of occurrences of AB in the resulting string.

Constraints

1 \leq N \leq 10^{4}

2 \leq |s_i| \leq 10

s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

For example, if we concatenate ABCA, BAD and XBAZ in this order, the resulting string ABCABADXBAZ has two occurrences of AB.

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4



Sample input:
3
ABCA
XBAZ
BAD


Sample output:
2

Submission status: Wrong Answer

Solution code:
import sys
from collections import 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':
            q_.append(s)
        q = q_

    return ans


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

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()
    i = 0
    while True:
        i = s.find("AB", i)
        if i < 0:
            break
        i += 2
        count+=1

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

#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),min(a,b+ba))
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
num_tail_A = 0
num_head_B = 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(num_tail_A != num_head_B):
  result = count + min(num_tail_A, num_head_B)
else:
  result =  count + num_tail_A - 1
print(result)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = ''
d= 0
e = {'A' : 0, 'B' : 0, 'A_double': 0}
for i in range(n):
    S = input().strip()
    d += S.count('AB')
    if (S[0] == 'A' or S[-1] == 'A'): 
        e['A'] += 1
    if len(S) == 1 and S[0] == 'A':
        e['A'] -= 1
    if S[0] == 'B' or S[-1] == 'B':
        e['B']+=1
    if len(S) == 1 and S[0] == 'B':
        e['B'] -= 1

d+= min(e.values())
print(d)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[]
ctr=0
ctr_a=0
ctr_b=0
ctr_ab=0
for i in range(n):
    s.append(str(input()))
    if "AB" in s[i]:
        ctr+=s[i].count("AB")
    if s[i][-1]=="A":
        ctr_a+=1
    if s[i][0]=="B":
        ctr_b+=1
    if s[i][-1]=="A" and s[i][0]=="B":
        ctr_ab+=1
if ctr_a==ctr_b==ctr_ab:
    print(max(ctr+ctr_ab-1,0))
else:
    print(ctr+min(ctr_a,ctr_b))

Submission status: Wrong Answer

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

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

    for s in S:
        if s.endswith('A'):
            if not s.startswith('B') or startB > 1:
                res += 1
                endA -= 1
                startB -= 1

    return res


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())
n,a,b,c = 0,0,0,0
for i in range(N):
  s = input()
  n += s.count('AB')
  if s[-2:-1] == 'A' and s[0:1] == 'B':
    a += 1
    b += 1
    c += 1
  elif s[0:1] == 'B':
    b += 1
  elif s[-2:-1] == 'A':
    a += 1
if a == b and a == c:
  print(n + a - 1)
else:
  print(n + max(a,b))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
	N = int(input())
	S = [""]*N
	ans = 0
	a_end = 0
	b_begin = 0
	a_end_b_begin = 0
	for i in range(N):
		S[i] = input()
		if "AB" in S[i]:
			ans += S[i].count("AB")
		if S[i][0] == "B":
			if S[i][-1] == "A":
				a_end_b_begin += 1
				continue
			b_begin += 1
		if S[i][-1] == "A":
			a_end += 1
	ans += a_end_b_begin-1
	if b_begin > 0:
		ans += 1
		b_begin -= 1
	if a_end > 0:
		ans += 1
		a_end -= 1
	ans += min(b_begin, a_end)

	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
        
    if 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:
n=int(input())
s=[]
for i in range(n):
  s.append(input())
cnt_a=0
cnt_b=0
for j in range(n):
  if s[j][0]=='B' and s[j][len(s[j])-1]=='A':
    cnt_a+=1
    cnt_b+=1
  elif s[j][0]=='B':
    cnt_b+=1
  elif s[j][len(s[j])-1]=='A':
    cnt_a+=1
cnt_2=0
for l in range(n):
  cnt_2+=s[l].count('AB')
if cnt_a==n and cnt_b==n:
  print(n+cnt_2-1)
else:
  print(min(cnt_a,cnt_b)+cnt_2)

Submission status: Wrong Answer

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

a = 0
both = 0
b = 0
ans = 0
for i, s in enumerate(s_list):
    ans += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        both += 1

    elif s[-1] == "A":
        a += 1

    elif s[0] == "B":
        b += 1


if a != 0:
    a -= 1
    ans += both
    if b != 0:
        b -= 1
        ans += 1
    ans += min(a, b)
else:
    if b != 0:
        b -= 1
        ans += both
    else:
        ans += both - 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = []
temp_sum = 0
A_or_B_count = [0,0]
AB_count = 0
for i in range(N):
	aaa = input()
	temp_sum += aaa.count("AB")
	if aaa[0] == "B" and aaa[-1] == "A":
		AB_count += 1
	elif aaa[-1] == "A":
		A_or_B_count[0] += 1
	elif aaa[0] == "B":
		A_or_B_count[1] += 1
A_or_B_count.sort()
if A_or_B_count[0] + AB_count > A_or_B_count[1]:
	AB_count -= (A_or_B_count[1] - A_or_B_count[0])
	print(temp_sum + min(N-1,AB_count + A_or_B_count[1]))
else:
	print(temp_sum + min(N - 1,A_or_B_count[0] + AB_count))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = 0
e = 0
se = 0
ans = 0
for i in range(n):
    stri = input()
    ans += stri.count('AB')
    if stri[0] == 'B':
        s += 1
    if stri[len(stri)-1] == 'A':
        e += 1
    if stri[0] == 'B' and stri[len(stri)-1] == 'A':
        se += 1
se = se % 2
for i in range(se):
    if s > e:
        s -= 1
    else:
        e -= 1
ans += min(s,e)
print(ans)

Submission status: Wrong Answer

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

cnt = 0
enda = 0
startb = 0
ba = 0
for s in strs:
    cnt += s.count("AB")
    enda += 1 if (s[-1] == "A") else 0
    startb += 1 if (s[0] == "B") else 0
    ba += 1 if (s[0] == "B" and s[-1] == "A") else 0

cnt -= 1 if (enda == ba and startb == ba) else 0

print(max(cnt + min(enda, startb), 0))

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":
        b += 1
    if s[l] == "A":
        a += 1
    ret += s.count("AB")
if a == b:
    a -= 1
ab = min(a,b)
print(ret + ab)

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'])

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

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for u in range(n)]
a = sum([u.count("AB") for u in s])
b = len([1 for u in s if u.endswith("A")])
c = len([1 for u in s if u.startswith("B")])
d = len([1 for u in s if u.endswith("A") and u.startswith("B")])
if d==1:
  print(a)
elif (b==c and c==d):
  print(a+b-1)
else:
  print(a+min(b,c))

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


c = min(d["A"],d["B"],d["all"])
ans += (c*2)
d["A"] -= c
d["B"] -= c
d["all"] -= c

if d["all"] == 0:
    ans += min(d["A"],d["B"])
elif d["A"] == 0 and d["B"] == 0:
    ans += d["all"] // 2
elif d["A"] == 0:
    ans += 2*min(d["all"]//2,d["B"])
elif d["B"] == 0:
    ans += 2*min(d["all"]//2,d["A"])



print(ans)

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 and b ==n:
    a =a-1
if a>b:
    print(sum+b)
else: 
    print(sum + a)

Submission status: Wrong Answer

Solution code:
import re

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

counter = 0
B_begin=0
A_end =0
AB_in =0
B_begin_and_A_end = 0


for i in range(N):
    m = len(re.findall('AB',s[i]))
    AB_in += m

    chech_b = False
    chech_a = False

    if s[i][0]=='B':
        B_begin +=1
        chech_b = True

    if s[i][-1]=='A':
        A_end+=1
        chech_a = True

    if chech_a and chech_b:
        B_begin_and_A_end+=1

ans = 0 
mini = min(B_begin, A_end)

if mini ==0:
    ans = AB_in

elif B_begin == A_end == B_begin_and_A_end:
    if m!=0:
        ans = AB_in + mini -1

else:
    ans = AB_in + mini

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from fractions import gcd
from functools import reduce
from itertools import combinations
from math import factorial

import numpy as np

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


def solve():
    N = ii()
    # S = imi(N, str)
    first_B = 0
    last_A = 0
    num_AB = 0
    # 重複要素
    dupl = 0
    for _ in range(N):
        s = ii(str)
        num_AB += s.count("AB")
        if s[0] == "B" and s[-1] == "A":
            dupl += 1
            continue
        if s[0] == "B":
            first_B += 1
        if s[-1] == "A":
            last_A += 1
    comb_AB = min(first_B, last_A)
    ans = num_AB + comb_AB
    if dupl != 0:
        ans += factorial(dupl)
    return ans


if __name__ == "__main__":
    print(solve())

Submission status: Wrong Answer

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

N = int(input())
S = [input() for _ in range(N)]
l, m, n = 0, 0, 0
ans = 0
for s in S:
    if s[0] == 'B' and s[-2] == 'A':
        n += 1
    elif s[0] == 'B':
        m += 1
    elif s[-2] == 'A':
        l += 1
    for i in range(len(s)-1):
        if s[i:i+2] == 'AB':
            ans += 1

if l == 1 and m == 1:
    ans += n + 1
else:
    p = min(l, m) + n
    m -= p
    l -= p
    if p > 0:
        ans += p - 1
    if p > 0 and l != m:
        ans += 1
print(ans)

Submission status: Wrong Answer

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

c = 0
a = 0
b = 0
ab = 0

for i in range(0, n):
    for j in range(1, len(s[i])):
        if s[i][j-1:j+1] == "AB":
            c += 1
    if s[i][-1] == "A" and s[i][0] == "B":
        ab += 1
    else: 
        if s[i][0] == "B":
            b += 1
        if s[i][-1] == "A":
            a += 1

s = c
s += min(a, b)
a -= min(a, b)
b -= min(a, b)

if(ab > 0):
    s += ab - 1
    if(a > 0):
        s += 1
    if(b > 0):
        s += 1

print(s)

Submission status: Wrong Answer

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

a = [0] * N
b = [0] * N

ans = 0
for i in range(N):
    if s[i][0] == "B":
        b[i] = 1
    if s[i][-1] == "A":
        a[i] = 1
    for j in range(0,len(s[i])-1):
        if s[i][j] == "A" and s[i][j+1] == "B":
            ans += 1

check = [0] * N 
candidate = 0
special = 0

for i in range(N):
    check[i] = a[i] + b[i]
for i in range(N):
    if check[i] == 2:
        special += 1
    if check[i] == 1:
        candidate += 1
if special > 0:
    ans += special - 1
if candidate > 0:
    ans += (candidate + 1) // 2

print(ans)

Submission status: Wrong Answer

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

start = 0
end = 0
same = 0
c = 0
for x in s:
    c = c + len(x.split('AB')) - 1
    if x.startswith('B'):
        start += 1
        if x.endswith('A'):
            same += 1
    if x.endswith('A'):
        end += 1

if start == end:
    c += start
    if same == start:
        c  -= 1
elif start > end:
    c += end
else:
    c += start
print(c)

Submission status: Wrong Answer

Solution code:
n=int(input())

#normal, start_b, end_a, both

kosu=0
tokushu=[0,0,0,0]

for i in range(n):
    line=input()
    switch=0
    for l in range(len(line)):
        if switch==1:
            if line[l]=='B':
                kosu+=1
            else:
                switch=0
        if line[l]=='A':
            switch=1
    
    if line[0]=='B':
        if line[-1]=='A':
            tokushu[3]+=1
        else:
            tokushu[1]+=1
    else:
        if line[-1]=='A':
            tokushu[2]+=1
        else:
            tokushu[0]+=1

#print(kosu)
#print(tokushu)
#gacchi=min(tokushu[1], tokushu[2])
#gacchi2=min(tokushu[3], max(tokushu[1]-gacchi, tokushu[2]-gacchi))

#tokushu=[5,4,4,2]

if tokushu[1]>tokushu[2]:
    gacchi=tokushu[2]
    nokori=tokushu[1]-gacchi
    if nokori==0 and tokushu[3]>0:
        gacchi2=tokushu[3]-1
    elif nokori>=tokushu[3]:
        gacchi2=tokushu[3]
    else:
        gacchi2=tokushu[3]
else:
    gacchi=tokushu[1]
    nokori=tokushu[2]-gacchi
    if nokori==0 and tokushu[3]>0:
        gacchi2=tokushu[3]-1
    elif nokori>=tokushu[3]:
        gacchi2=tokushu[3]
    else:
        gacchi2=tokushu[3]
    
#print(kosu,gacchi,gacchi2)
print(kosu+gacchi+gacchi2)

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+=int(both/2)
index=min(A_list,B_list)
count+=index
if(A_list!=B_list and both>0):
    count+=1
print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
s=' '
num_AB=0
num_A=0
num_B=0
num_A_and_B=0
for i in range(0,N):
    s=str(input())
    if(s[0]=='B'):
        num_B+=1
    if(s[-1]=='A'):
        num_A+=1
    if(s[0]=='B' and s[-1]=='A'):
        num_A_and_B+=1
    for j in range(0,len(s)-1):
        if(s[j]=='A' and s[j+1]=='B'):
            num_AB+=1;
ans=num_AB+min(num_A,num_B)
if(num_A==num_A_and_B and num_B==num_A_and_B):
    ans-=1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
sum = 0
sum_a = 0
sum_b = 0
for i in range(N):
  s = input()
  sum += s.count('AB')
  if (s[-1] == 'A' and s[0] == 'B'):
    sum_b += 1
    sum_a += 1
  elif (s[0] == 'B'):
    sum_b += 1
  elif (s[-1] == 'A'):
    sum_a += 1
print(sum + min(sum_a, sum_b))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
N = int(input())
S = [input() for i in range(N)]
ac = 0
bc = 0
bac = 0
abc = 0
for s in S:
    if s[-1] == 'A':
        ac += 1
        if s[0] == 'B':
            bac += 1
    if s[0] == 'B':
        bc += 1
    abc += s.count('AB')

sur = max(ac, bc) - min(ac, bc)
k = bac - bac//2
print(abc + min(ac-bac, bc-bac) + bac//2 + min(sur, k))

# print(ac, bc, bac, abc)
# cross = min(ac-bac, bc-bac)
# sur = max(ac, bc) - min(ac, bc)
# k = min(sur, bac)
# r = 0 if sur >= bac else bac - sur
# print(abc, cross, k , r//2)
# print(abc + cross + k + r//2)

Submission status: Wrong Answer

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

N = int(raw_input())
S = list()

_count = 0
_A = 0
_B = 0
for i in range(N):
    S.append(raw_input())

for s in S:
    if s[-1] == "A":
        _A += 1
    if s[0] == "B":
        _B += 1
    for i in range(len(s)-1):
        if s[i:i+2] == "AB":
            _count += 1
    
_count += min(_A,_B)

print str(_count)

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>B:
  print(ans+B)
else:
  print(ans+A)

Submission status: Wrong Answer

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

#読み込み
n = int(input())

strs = []

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

#文字列の確認
str_in_num = 0
str_head_a = 0
str_tail_b = 0

for st in strs:
    #文字列中にABを含むもの
    #print(st)
    if len(st) > 2:
        str_in_num += st[0:-1].count('AB')

    #文字列の先頭がBのもの
    if st[-1] == 'A':
        str_head_a += 1

    if st[0] == 'B':
        str_tail_b += 1

print(str_in_num + min(str_head_a, str_tail_b))

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+max(d-1,0))
    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:
from collections import deque

N = int(input())

li_t_a = deque([])
li_t_b = deque([])
ans = ""
for _ in range(N):
    s = input()
    if s[0] == "B":
        li_t_b.append(s)
    elif s[-1] == "A":
        li_t_a.append(s)
    else:
        ans += s

m = min(len(li_t_a), len(li_t_b))
for _ in range(m):
    ans += li_t_a.popleft()
    ans += li_t_b.popleft()
ss = "".join(li_t_a)
ss2 = "".join(li_t_b)
ans = ans + ss + ss2
print(ans.count("AB"))

Submission status: Wrong Answer

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

for i in range(N):
    L.append(input())
    
a = 0
b = 0
ab = 0
ans = 0
for i in range(N):
    s = L[i]
    ab = s.count("AB")
    ans += ab

    if s[0] is "B":
        b += 1
    if s[-1] is "A":
        a += 1
        

if a == b:
    ans += a-1
else:
    ans += min(a, b)



print(ans)

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))
    print(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])
print(AB)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

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

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

ans = count + min(count_a, count_b)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
c = 0
tail_A = 0
head_B = 0
B_A = 0
def count(S):
    if len(S) == 1:
        return 0
    c = 0
    for i in range(len(S)-1):
        if S[i] == 'A' and S[i+1] == 'B':
            c += 1
    return c

for _ in range(N):
    s = input()
    c += count(s)
    if s[-1] == 'A':
        if s[0] == 'B':
            B_A += 1
        else:
            tail_A += 1
    else:
        if s[0] == 'B':
            head_B += 1

if tail_A > 0 and head_B > 0:
    c = c + B_A + 1
    tail_A -= 1
    head_B -= 1
elif tail_A == 0 and head_B > 0:
    c = c + B_A
    head_B -= 1
elif tail_A > 0 and head_B == 0:
    c = c + B_A
    tail_A -= 1
else:
    c = c + B_A - 1

c += min(tail_A, head_B)
print(c)

Submission status: Wrong Answer

Solution code:
N = int(input())
abcount = 0
bleft = 0
aright = 0
for i in range (1,N+1):
    S = input()
    abcount += S.count("AB")
    if S[0] == "B":
        bleft += 1
    if S[-1] == "A":
        aright += 1
print(abcount+min(bleft,aright))

Submission status: Wrong Answer

Solution code:
n=int(input())

ans=0
ba=0
bstart=0
aend=0
for i in range(n):
  s=input()
  if 'AB' in s:
    ans+=1
  
  if s[0]=="B" and s[-1]=="A":
    ba+=1
  else:
    if s[0]=="B":
      bstart+=1
    if s[-1]=="A":
      aend+=1
if ba==0:
  print(ans+min(bstart,aend))
elif bstart>=1 and aend>=1:
  print(ans+ba-1+2+min(bstart-1,aend-1))
else:
  print(ans+ba-1+max(bstart,aend))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
Alist = [word for word in S if word[-1] == 'A']
Blist = [word for word in S if word[0] == 'B']
Slist = [word.count('AB') for word in S]
C = min(len(Alist),len(Blist))
print(sum(Slist)+min(C,N-1))

Submission status: Wrong Answer

Solution code:
n = input()
N = int(n)

count = 0
Acount = 0
AScount = 0
Bcount = 0
BScount = 0
BAcount = 0

for i in range(N):
    s = input()
    if 1 < len(s):
        if 'AB' in s:
            count += s.count('AB')
        if s[0] == 'B' and s[-1] == 'A':
            BAcount += 1
        elif s[0] == 'B':
            Bcount += 1
        elif s[-1] == 'A':
            Acount += 1
    else:
        if s == 'B':
            BScount += 1
        if s == 'A':
            AScount += 1

BAcount += min(BScount, AScount)

if BScount <= AScount:
    Acount += AScount - BScount
else:
    Bcount += BScount - AScount

if 0 < BAcount:
    count += BAcount - 1
    BAcount = 1

if 0 < Acount:
    count += 1
    Acount -= 1
if 0 < Bcount:
    count += 1
    Bcount -= 1

count += min(Bcount, Acount)
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
s_list = list()
counter = 0
a_counter = 0
b_counter = 0

for i in range(n):
    s_list.append(input())
    
for s in s_list:
    counter += s.count("AB")
    if s[0] == "B":
        b_counter += 1
    if s[-1] == "A":
        a_counter += 1
        
count = min(a_counter,b_counter)

counter += count

print(counter)

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:
  ab += 1
print(min([a,b])+ab-1+count)

Submission status: Wrong Answer

Solution code:
N = input()
s = []
flaga = 0
flagbs = 0
flagba = 0
Aend = 0
Bstart = 0
AB = 0
flaginit = 0
for i in range(N):
	s.append(raw_input())
for s2 in s:
	flaginit = 1
	flaga = 0
	flagbs = 0
	for c in s2:
		if flaginit == 1:
			if c =='A':
				flaga = 1
			elif c =='B':
				Bstart += 1
				flagbs = 1
			flaginit = 0
		else:
			if c == 'A':
				flaga = 1
			elif c =='B':
				if flaga == 1:
					AB += 1
				flaga = 0
			else:
				flaga = 0
	if flaga == 1:
		Aend += 1
		if flagbs == 0:
			flagba = 1
	else:
		if flagbs == 1:
			flagba = 1
if(flagba == 0):
	Aend -= 1
AB += min(Aend,Bstart,N-1)
print(AB)

Submission status: Wrong Answer

Solution code:
n = int(input())

ans = 0
A = 0
B = 0
AB = 0
for _ in range(n):
    s = input()
    ans += s.count('AB')
    
    if s[-1] == 'A' and s[0] != 'B':
        A += 1 
    if s[0] == 'B' and s[-1] != 'A':
        B += 1
    if s[-1] == 'A' and s[0] == 'B':
        AB += 1

        
ans += min(A, B)
rest_A_B = max(A, B) - min(A, B)
ans += min(rest_A_B, AB)
if rest_A_B > AB:
    print(ans)
else:
    rest_AB = max(rest_A_B, AB) - min(rest_A_B, AB)
    ans += rest_AB // 2

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
left, right, both = 0, 0, 0
ans = 0
for i in range(N):
    s = input()
    if s[0] == "B":
        if s[-1] == "A":
            both += 1
        else:
            left += 1
    elif s[-1] == "A":
        right += 1
    for j in range(len(s) - 1):
        if s[j] == "A" and s[j + 1] == "B":
            ans += 1
# print(left, right, both, ans)

if min(left, right) >= 1:
    bonus = min(left, right) + both

elif max(left, right) == 0:
    bonus = min(both - 1, 0)

else:
    bonus = both

print(ans + bonus)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())

start_B = 0
end_A = 0

ans = 0
for _ in range(N):
    s = list(input())
    if s[0] == 'B':
        start_B += 1
    if s[-1] == 'A':
        end_A += 1
    for i in range(len(s)-1):
        if s[i] == 'A' and s[i+1] == 'B':
            ans += 1
connection = min(start_B, end_A)
if connection == N:
    connection -= 1
print(ans+connection)

Submission status: Wrong Answer

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

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:
    print(a+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)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [0]*n
i = 0
while(i < n):
    s[i] = str(input())
    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
for i in range(len(s)):
    word = s[i]
    if(word[0] != 'B' and word[len(word)-1] != 'A'):
        notab += 1

add = len(word) - notab -1 
print(count + add)

Submission status: Wrong Answer

Solution code:
N = int(input())

ans = 0
dic = {"A": 0, "B": 0}
dic2 = {"AB": 0, "A": 0, "B": 0}
for i in range(N):
    S = list(str(input()))
    pre = S[0]
    for j in range(1, len(S)):
        if (S[j - 1] + S[j]) == "AB":
            ans += 1
    if S[-1] == "A":
        dic["A"] += 1
    if S[0] == "B":
        dic["B"] += 1

    if S[-1] == "A" and S[0] == "B":
        dic2["AB"] += 1
    elif S[-1] == "A":
        dic2["A"] += 1
    elif S[0] == "B":
        dic2["B"] += 1
if dic2["A"] == 0 and dic2["B"] == 0:
    print(ans + int(dic2["AB"]/2))
else:
    print(ans + min(dic["A"], dic["B"]))

Submission status: Wrong Answer

Solution code:
n = int(input())

count = 0
frontb = 0
lasta = 0

for _ in range(n):
  s = input()
  count+= s.count("AB")
  if s[0] is "B":
    frontb += 1
  if s[-1] is "A":
    lasta += 1
b,a,ba = n-lasta,n-frontb,frontb+lasta-n

if ba > 0:
  count+= ba-1
  if a>0 or b>0:
    count+=1
count += min(a,b) 
    
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N= int(input())

a=0
b = 0
k = 0
ans = 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 s[len(s)-1] == 'A' or s[0] == 'B': k+=1
    

print(ans + min(a,b,N-1,k-1))

Submission status: Wrong Answer

Solution code:
n=int(input())
count=0
dual=0
startb=0
enda=0

f1=0
f2=0

for i in range(n):
    tmp=list(input())
    if tmp[0]=="B" and tmp[-1]=="A":
        #dual+=1
        startb+=1
        enda+=1
    elif tmp[0]=="B":
        startb+=1
        f1+=1
    elif tmp[-1]=="A":
        enda+=1
        f2+=1

    aflag=0
    for t in tmp:
        if aflag==1 and t=="B":
            count+=1
            aflag=0
        elif t=="A":
            aflag=1
        else:
            aflag=0
"""
while dual>0:
    if startb>enda:
        enda+=1
    else:
        startb+=1
    dual-=1
"""
count+=min(startb,enda)

#補正
if f1 ==0 and f2==0:
    count-=1

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
B = 0
A = 0
AB = 0
count = 0

for _ in range(n):
    line = list(input())
    for i in range(len(line)-1):
        if line[i] == 'A' and line[i+1] == 'B':
            count += 1
    if line[0] == 'B' and line[len(line)-1] == 'A':
        AB += 1
    elif line[0] == 'B':
        B += 1
    elif line[len(line)-1] == 'A':
        A += 1


array = [A,B]
count += min(array)

if abs(A-B) != 0:
    count += AB
elif AB > 0:
    count += AB - 1

print(count)