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())
ans=0
cnt_a,cnt_b=0,0
for i in range(N):
    s=input()
    ans+=s.count("AB")
    s=s.replace("AB","")
    if s[0]=="B":
        cnt_b+=1
    if s[-1]=="A":
        cnt_a+=1
print(ans+min(cnt_a,cnt_b))

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
cnt_a, cnt_b, cnt_ab  = 0, 0, 0
for i in range(n):
    s = input()
    ans += s.count("AB")
    if s[-1] == "A":
        cnt_a += 1
    if s[0] == "B":
        cnt_b += 1
    if s[0] == "B" and s[-1] == "A":
        cnt_ab += 1

print(ans + min(cnt_a, cnt_b) if cnt_ab < max(cnt_a, cnt_b) else ans + (cnt_ab - 1))

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
cnt_a, cnt_b, cnt_ab  = 0, 0, 0
for i in range(n):
    s = input()
    ans += s.count("AB")
    if s[-1] == "A":
        cnt_a += 1
    if s[0] == "B":
        cnt_b += 1
    if s[0] == "B" and s[-1] == "A":
        cnt_ab += 1

print(ans + min(cnt_a, cnt_b) if cnt_ab != cnt_a or cnt_ab != cnt_b else max(0, ans + cnt_ab - 1))

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
cnt_a, cnt_b, cnt_ab  = 0, 0, 0
for i in range(n):
    s = input()
    ans += s.count("AB")
    if s[-1] == "A":
        cnt_a += 1
    if s[0] == "B":
        cnt_b += 1
    if s[0] == "B" and s[-1] == "A":
        cnt_ab += 1

print(ans + min(cnt_a, cnt_b) if cnt_ab != cnt_a or cnt_ab != cnt_b else ans + cnt_ab - 1)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
cnt_a, cnt_b, cnt_ab  = 0, 0, 0
for i in range(n):
    s = input()
    ans += s.count("AB")
    if s[-1] == "A":
        cnt_a += 1
    if s[0] == "B":
        cnt_b += 1
    if s[0] == "B" and s[-1] == "A":
        cnt_ab += 1

print(ans + min(cnt_a, cnt_b) if cnt_ab != cnt_a or cnt_ab != cnt_b else ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
cnt_a, cnt_b = 0, 0
for i in range(n):
    s = input()
    ans += s.count("AB")
    if s[-1] == "A":
        cnt_a += 1
    if s[0] == "B":
        cnt_b += 1

print(ans + min(cnt_a, cnt_b))

Submission status: Wrong Answer

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

last_A = []
first_B = []
A_B = []
for i in range(N):
    if (S[i][0] == 'B' and S[i][-1] == 'A'):
        A_B.append(S[i])
    elif (S[i][0] == 'B'):
        first_B.append(S[i])
    elif (S[i][-1] == 'A'):
        last_A.append(S[i])

count = 0
for i in S:
    for j in range(len(i) - 1):
        if i[j] == 'A' and i[j + 1] == 'B':
            count += 1
# 両方の場合から先に処理する
if (len(A_B) > 0):
    count_A_B = len(A_B)
    count_last_A = len(last_A)
    count_first_B = len(first_B)
    while count_A_B > 0:
        if (count_last_A == 0 or count_first_B == 0):
            break
        count_last_A -= 1
        count_first_B -= 1
        count_A_B -= 1
        count += 2
    if (count_last_A == 0 and count_first_B == 0 and count_A_B != 0):
        count += count_A_B - 1
        print(count)
        exit()
    elif (count_A_B != 0):
        count += count_A_B
        print(count)
        exit()
    else:
        count += abs(count_last_A - count_first_B)
        print(count)
        exit()

else:
    if (len(last_A) >= len(first_B)):
        count += len(first_B)
    else:
        count += len(last_A)

    print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
s_array = [input() for x in range(n)]
a_count = 0
b_count = 0
ab_count = 0
res = 0
for s in s_array:
    if 'AB' in s:
        res += 1
    if s[-1] == "A" and s[0] != "B":
        a_count += 1
    elif s[0] == "B" and s[-1] != "A":
        b_count += 1
    elif s[0] == "B" and s[-1] == "A":
        ab_count += 1
    else:
        pass

if n == 1:
    print(res)
elif ab_count == 0:
    print(res + min(a_count, b_count))
elif a_count == 0 and b_count == 0:
    print(res - 1 + ab_count)
else:
    print(res + min(a_count, b_count) + ab_count)

Submission status: Wrong Answer

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

# 文字列にABが入ってる数 + 組み合わせて入る数
# 文字列にABが入ってる数 + f(Aで終わる数, Bで始まる数, Bで始まりAで終わる数)
include = 0
sb = 0
fa = 0
sbfa = 0
for s in S:
    if 'AB' in s:
        include += 1
    if s[0] == 'B' and s[-1] == 'A':
        sbfa += 1
    elif s[0] == 'B':
        sb += 1
    elif s[-1] == 'A':
        fa += 1

res = include
a_count = fa
b_count = sb
ab_count = sbfa
n = N
if n == 1:
    print(res)
elif ab_count == 0:
    print(res + min(a_count, b_count))
elif a_count == 0 and b_count == 0:
    print(res - 1 + ab_count)
else:
    print(res + min(a_count, b_count) + ab_count)

Submission status: Wrong Answer

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

# 文字列にABが入ってる数 + 組み合わせて入る数
# 文字列にABが入ってる数 + f(Aで終わる数, Bで始まる数, Bで始まりAで終わる数)
include = 0
sb = 0
fa = 0
sbfa = 0
for s in S:
    if 'AB' in s:
        include += 1
    if s[0] == 'B' and s[-1] == 'A':
        sbfa += 1
    elif s[0] == 'B':
        sb += 1
    elif s[-1] == 'A':
        fa += 1

def calc_comb(sb, fa, sbfa):
    ans = 0
    if sbfa > 0:
        ans += sbfa-1
        if sb > 0:
            sb -= 1
            ans += 1
        if fa > 0:
            fa -= 1
            ans += 1
    ans += min(sb, fa)
    return ans

print(include + calc_comb(sb, fa, sbfa))

Submission status: Wrong Answer

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

# 文字列にABが入ってる数 + 組み合わせて入る数
# 文字列にABが入ってる数 + f(Aで終わる数, Bで始まる数, Bで始まりAで終わる数)
include = 0
sb = 0
fa = 0
sbfa = 0
for s in S:
    if 'AB' in s:
        include += 1
    if s[0] == 'B' and s[-1] == 'A':
        sbfa += 1
    elif s[0] == 'B':
        sb += 1
    elif s[-1] == 'A':
        fa += 1

def calc_comb(sb, fa, sbfa):
    if max(sb, fa) != 0:
        return sbfa + min(sb, fa)
    elif sbfa > 0:
        return sbfa -1
    else:
        return 0

print(include + calc_comb(sb, fa, sbfa))

Submission status: Wrong Answer

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

# 文字列にABが入ってる数 + 組み合わせて入る数
# 文字列にABが入ってる数 + f(Aで終わる数, Bで始まる数, Bで始まりAで終わる数)
include = 0
sb = 0
fa = 0
sbfa = 0
for s in S:
    if 'AB' in s:
        include += 1
    if s[0] == 'B' and s[-1] == 'A':
        sbfa += 1
    elif s[0] == 'B':
        sb += 1
    elif s[-1] == 'A':
        fa += 1

def calc_comb(sb, fa, sbfa):
    if sbfa == 0:
        return min(sb, fa)
    elif (sb == 0) and (fa == 0):
        return max(0, sbfa-1)
    else:
        return sbfa + min(sb, fa)

print(include + calc_comb(sb, fa, sbfa))

Submission status: Wrong Answer

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

# 文字列にABが入ってる数 + 組み合わせて入る数
# 文字列にABが入ってる数 + f(Aで終わる数, Bで始まる数, Bで始まりAで終わる数)
include = 0
sb = 0
fa = 0
sbfa = 0
for s in S:
    if 'AB' in s:
        include += 1
    if s[0] == 'B' and s[-1] == 'A':
        sbfa += 1
    elif s[0] == 'B':
        sb += 1
    elif s[-1] == 'A':
        fa += 1

def calc_comb(sb, fa, sbfa):
    if sb != fa:
        return sbfa + min(sb, fa)
    elif sbfa > 0:
        return sbfa - 1 + min(sb, fa)
    else:
        return min(sb, fa)

print(include + calc_comb(sb, fa, sbfa))

Submission status: Wrong Answer

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

# 文字列にABが入ってる数 + 組み合わせて入る数
# 文字列にABが入ってる数 + f(Aで終わる数, Bで始まる数, Bで始まりAで終わる数)
include = 0
sb = 0
fa = 0
sbfa = 0
for s in S:
    if 'AB' in s:
        include += 1
    if s[0] == 'B' and s[-1] == 'A':
        sbfa += 1
    elif s[0] == 'B':
        sb += 1
    elif s[-1] == 'A':
        fa += 1

def calc_comb(sb, fa, sbfa):
    if sb != fa:
        return sbfa + min(sb, fa)
    else:
        return sbfa - 1 + min(sb, fa)

print(include + calc_comb(sb, fa, sbfa))

Submission status: Wrong Answer

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

# 文字列にABが入ってる数 + 組み合わせて入る数
# 文字列にABが入ってる数 + f(Aで終わる数, Bで始まる数, Bで始まりAで終わる数)
include = 0
sb = 0
fa = 0
sbfa = 0
for s in S:
    if 'AB' in s:
        include += 1
    if s[0] == 'B' and s[-1] == 'A':
        sbfa += 1
    elif s[0] == 'B':
        sb += 1
    elif s[-1] == 'A':
        fa += 1

def calc_comb(sb, fa, sbfa):
    return sbfa + min(sb, fa)

print(include + calc_comb(sb, fa, sbfa))

Submission status: Wrong Answer

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

# 文字列にABが入ってる数 + 組み合わせて入る数
# 文字列にABが入ってる数 + f(Aで終わる数, Bで始まる数, Bで始まりAで終わる数)
include = 0
sb = 0
fa = 0
sbfa = 0
for s in S:
    if 'AB' in s:
        include += 1
    if s[0] == 'B' and s[-1] == 'A':
        sbfa += 1
    elif s[0] == 'B':
        sb += 1
    elif s[-1] == 'A':
        fa += 1

def calc_comb(sb, fa, sbfa):
   diff = abs(sb-fa) 
   if sbfa <= diff:
       return sbfa + min(sb, fa)
   else:
       return sbfa - diff + max(sb, fa)

print(include + calc_comb(sb, fa, sbfa))

Submission status: Wrong Answer

Solution code:
n=int(input())
c=0
a=0
b=0
ab=0

for i in range(n):
  s=input()
  c+=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

c+=min(a,b)+ab if max(a,b)!=0 else ab-1
print(c)

Submission status: Wrong Answer

Solution code:
n=int(input())
c=0
a=0
b=0
ab=0

for i in range(n):
  s=input()
  c+=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
c+=min(a+1,b+1)+(ab-1)
print(c)

Submission status: Wrong Answer

Solution code:
n=int(input())
c=0
a=0
b=0
ab=0

for i in range(n):
  s=input()
  c+=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
c+=min(a,b)+ab
print(c)

Submission status: Wrong Answer

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

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()

def main():
	n=II()

	a=0
	b=0
	ans=0

	for _ in range(n):
		s=input().rstrip().decode()
		ans+=s.count("AB")
		if s[0]=="B":
			b+=1
		if s[-1]=="A":
			a+=1

	print(ans+min(a,b))







if __name__ == "__main__":
	main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
xa = 0
by = 0
ba = 0
counter = 0
for _ in range(n):
  s = input()
  counter += s.count('AB')
  if s[0]=='B':
    if s[-1]=='A':
      ba += 1
    else:
      by += 1
  elif s[-1] == 'A':
    xa += 1
if ba > 0:
  counter += ba -1
counter += min(xa, by)
if xa != by and ba >0:
  counter += 1
print(counter)

Submission status: Wrong Answer

Solution code:
from math import ceil,floor,comb,factorial,gcd,pow,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

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

a = 0
b = 0
ab = 0
c = 0
for x in s:
    ab += x.count("AB")
    if x[0] == 'B' and x[-1] == 'A':
        c += 1
    elif x[0] == 'B':
        b += 1
    elif x[-1] == 'A':
        a += 1
    
print( ab + max(c-1, 0) + min(a, b) + (1 if a + b != 0 else 0))

Submission status: Wrong Answer

Solution code:
from math import ceil,floor,comb,factorial,gcd,pow,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

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

a = 0
b = 0
ab = 0
c = 0
for x in s:
    ab += x.count("AB")
    if x[0] == 'B' and x[-1] == 'A':
        c += 1
    elif x[0] == 'B':
        b += 1
    elif x[-1] == 'A':
        a += 1
    
print( ab + max(c-1, 0) + min(a, b) + (1 if a != b and 0 < c < n else 0))

Submission status: Wrong Answer

Solution code:
from math import ceil,floor,comb,factorial,gcd,pow,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

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

a = 0
b = 0
ab = 0
c = 0
for x in s:
    ab += x.count("AB")
    if x[0] == 'B' and x[-1] == 'A':
        c += 1
    elif x[0] == 'B':
        b += 1
    elif x[-1] == 'A':
        a += 1
    
print( ab + min(n - 1, max(c-1, 0) + min(a, b) + (1 if a != b and c > 0 else 0)))

Submission status: Wrong Answer

Solution code:
from math import ceil,floor,comb,factorial,gcd,pow,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

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

a = 0
b = 0
ab = 0
c = 0
for x in s:
    ab += x.count("AB")
    if x[0] == 'B' and x[-1] == 'A':
        c += 1
    elif x[0] == 'B':
        b += 1
    elif x[-1] == 'A':
        a += 1
    
print( ab + max(c-1, 0) + min(a, b) + (1 if a != b and c > 0 else 0) )

Submission status: Wrong Answer

Solution code:
from math import ceil,floor,comb,factorial,gcd,pow,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

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

a = 0
b = 0
ab = 0
c = 0
for x in s:
    ab += x.count("AB")
    if x[0] == 'B' and x[-1] == 'A':
        c += 1
    elif x[0] == 'B':
        b += 1
    elif x[-1] == 'A':
        a += 1
    
print( ab + max(c-1, 0) + min(a, b) + (1 if a != b else 0) )

Submission status: Wrong Answer

Solution code:
n = int(input())
hB = set()
tA = set()
cAB = 0
for i in range(n):
    s = input()
    cAB += s.count('AB')
    if s[0] == 'B': hB.add(i)
    if s[-1] == 'A': tA.add(i)
if tA==hB:
    print(cAB+min(len(hB),len(tA))-1)
else:
    print(cAB+min(len(hB),len(tA)))

Submission status: Wrong Answer

Solution code:
n, *ss = open(0).read().split()

ans = 0

inter_count = sum([s.count('AB') for s in ss])

ans += inter_count

_a = sum(s[-1] == 'A' for s in ss)
b_ = sum(s[0] == 'B' for s in ss)
b_a = sum(s[0] == 'B' and s[-1] == 'A' for s in ss)
_a -= b_a
b_ -= b_a

ans += b_a-1

if b_a:
    ans += bool(b_)
    b_ -= bool(b_)
    ans += bool(_a)
    _a -= bool(_a)

ans += min(b_, _a)

print(ans)

Submission status: Wrong Answer

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

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

if cnt[1] + cnt[2] == 0:
    print(ans + cnt[0] - 1)
else:
    print(ans + min(cnt[1], cnt[2]) + cnt[0])

Submission status: Wrong Answer

Solution code:
n = int(input())

cnt_a = 0   # Bで始まらないがAで終わる
cnt_b = 0   # Bで始まるがAで終わらない
cnt_ba = 0  # Bで始まりAで終わる

ans = 0
for _ in range(n):
    s = input()
    ans += s.count('AB')
    if s.endswith('A') and s.startswith('B'):
        cnt_ba += 1
    else:
        if s.endswith('A'):
            cnt_a += 1
        if s.startswith('B'):
            cnt_b += 1

if cnt_a == 0:
    ans += cnt_ba - 1
    if cnt_b > 0:
        ans += 1
else:
    ans += cnt_ba
    cnt_a -= 1
    if cnt_b > 0:
        ans += 1
        cnt_b -= 1
    # 残ったxxAとBxxを組み合わせてABを作る
    ans += min(cnt_a, cnt_b)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

ends_a = 0
starts_b = 0

ans = 0
for _ in range(n):
    s = input()
    ans += s.count('AB')
    if s.endswith('A'):
        ends_a += 1
    if s.startswith('B'):
        starts_b += 1

print(ans + min(ends_a, starts_b))

Submission status: Wrong Answer

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

AB_cnt = 0
c1 = 0
c2 = 0
c3 = 0


for s in S:
    AB_cnt += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        c1 += 1
    elif s[0] == 'B' and s[-1] != 'A':
        c2 += 1
    elif s[-1] == 'A' and s[0] != 'B':
        c3 += 1
print(AB_cnt + c1 + min(c2,c3) if c2 + c3 > 0 else AB_cnt + c1 - 1)

Submission status: Wrong Answer

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

AB_cnt = 0
c1 = 0
c2 = 0
c3 = 0


for s in S:
    AB_cnt += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        c1 += 1
    elif s[0] == 'B':
        c2 += 1
    elif s[-1] == 'A':
        c3 += 1
print(AB_cnt + c1 + min(c2,c3) if c2 + c3 > 0 else AB_cnt + c1 - 1)

Submission status: Wrong Answer

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

A_cnt = 0
B_cnt = 0
AB_cnt = 0

for s in S:
    AB_cnt += s.count('AB')
    A_cnt += s[-1].count('A')
    B_cnt += s[0].count('B')
print(AB_cnt + min(A_cnt,B_cnt) if min(A_cnt,B_cnt) != N else AB_cnt + min(A_cnt,B_cnt) - 1)

Submission status: Wrong Answer

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

A_cnt = 0
B_cnt = 0
AB_cnt = 0

for s in S:
    AB_cnt += s.count('AB')
    A_cnt += s[-1].count('A')
    B_cnt += s[0].count('B')
print(AB_cnt + min(A_cnt,B_cnt))

Submission status: Wrong Answer

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

ba = 0
bo = 0
oa = 0
ans = 0
for i in range(n):
    ans += s[i].count('AB')
    if s[i][0] == 'B' and s[i][-1] == 'A':
        ba += 1
    elif s[i][0] == 'B':
        bo += 1
    elif s[i][-1] == 'A':
        oa += 1
#print(ans, ba, min(bo, oa))

if ba > 0:
    ans += ba - 1
    ba = 1
pair = min(bo, oa)
ans += pair

bo -= pair
oa -= pair

if ba > 0 and bo > 0:
    ans += 1
if ba > 0 and oa > 0:
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
lb=0
ra=0
ba=0
ans=0

import re

for _ in range(n):
    s=input()

    ans+=s.count('AB')

    if s[0]=='B' and s[-1]=='A':
        ba+=1
    elif s[0]=='B':
        lb+=1
    elif s[-1]=='A':
        ra+=1

tmp=min(lb,ra,ba)
ans += 2*tmp

lb-=tmp
ra-=tmp
ba-=tmp
if lb<0:
    lb=0
if ra<0:
    ra=0
if ba<0:
    ba=0


if ba>0:
    ans+=ba-1
    ba=1
    
if lb!=0 or ra!=0:
    ans+=min(lb,ra)
    ans+=min(lb,ba)
    ans+=min(ra,ba)


print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
lb=0
ra=0
ba=0
ans=0

import re

for _ in range(n):
    s=input()

    ans+=s.count('AB')

    if s[0]=='B' and s[-1]=='A':
        ba+=1
    elif s[0]=='B':
        lb+=1
    elif s[-1]=='A':
        ra+=1

tmp=min(lb,ra,ba)
ans += 2*tmp

lb-=tmp
ra-=tmp
ba-=tmp
if lb<0:
    lb=0
if ra<0:
    ra=0
if ba<0:
    ba=0

if lb==0 and ra==0 and ba>0:
    ans+=ba-1
elif lb!=0 or ra!=0:
    ans+=min(lb,ra)
    ans+=min(lb,ba)
    ans+=min(ra,ba)


print(ans)

Submission status: Wrong Answer

Solution code:


def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    cnt=0
    a=0
    b=0
    A="A"
    B="B"
    ab=0
                
    
    for i in range(N):
        s=input()
        cnt+=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
            
    if a==0 and b==0:
        cnt+=ab-1
    else:
        cnt+=ab
    ans=cnt+min(a,b)
    print(ans)

main()

Submission status: Wrong Answer

Solution code:


def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    cnt=0
    a=0
    b=0
    A="A"
    B="B"
    ab=0
    
    def calc(s):
        af=0
        temp=0
        for i in range(len(s)):
            if af and s[i]==B:
                temp+=1
            if s[i]==A:
                af=1
            else:
                af=0
        return temp
                
    
    for i in range(N):
        s=input()
        cnt+=calc(s)
        if s[0]==B and s[-1]==A:
            ab+=1
        elif s[0]==B:
            b+=1
        elif s[-1]==A:
            a+=1
            
    if ab>=1:
        cnt+=ab-1
        if a!=b:
            cnt+=1
    ans=cnt+min(a,b)
    print(ans)

main()

Submission status: Wrong Answer

Solution code:


def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    cnt=0
    a=0
    b=0
    A="A"
    B="B"
    ab=0
    for i in range(N):
        s=input()
        cnt+=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
            
    if ab>=1:
        cnt+=ab-1
        if a!=b:
            cnt+=1
    ans=cnt+min(a,b)
    print(ans)

main()

Submission status: Wrong Answer

Solution code:


def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    cnt=0
    a=0
    b=0
    A="A"
    B="B"
    ab=0
    for i in range(N):
        s=input()
        cnt+=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
            
    if ab>=1:
        a+=1
        b+=1
        cnt+=ab-1
    ans=cnt+min(a,b)
    print(ans)

main()

Submission status: Wrong Answer

Solution code:


def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    cnt=0
    a=0
    b=0
    A="A"
    B="B"
    ab=0
    for i in range(N):
        s=input()
        cnt+=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
            
    ans=min(a+ab,b+ab)+cnt
    print(ans)

main()

Submission status: Wrong Answer

Solution code:


N=int(input())
L=[] #(lastA, firstB, have AB)
ans=0
lastA=0
firB=0
AB=0
for i in range(N):
    s=input()
    c=s.count("AB")
    #print(c)
    a=s[-1]=="A"
    b=s[0]=="B"
    d=(a==b==1)
    if d:
        a=0
        b=0
    ans+=c
    lastA+=a
    firB+=b
    AB+=d
print(ans)
if lastA and firB:
    ans+=2+AB-1
    lastA-=1
    firB-=1
    ans+=min(lastA, firB)
elif lastA or firB:
    ans+=AB
else:
    ans+=max(AB-1,0)
print(ans, lastA, firB, AB)

Submission status: Wrong Answer

Solution code:


N=int(input())
L=[] #(lastA, firstB, have AB)
ans=0
lastA=0
firB=0
AB=0
for i in range(N):
    s=input()
    c=s.count("AB")
    #print(c)
    a=s[-1]=="A"
    b=s[0]=="B"
    d=(a==b==1)
    if d:
        a=0
        b=0
    ans+=c
    lastA+=a
    firB+=b
    AB+=d
#print(ans)
if abs(lastA-firB)>=AB:
    x=min(abs(lastA-firB), AB)
else:
    x=min(abs(lastA-firB), AB)+AB-1
ans+=min(lastA,firB)+x
print(ans)

Submission status: Wrong Answer

Solution code:


N=int(input())
L=[] #(lastA, firstB, have AB)
ans=0
lastA=0
firB=0
AB=0
for i in range(N):
    s=input()
    c=s.count("AB")
    #print(c)
    a=s[-1]=="A"
    b=s[0]=="B"
    d=(a==b==1)
    if d:
        a=0
        b=0
    ans+=c
    lastA+=a
    firB+=b
    AB+=d
print(ans)
if abs(lastA-firB)>=AB:
    x=min(abs(lastA-firB), AB)
else:
    x=min(abs(lastA-firB), AB)+AB-1
ans+=min(lastA,firB)+x
print(ans, lastA, firB, AB)

Submission status: Wrong Answer

Solution code:
N=int(input())
L=[] #(lastA, firstB, have AB)
ans=0
lastA=0
firB=0
AB=0
for i in range(N):
    s=input()
    c=s.count("AB")
    #print(c)
    a=s[-1]=="A"
    b=s[0]=="B"
    d=(a==b==1)
    if d:
        a=0
        b=0
    ans+=c
    lastA+=a
    firB+=b
    AB+=d
if abs(lastA-firB)>=AB:
    x=min(abs(lastA-firB), AB)
else:
    x=min(abs(lastA-firB), AB)+AB//2
ans+=min(lastA,firB)+x
print(ans)

Submission status: Wrong Answer

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

import sys
from typing import Any, Callable, Deque, Dict, List, Mapping, Optional, Sequence, Set, Tuple, TypeVar, Union
# import time
# import math, cmath
# import numpy as np
# import scipy.sparse.csgraph as cs            # csgraph_from_dense(ndarray, null_value=inf), bellman_ford(G, return_predecessors=True), dijkstra, floyd_warshall
# import random                                # random, uniform, randint, randrange, shuffle, sample
# import string                                # ascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from datetime import date, datetime          # date.today(), date(year,month,day) => date obj; datetime.now(), datetime(year,month,day,hour,second,microsecond) => datetime obj; subtraction => timedelta obj
# from datetime.datetime import strptime       # strptime('2019/01/01 10:05:20', '%Y/%m/%d/ %H:%M:%S') returns datetime obj
# from datetime import timedelta               # td.days, td.seconds, td.microseconds, td.total_seconds(). abs function is also available.
# from copy import copy, deepcopy              # use deepcopy to copy multi-dimentional matrix without reference
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import _heapify_max, _heappop_max, _siftdown_max
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from fractions import Fraction               # Fraction(a, b) => a / b ∈ Q. note: Fraction(0.1) do not returns Fraciton(1, 10). Fraction('0.1') returns Fraction(1, 10)



def main():
    Num = Union[int, float]
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79e+308
    # inf = 2 ** 63 - 1             # (for fast JIT compile in PyPy) 9.22e+18
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def isp():   return input().split()
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    # def _heappush_max(h, item): h.append(item); _siftdown_max(h, 0, len(h)-1)
    
    
    n = ii()
    b_a, b_start, a_end = 0, 0, 0
    ans = 0

    for _ in range(n):
        s = input()
        ans += s.count('AB')
        if s[0] == 'B' and s[-1] == 'A':
            b_a += 1
        elif s[0] == 'B':
            b_start += 1
        elif s[-1] == 'A':
            a_end += 1
    
    m = min(b_start, a_end)
    ans += m
    b_start -= m
    a_end -= m
    if max(b_start, a_end) > 0:
        ans += b_a
    else:
        ans += max(b_a - 1, 0)
    print(ans)




if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
a,b,ba,ans=0,0,0,0
for _ in range(n):
    s=input()
    for i in range(len(s)-1):
        if s[i]=="A" and s[i+1]=="B": ans+=1
    if s[0]=="B": b+=1
    if s[-1]=="A": a+=1
    if s[0]=="B" and s[-1]=="A": ba+=1
if ba==b==a: ans-=1 
print(ans+min(a,b))