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
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits



def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


def MAP(): return map(int, input().split())


def LIST(): return list(map(int, input().split()))



sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

n = INT()
s = [input() for _ in range(n)]

lis1 = []
lis2 = []
lis3 = []
lis4 = []

for x in s:
    if (x[0] == 'B' and x[-1] == 'A'):
        lis1.append(x)
        
    if (x[-1] == 'A' and x not in lis1):
        lis2.append(x)

    if (x[0] == 'B' and x not in lis1):
        lis3.append(x)
        
for x in s:
    if 'AB' in x and x not in lis1 and x not in lis2 and x not in lis3:
        lis4.append(x)

#print(lis1)
#print(lis2)
#print(lis3)
#print(lis4)

ct = 0
for x in lis1:
    ct += x.count('AB')
for x in lis2:
    ct += x.count('AB')
for x in lis3:
    ct += x.count('AB')
for x in lis4:
    ct += x.count('AB')


if lis2 == [] and lis3 != []:
    p = min(len(lis1), len(lis3))

    #print(ct,p)

    print(ct + p)
elif lis2 == [] and lis3 == []:
    print(len(lis1) - 1 + ct)
else :        
    p = min(len(lis2), len(lis3))
    q = len(lis1)

    #print(ct,p,q)

    print(ct + p + q)

Submission status: Wrong Answer

Solution code:
n =int(input())
a = ["."]*n
for i in range(n):
    a[i] = input().split()

c=0
ica = [0]*n
icb = [0]*n
for i in range(n):
    c += a[i][0].count('AB')
    if a[i][0][-1:] == 'A':
        ica[i] = 1
    if a[i][0][:1] == 'B':
        icb[i] = 1


cc = [1]*n
for i in range(n):
    if ica[i] == 1 and icb[i] == 0:
        cc[i] = 0
        break

e=0
s=0
for i in range(n):
    s=0
    for j in range(n):
        if ica[j] == 1 and icb[j] == 1 and cc[j] == 1:
            cc[j] = 0
            c+=1
            s=1
            break
    if s==0 and e==0:
        for j in range(n):
            if icb[j] == 1 and cc[j] == 1:
                s=1
                e=1
                c+=1
                cc[j]=0
                break
    if s==0 and e==1:
        for j in range(n):
            if ica[j] == 1 and cc[j] == 1:
                e=0
                s=1
                cc[j]=0
                break
    if s==0:
        break

print(c)

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 + dic2["AB"] - 1)
else:
    print(ans + min(dic["A"], dic["B"]))

Submission status: Wrong Answer

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

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 = []
    last_A = []
    num_AB = 0
    # 重複要素
    dupl = []
    for _ in range(N):
        s = ii(str)
        num_AB += s.count("AB")
        if s[0] == "B" and s[-1] == "A":
            dupl.append(s)
            continue
        if s[0] == "B":
            first_B.append(s)
        if s[-1] == "A":
            last_A.append(s)
    comb_AB = min(len(first_B), len(last_A))
    ans = num_AB + comb_AB
    if dupl:
        ans += len(dupl)
    return ans


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

Submission status: Wrong Answer

Solution code:
import re

N = int(input())
bn=0
an=0
abn=0
for i in range(N):
   s = input()
   if s[0] == "B":
      bn += 1
   if s[len(s)-1] == "A":
      an += 1
   ss = re.findall('AB',s)
   abn += len(ss) 

if an < bn:
   ans = an + abn
else:
   ans = bn + abn

print(ans)

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
     
if d >= 1:
    res += d - 1

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:
# -*- 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

        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:
# 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

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

startb = startb-both
enda = enda-both
if startb != enda:
    if both > 2:
        print(count + both-1 + min(enda,startb) + 1)
    else:
        print(count + min(enda, startb)+ both)

else:
    if both > 2:
        print(count + both - 1 + enda)
    else:
        print(count + enda)

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 += AB // 2
ans += min(A, B)

rest_AB = AB % 2
rest_A_B = max(A, B) - min(A, B)
ans += min(rest_AB, rest_A_B)

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

n = int(input())
S = np.asarray([])
count = 0
ac=0
bc=0
for i in range(n):
  S=np.append(S,input())
  tbc=0
  tac=0
  if S[i][0]=="B":
    if ac > 0:
      ac-=1
      count+=1
    else: tbc=1
  if S[i][-1]=="A":
    if bc > 0:
      bc-=1
      count+=1
    else:tac=1
  count += S[i].count("AB")
  ac+=tac
  bc+=tbc
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
c = 0
st = 0
en = 0
for x in range(n):
	s = input()
	c += s.count("AB")
	if s[0] == "B": st += 1
	if s[-1] == "A": en += 1
print(c + min(st,en))

Submission status: Wrong Answer

Solution code:
n = int(input())
firsts = 0
lasts = 0
doubles = 0
inners = 0
for i in range(n):
    s = input()
    first = 1 if (s[0] == "B" and s[-1] != "A") else 0
    last = 1 if (s[-1] == "A" and s[0] != "B") else 0
    double = 1 if (s[0] == "B" and s[-1] == "A") else 0
    inner = s.count("AB")
    # ss += s
    firsts += first
    lasts += last
    doubles += double
    inners += inner

if firsts == 0 and lasts == 0:
    print(inners + doubles - 1)
elif firsts == 0 or lasts == 0:
    print(inners + doubles)
else:
    print(inners + doubles + min(firsts, lasts))

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 - 1)
else:
    print(ct + min(_anum, b_num))

Submission status: Wrong Answer

Solution code:
#coding: utf-8

n = int(input())

s = []


res = 0

a=0
b=0
ba=0

for i in range(0,n):
    now = input()
    res += now.count('AB')
    if now[0] == 'B':
        if now[-1] == 'A':
            ba += 1
        else:
            b += 1
    elif(now[-1] == 'A'):
        a += 1

res += max(0, ba-1)
res += min(a, b)
if (a != b and ba > 0):
    res += 1

print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = [input() for i in range(N)]
bvvv = 0
vvva = 0
bvva = 0
for i in s:
    if i[0] == "B":
        bvvv += 1
    if i[-1] == "A":
        vvva += 1
ABcount = 0
for i in s:
    ABcount += i.count("AB")

print(ABcount + min(bvvv,vvva) - 1 if bvva == bvvv else ABcount + min(bvvv,vvva))

Submission status: Wrong Answer

Solution code:
import numpy as np

n=int(input())
a=0
b=0
ab=0
count=0
for i in range(n):
    s=input()
    if s[0]=='B':
        a+=1
    if s[-1]=='A':
        b+=1
    if s[0]=='B' and s[-1]=='A':
        ab+=1
    for j in range(len(s)-1):
        if s[j]=='A' and s[j+1]=='B':
            count+=1
if a==ab and b==ab:
    count+=min([a,b])-1
else:
    count+=min([a,b])
print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[input() for __ in range(n)]
a_ed=[a for a in s if a.endswith('A')]
b_st=[b for b in s if b.startswith('B')]
ab_in=[ab for ab in s if ('AB') in ab]

print(len(ab_in)+min(len(a_ed),len(b_st)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

res = 0
counta = 0
countb = 0
countab = 0
for s in S:
    res += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        countab += 1
    elif s[0] == "B":
        countb += 1
    elif s[-1] == "A":
        counta += 1

if min(counta, countab) + min(countb, countab) < countab:
    res += countab - 1
    counta += 1
    countb += 1
else:
    res += min(counta, countab)
    res += min(countb, countab)
    counta += max(0, countab - counta)
    countb += max(0, countab - countb)

    counta = max(0, counta - min(counta, countab))
    countb = max(0, countb - min(countb, countab))
res += min(counta, countb)
print(res)

Submission status: Wrong Answer

Solution code:
i = 1
n = input("")
num = int(n)
string = input("")
array = [string]
while i < num :
  strtemp = input("")
  array.append(strtemp)
  i+=1
front = 0
back = 0
is_a = 0
is_b = 0
is_ab = 0
f_b = 0
a_b = 0
for tmp in array :
  char = list(tmp)
  for tmp2 in char :
    if tmp2 == "A" :
      is_a = 1
      is_b = 0
    if is_a == 1 and tmp2 == "B" :
      is_ab += 1
      is_a = 0
    elif tmp2 == "B" :
      is_a = 0
      is_b = 1
      if char[0] == tmp2 :
        front += 1
        f_b = 1
  if is_a == 1 :
    back += 1
    if f_b == 1 :
      a_b += 1
  is_a = 0
  is_b = 0
  f_b = 0
if front >= back :
  is_ab += back
elif back > front :
  is_ab += front
if a_b >= 2 :
  is_ab -= 1
if is_ab < 0 :
  is_ab = 0
print(is_ab)

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 + min(a,b))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for i in range(n)]
count = 0
countA=0
countB=0
countAB = 0
for i in s:
    count += i.count("AB")
    if (i[-1:] == "A"):
        countA +=  1
    if (i[0] == "B"):
        countB += 1
    if (i[-1:] == "A" and i[0] == "B"):
        countAB += 1
countC = (countA,countB)
ans = count+min(countC)
if(countA == countB and countAB != 0):
    ans -=1
print(ans)

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

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

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

    return max(res1, res2)


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:
l = raw_input().split()
N = int(l[0])
S = [0]*N
for i in range(N):
    l = raw_input().split()
    S[i] = l[0]

#print(N, S)

count = 0
for s in S:
    count += s.count('AB')
#print(count)

numA = 0
numB = 0
numAB = 0
for s in S:
    if s[0] != 'B' and s[-1] == 'A':
        numA += 1
    if s[0] == 'B' and s[-1] != 'A':
        numB += 1
    if s[0] == 'B' and s[-1] == 'A':
        numAB += 1

count += min(numA, numB)
count += numAB - 1
if numA != numB and numAB > 0:
    count += 1
print(count)

Submission status: Wrong Answer

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

countAB = 0
firstB = 0
lastA = 0

for i in range(len(s)):
    if s[i][0] == 'B':
        firstB = firstB + 1
    if s[i][-1] == 'A':
        lastA = lastA + 1
    for j in range(len(s[i]) - 1):
        if s[i][j] == 'A' and s[i][j + 1] == 'B':
            countAB = countAB + 1

if lastA == n and firstB == n:
    print(countAB + lastA - 1)
elif firstB > lastA:
    print(countAB + lastA)
else:
    print(countAB + firstB)

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 "AB" in S[i]:
        while "AB" in S[i]:
            ans += 1
            tmpIndex = S[i].find("AB")
            S[i] = S[i][0:tmpIndex]+S[i][tmpIndex+2:]

for j in S:
    if j.startswith("B"): BFirst+=1
    if j.endswith("A"): ALast+=1
print(ans + min(BFirst, ALast))

Submission status: Wrong Answer

Solution code:
n=int(input())
ab=0
a=[0]*n
b=[0]*n
for i in range(n):
  s=input()
  ab+=s.count("AB")
  if(s[0]=='B'):
    b[i]=1
  if(s[-1]=='A'):
    a[i]=1
c=0
a1=a
b1=b
for i in range(n):
  for j in range(n):
    if(i!=j and b[i]==1 and a[j]==1):
      a[i]=0
      b[i]=0
      c+=1
      break
d=0
for i in range(n):
  for j in range(n):
    if(i!=j and a1[i]==1 and b1[j]==1):
      a1[i]=0
      b1[i]=0
      d+=1
      break
print(ab+max(c,d))

Submission status: Wrong Answer

Solution code:
n = int(input())
ab = 0
b_start = 0
a_end = 0
for ln in range(n):
    line = input().rstrip()
    ab += line.count("AB")
    if line[0] == 'B':
        b_start += 1
    if line[-1] == 'A':
        a_end += 1

print(ab + min(a_end, b_start))

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
        continue
    if s[i][-1]=='A':
        a += 1
    if s[i][0]=='B':
        b += 1
if ab%2==0 and a!=b:
    k = 1
else:
    k = 0
print(ans+ab//2+ab%2+min(a,b)+k)

Submission status: Wrong Answer

Solution code:
n =  int(input())
a,b,c,d = 0,0,0,0
for _ in [0]*n:
  s = input()
  if s[0] == "B" and  s[-1] == "A":
    d += 1
  elif s[0] == "B":
    b += 1
  elif s[-1] == "A":
    a += 1
  for i in range(len(s)-1):
    if s[i:i+2] == "AB":
      c += 1
if a == 0 and b == 0:
  c += min(d-1,0)
else:
   c += d + min(a,b)
print(c)

Submission status: Wrong Answer

Solution code:
n,*s=map(str,open(0).read().split())
n=int(n)
b=sum([c.startswith('B')for c in s])
a=sum([c.endswith('A')for c in s])
e=a if b>=a else b
q=[c.startswith('B')and c.endswith('A')for c in s]
if all(q) or e==n:e-=1
if sum(q)==1and b==1and a==1:e=0
print(sum([c.count('AB')for c in s])+e)

Submission status: Wrong Answer

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

count = 0
aa = 0
bb = 0
f = False
flag=0
for a in A:
    if a[0]=='B':
        bb+=1
        f = True
    if a[-1]=='A':
        aa+=1
        if f:
            flag +=1
    f=False
    count += a.count('AB')
if flag:
    if aa==bb:
        if flag>(aa-flag):
            aa-=1
count = count+min(aa,bb)
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#d=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
#import numpy as np
#n=int(input())
#li=input().split()
#import fractions
#lis=list(map(int,input().split()))

n=int(input())
S=[]
ans=0
firstB,lastA,bothAB=0,0,0

for i in range(n):
    S.append(input())
    flag=0
    for j in range(len(S[-1])-1):
        if S[-1][j:j+2]=="AB":
            ans+=1
    if S[-1][0]=="B":
        firstB+=1
        flag+=1
    if S[-1][-1]=="A":
        lastA+=1
        flag+=1
    if flag==2:
        bothAB+=1
        firstB-=1
        lastA-=1

if max(firstB,lastA)!=0:
    if firstB==lastA:
        ans+=(2*bothAB+firstB-1)
    else:
        ans+=min(firstB,lastA)+bothAB
else:
    ans+=(bothAB-1)

print(ans)

Submission status: Wrong Answer

Solution code:
#python 3.5.2

N = int(input())
s_list = [input() for i in range(N)]
cnt = sum([1 for s in s_list if "AB" in s])
a_end, b_start, a_b = 0, 0, 0
for s in s_list:
    if s[0] == "B" and s[-1] == "A":
        a_b += 1
    elif s[0] == "B":
        b_start += 1
    elif s[-1] == "A":
        a_end += 1
if a_b == 0:
    cnt += min(a_end, b_start)
else:
    cnt += a_b - 1
    if a_end >= 1:
        cnt += 1
        a_end -= 1
    if b_start >= 1:
        cnt += 1
        b_start -= 1
    cnt += min(a_end, b_start)
print(cnt)

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))
else:
  if 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 = []
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
        
    if 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 = [input() for _ in range(n)]

acount = 0
bcount = 0
abcount = 0
ans = 0

for i in range(len(s)):
    if s[i][0] == 'B':
        bcount += 1
    if s[i][-1] == 'A':
        acount += 1
    if s[i][0] == 'B' and s[i][-1] == 'A':
        abcount += 1
    for j in range(len(s[i])-1):
        if s[i][j] == 'A' and s[i][j+1] == 'B':
            ans += 1

if max(abcount,acount,bcount) == abcount:
    print(ans + min(acount,bcount) -1)
else:
    print(ans + min(acount,bcount))

Submission status: Wrong Answer

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

ab = 0
abc = 0
aend = 0
bbeg = 0
for s in S:
	if "AB" in s:
		abc += s.count("AB")
	if "A" == s[-1] and "B" == s[0]:
		ab += 1
	elif "A" == s[-1]:
		aend += 1
	elif "B" == s[0]:
		bbeg += 1

if 2 < ab:
	ab -= 1

print(abc + min(aend + ab, bbeg + ab))

Submission status: Wrong Answer

Solution code:
i = 1
n = input("")
num = int(n)
string = input("")
array = [string]
while i < num :
  strtemp = input("")
  array.append(strtemp)
  i+=1
front = 0
back = 0
is_a = 0
is_b = 0
is_ab = 0
f_b = 0
a_b = 0
for tmp in array :
  char = list(tmp)
  for tmp2 in char :
    if tmp2 == "A" :
      is_a = 1
      is_b = 0
    if is_a == 1 and tmp2 == "B" :
      is_ab += 1
      is_a = 0
    elif tmp2 == "B" :
      is_a = 0
      is_b = 1
      if char[0] == tmp2 :
        front += 1
        f_b = 1
  if is_a == 1 :
    back += 1
    if f_b == 1 :
      a_b += 1
  is_a = 0
  is_b = 0
  f_b = 0
if front >= back :
  is_ab += back
elif back > front :
  is_ab += front
if a_b >= 2 :
  is_ab -= 1
print(is_ab)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

count=0
for i in range(N):
    ok=S[i].find("AB")
    if ok!=-1:
        count=count+1

pre=[]
la=[]
for i in range(N):
    if S[i][0]=="B":
        pre.append(i)
    if S[i][-1]=="A":
        la.append(i)

a=min(len(pre),len(la))
count=count+a
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())

moji_list = []
A_count = 0
B_count = 0
AB_count = 0

if N == 1:
    moji = input()
    print(moji.count('AB'))

else:
    for i in range(N):
        moji = input()
        if moji[0] == 'B':
            B_count += 1
        if moji[-1] == 'A':
            A_count += 1
        
        AB_count += moji.count('AB')

    if AB_count == 0 and A_count == 0:
        print(0)

    elif A_count == B_count:
        print(AB_count+B_count-1)

    elif A_count > B_count:
        print(AB_count+B_count)
    else:
        print(AB_count+A_count)

Submission status: Wrong Answer

Solution code:
N = int(input())

back_a = 0
forward_b = 0
inside = 0
for i in range(N):
    line = input()
    if line[-1] == 'A':
        back_a += 1
    if line[0] == 'B':
        forward_b += 1
    for s in range(len(line) - 1):
        if line[s] == 'A' and line[s + 1] == 'B':
            inside += 1
print(min(back_a, forward_b) + inside)

Submission status: Wrong Answer

Solution code:
import re


pattern = "AB"
num_input = int(input())

total_ab = 0

num_ab = 0
num_end_with_a = 0
num_start_with_b = 0
num_both = 0

prints = []

for i in range(0, num_input):
    input_string = input()
    num_ab += len(re.findall(pattern, input_string))
    start_with_b = (input_string[0] == 'B')
    end_with_a = (input_string[-1] == 'A')

    if start_with_b and end_with_a:
        num_both += 1
    elif start_with_b:
        num_start_with_b += 1
    elif end_with_a:
        num_end_with_a += 1

# print(num_ab, num_both, num_start_with_b, num_end_with_a)

# add in-string 'AB'
total_ab += num_ab

# consume both first
if num_end_with_a == 0:
    num_both -= 1
    num_end_with_a += 1

if num_start_with_b == 0:
    num_both -= 1
    num_start_with_b += 1

if num_both > 0:
    total_ab += (num_both+1)
    num_end_with_a -= 1
    num_start_with_b -= 1

# then consume other '--A's  and 'B--'s
if min(num_start_with_b, num_end_with_a) > 0:
    total_ab += min(num_start_with_b, num_end_with_a)

print(total_ab)
exit(0)

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-1+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)

Submission status: Wrong Answer

Solution code:
n=int(input())  #数値入力

list_s = []
for i in range(n):
    list_s.append(input())

sum_ab = 0
sum_ab += sum([x[:-1].count('AB') for x in list_s])

ba = 0
bx = 0
xa = 0
for x in list_s:
    if (x[0] == 'B') & (x[-1] == 'A'):
        ba += 1
    elif x[0] == 'B':
        bx += 1
    elif x[-1] == 'A':
        xa += 1
        
# print(ba, bx, xa)

if ba > 1:
    sum_ab += ba - 1

sum_ab += min(bx, xa)

if (ba > 0) & (bx != xa):
    sum_ab += 1

print(sum_ab)

Submission status: Wrong Answer

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

base_ab=0
last_a=0
first_b=0
for s in string:
    base_ab+=s.count("AB")
    if s[-1]=="A":
        last_a+=1
    if s[0]=="B":
        first_b+=1
print(base_ab+min(last_a,first_b))

Submission status: Wrong Answer

Solution code:
n = int(input())
a, b, ba = 0, 0, 0
ans = 0
for _ in range(n):
    s = str(input())
    if s[0] == 'B' and s[-1] == 'A':
        ba += 1
    elif s[0] == 'B':
        b += 1
    elif s[-1] == 'A':
        a += 1
    for i in range(len(s) - 1):
        if s[i: i + 2] == 'AB':
            ans += 1
d = min([a, b, ba])
ans += 2 * d
a -= d
b -= d
ba -= d
if ba == 0:
    ans += min(a, b)
else:
    c = max(a, b)
    if c <= ba:
        ans += (ba + c) // 2
    else:
        ans += ba
print(ans)

Submission status: Wrong Answer

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

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/2)
    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/2)
    else:
        temp = min(len_2, hoge)

ans = count + temp

print(ans)

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' and s[-1] is 'A':
        ab += 1
    if s[0] is 'B':
        a += 1
    if s[-1] is 'A':
        b += 1
if a == ab and b == ab:
    a -= 1
print(ans+min(a,b))

Submission status: Wrong Answer

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