Problem:


Score : 400 points

Problem Statement

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

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

Constraints

1 \leq N \leq 10^{4}

2 \leq |s_i| \leq 10

s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

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

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4



Sample input:
3
ABCA
XBAZ
BAD


Sample output:
2

Submission status: Wrong Answer

Solution code:
N = int(input())
S = []
for i in range(N):
    s = str(input())
    S.append(s)
    
count_AB = 0
for s in S:
    count_AB += s.count("AB")
# print(count_AB)

count_BA = 0
count_A = 0
count_B = 0
for s in S:
    if s[0] == 'B' and s[-1] == 'A':
        count_BA += 1
    if s[0] != 'B' and s[-1] == 'A':
        count_A += 1
    if s[0] == 'B' and s[-1] != 'A':
        count_B += 1
# print(count_BA, count_A, count_B)

if count_BA >= 1:
    count_AB += count_BA -1
if count_BA >= 2:
    if count_A >= 1:
        count_A -= 1
        count_AB += 1
    if count_B >= 1:
        count_B -= 1
        count_AB += 1
count_AB += min(count_A, count_B)
print(count_AB)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = []
for i in range(N):
    s = str(input())
    S.append(s)
    
count_AB = 0
for s in S:
    count_AB += s.count("AB")
# print(count_AB)

count_BA = 0
count_A = 0
count_B = 0
for s in S:
    if s[0] == 'B' and s[-1] == 'A':
        count_BA += 1
    if s[0] != 'B' and s[-1] == 'A':
        count_A += 1
    if s[0] == 'B' and s[-1] != 'A':
        count_B += 1
# print(count_BA, count_A, count_B)


if count_BA >= 1:
    count_AB += count_BA -1
if count_A >= 1:
    count_A -= 1
    count_AB += 1
if count_B >= 1:
    count_B -= 1
    count_AB += 1
count_AB += min(count_A, count_B)
print(count_AB)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = []
for i in range(N):
    s = str(input())
    S.append(s)
    
count_AB = 0
for s in S:
    count_AB += s.count("AB")
# print(count_AB)

count_BA = 0
count_A = 0
count_B = 0
for s in S:
    if s[0] == 'B' and s[-1] == 'A':
        count_BA += 1
    if s[0] != 'B' and s[-1] == 'A':
        count_A += 1
    if s[0] == 'B' and s[-1] != 'A':
        count_B += 1
# print(count_BA, count_A, count_B)

count_AB += count_BA -1
if count_A >= 1:
    count_A -= 1
    count_AB += 1
if count_B >= 1:
    count_B -= 1
    count_AB += 1
count_AB += min(count_A, count_B)
print(count_AB)

Submission status: Wrong Answer

Solution code:
def abcheck(word):
    A = 0
    B = 0
    AB = 0
    x = 0
    pre = ""
    for i in word:
        if i=="B":
            if pre == "A":
                AB += 1
        pre = i
    if word[0] == "B":
        B = 1
    if word[-1] == "A":
        A = 1
    if A and B:
        x = 1
    if A and not B:
        x = 2
    if not A and B:
        x = 3
    return A,B,AB,x

N = int(input())
abnumber = 0
A = 0
B = 0
X = [0]*4
for _ in range(N):
    w = str(input())
    a,b,ab,x = abcheck(w)
    abnumber += ab
    A += a
    B += b
    X[x] += 1
if X[1] > 0 or X[2]>0:
    abnumber += min(A,B)
else:
    abnumber += min(A,B)-1
print(abnumber)

Submission status: Wrong Answer

Solution code:
def abcheck(word):
    A = 0
    B = 0
    AB = 0
    pre = ""
    for i in word:
        if i=="B":
            if pre == "A":
                AB += 1
        pre = i
    if word[0] == "B":
        B = 1
    if word[-1] == "A":
        A = 1
    return A,B,AB







N = int(input())
abnumber = 0
A = 0
B = 0
for _ in range(N):
    w = str(input())
    a,b,ab = abcheck(w)
    abnumber += ab
    A += a
    B += b
abnumber += min(A,B)
print(abnumber)

Submission status: Wrong Answer

Solution code:
def abcheck(word):
    A = 0
    B = 0
    AB = 0
    pre = ""
    for i in word:
        if i=="B":
            if pre == "A":
                AB += 1
        pre = i
    if w[0] == "B":
        B = 1
    if w[-1] == "A":
        A = 1
    return A,B,AB







N = int(input())
abnumber = 0
A = 0
B = 0
for _ in range(N):
    w = str(input())
    a,b,ab = abcheck(w)
    abnumber += ab
    A += a
    B += b
abnumber += min(A,B)
print(abnumber)

Submission status: Wrong Answer

Solution code:
n = int(input())
ss = [input() for _ in range(n)]
ans = 0
count_ab = 0 # if ab exists
count_a = 0 # if end in a
count_b = 0 # if start in b
count_ba = 0 # if start in b and end in a
for s in ss:
  slen = len(s)
  if s[0] == 'B' and  s[-1] == 'A':
    count_ba +=1
  elif s[0] == 'B':
    count_b +=1
  elif s[-1] == 'A':
    count_a +=1
  count_ab += s.count("AB")

npair = min(count_a, count_b)
count_a = count_a - npair
count_b = count_b - npair
ans += count_ab + npair

if count_a == count_b:
  pass
elif count_a > 0:
  ans += min(count_a, count_ba)
  count_ba -= min(count_a, count_ba)
else:
  ans += min(count_b, count_ba)
  count_ba -= min(count_b, count_ba)
  
ans+= count_ba

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = 0
b = 0
ab = 0
total = 0
t = []

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

for i in t:
	total += i.count("AB")
	if i[-1] == "A" and i[0] == "B":
		ab += 1
	else:
		a += i[-1] == "A"
		b += i[0] == "B"
print(a,b ,ab)
if ab == 0:
	print(total + min(a,b))
elif a == 0 and b == 0:
	print(total + ab - 1)
elif a > 0 and b > 0:
	print(total + ab + min(a, b))
else:
	print(total + ab)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = 0
b = 0
total = 0
for i in range(n):
	t = input()
	total += t.count("AB")
	a += t[-1] == "A"
	b += t[0] == "B"
if a == n and b == n:
	a -= 1
print(min(a,b) + total)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = 0
b = 0
total = 0
for i in range(n):
	t = input()
	total += t.count("AB")
	a += t[-1] == "A"
	b += t[0] == "B"
print(min(a,b) + total)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


# aとbの最大公約数
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)

# aとbの最小公倍数
def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


def main():
    N = int(input())

    ans = 0
    a, b, c = 0, 0, 0
    for _ in range(N):
        S = input()
        ans += S.count("AB")

        if S[-1] == "A" and S[0] == "B":
            a += 1
        elif S[-1] == "A":
            b += 1
        elif S[0] == "B":
            c += 1

    # A B A B
    if b > 0:
        b -= 1
        ans += a

        a = 0
        if c > 0:
            ans += 1
            c -= 1
    else:
        ans += a - 1
        a = 0
        if c > 0:
            ans += 1
            c -= 1

    # A B
    ans += min(b, c)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys,random
try: sys.stdin = open('in.in','r')
except: pass

ans = 0
allboth = 1
l = r = 0

n = int(input())
for i in range(n):
	s = input().strip()
	ans += s.count('AB')
	if s[0] == 'B' or s[-1] == 'A':
		l += s[0] == 'B'
		r += s[-1] == 'A'
		if s[0] != 'B' or s[-1] != 'A':
			allboth = 0

ans += min(l,r)
if allboth and ans: ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys,random
try: sys.stdin = open('in.in','r')
except: pass

ans = 0
allboth = 1
l = r = 0

n = int(input())
for i in range(n):
	s = input().strip()
	ans += s.count('AB')
	l += s[0] == 'B'
	r += s[-1] == 'A'
	if s[0] == 'B' or s[-1] == 'A':
		allboth &= s[0]=='B' and s[-1] == 'A'

ans += min(l,r)
if allboth and ans: ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys,random
try: sys.stdin = open('in.in','r')
except: pass

ans = 0
allboth = 1
l = r = 0

n = int(input())
for i in range(n):
	s = input().strip()
	ans += s.count('AB')
	l += s[0] == 'B'
	r += s[-1] == 'A'
	allboth &= s[0]=='B' and s[-1] == 'A'

ans += min(l,r)
if allboth: ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s_list = [input() for x in range(n)]

cnt = 0
a_cnt = 0
b_cnt = 0
ab_cnt = 0
for x in s_list:
    cnt = x.count("AB")
    if (x[-1] == "A")&(x[0] != "B"):
        a_cnt += 1
    if (x[0] == "B")&(x[-1] != "A"):
        b_cnt += 1
    if (x[-1] == "A")&(x[0] == "B"):
        ab_cnt += 1
if ab_cnt == 0:
    print(cnt+min(a_cnt,b_cnt))
elif (a_cnt+b_cnt>0)&(ab_cnt != 0):
    print(cnt+ab_cnt+min(a_cnt,b_cnt))
elif (a_cnt+b_cnt==0):
    print(cnt+ab_cnt-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
s_list = [input() for x in range(n)]

cnt = 0
a_cnt = 0
b_cnt = 0
ab_cnt = 0
for x in s_list:
    cnt = x.count("AB")
    if (x[-1] == "A")&(x[0] != "B"):
        a_cnt += 1
    if (x[0] == "B")&(x[-1] != "A"):
        b_cnt += 1
    if (x[-1] == "A")&(x[0] == "B"):
        ab_cnt += 1
if ab_cnt == 0:
    print(cnt+min(a_cnt,b_cnt))
elif a_cnt+b_cnt>0:
    print(cnt+ab_cnt+min(a_cnt,b_cnt))
else:
    print(cnt+ab_cnt-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
s_list = [input() for x in range(n)]

cnt = 0
a_cnt = 0
b_cnt = 0
ab_cnt = 0
for x in s_list:
    cnt += x.count("AB")
    if x[-1] == "A":
        a_cnt += 1
    if x[0] == "B":
        b_cnt += 1
    if (x[-1] == "A")&(x[0] == "B"):
        ab_cnt += 1
if ab_cnt==a_cnt==b_cnt:
    print(cnt+min(a_cnt,b_cnt)-1)
else:
    print(cnt+min(a_cnt,b_cnt))

Submission status: Wrong Answer

Solution code:
n = int(input())
s_list = [input() for x in range(n)]

cnt = 0
a_cnt = 0
b_cnt = 0
ab_cnt = 0
for x in s_list:
    cnt += x.count("AB")
    if x[-1] == "A":
        a_cnt += 1
    if x[0] == "B":
        b_cnt += 1
    if (x[-1] == "A")&(x[0] == "B"):
        ab_cnt += 1
if ab_cnt==0:
    print(cnt+min(a_cnt,b_cnt))
else:
    print(cnt+min(a_cnt,b_cnt,(ab_cnt-1)))

Submission status: Wrong Answer

Solution code:
n = int(input())
s_list = [input() for x in range(n)]

cnt = 0
a_cnt = 0
b_cnt = 0
ab_cnt = 0
for x in s_list:
    cnt += x.count("AB")
    if x[-1] == "A":
        a_cnt += 1
    if x[0] == "B":
        b_cnt += 1
    if (x[-1] == "A")&(x[0] == "B"):
        ab_cnt += 1
print(cnt+min(a_cnt,b_cnt)-(ab_cnt-1))

Submission status: Wrong Answer

Solution code:
n = int(input())
s_list = [input() for x in range(n)]

cnt = 0
a_cnt = 0
b_cnt = 0
for x in s_list:
    cnt += x.count("AB")
    if x[-1] == "A":
        a_cnt += 1
    if x[0] == "B":
        b_cnt += 1
print(cnt+min(a_cnt,b_cnt))

Submission status: Wrong Answer

Solution code:
n = int(input())
s_list = [input() for x in range(n)]

cnt = 0
a_cnt = 0
b_cnt = 0
for x in s_list:
    if x.find("AB") > -1:
        cnt += 1
    if x[-1] == "A":
        a_cnt += 1
    if x[0] == "B":
        b_cnt += 1
print(cnt+min(a_cnt,b_cnt))

Submission status: Wrong Answer

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

cnta = 0
cntb = 0
isok = False
cnt = 0


for i in range(n):
    if s[i][0] == 'B' and s[i][-1] =='A':
        cnta+=1
        cntb+=1
    elif s[i][0] =='B':
        cntb+=1
        isok = True
    elif s[i][-1] =='A':
        cnta+=1
        isok=True
    for j in range(len(s[i])-1):
        if s[i][j] =='A' and s[i][j+1] == 'B':
            cnt += 1


if isok:
    print(min(cnta,cntb)+cnt)
else:
    print(min(cnta,cntb)+cnt-1)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
import itertools

N = int(input())
li = []
lasta = 0
firstb = 0
ab = 0
cnt = 0

# 各入力のABの数を求める
# HeadにBもしくはTailにAを持つリストをカウント
for i in range(N):
    s = input()
    cnt += s.count('AB')
    li = list(s)
    if li[-1] == 'A' and li[0] != 'B':
        lasta += 1
    if li[0] == 'B' and li[-1] != 'A':
        firstb += 1
    if li[-1] == 'A' and li[0] == 'B':
        lasta += 1
        firstb += 1
        ab += 1

if ab % 2 == 0:
    ab = int(ab / 2)
elif ab % 2 == 1:
    ab = int(ab / 2) + 1

# print('lasta : %d firstb : %d ab : %d cnt : %d' % (lasta, firstb, ab, cnt))
if lasta == firstb:
    cnt += lasta - ab
else:
    cnt += max(lasta, firstb) - ab

print(cnt)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
import itertools

N = int(input())
li = []
lasta = 0
firstb = 0
ab = 0
cnt = 0

# 各入力のABの数を求める
# HeadにBもしくはTailにAを持つリストをカウント
for i in range(N):
    s = input()
    cnt += s.count('AB')
    li = list(s)
    if li[-1] == 'A' and li[0] != 'B':
        lasta += 1
    if li[0] == 'B' and li[-1] != 'A':
        firstb += 1
    if li[-1] == 'A' and li[0] == 'B':
        lasta += 1
        firstb += 1
        ab += 1

if ab % 2 == 0:
    ab = int(ab / 2)
elif ab % 2 == 1:
    ab = int(ab / 2) + 1

# print('lasta : %d firstb : %d ab : %d cnt : %d' % (lasta, firstb, ab, cnt))
if lasta == firstb:
    cnt += lasta - ab
else:
    cnt += min(lasta, firstb)

print(cnt)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
import itertools

N = int(input())
li = []
lasta = 0
firstb = 0
ab = 0
cnt = 0

# 各入力のABの数を求める
# HeadにBもしくはTailにAを持つリストをカウント
for i in range(N):
    s = input()
    cnt += s.count('AB')
    li = list(s)
    if li[-1] == 'A' and li[0] != 'B':
        lasta += 1
    if li[0] == 'B' and li[-1] != 'A':
        firstb += 1
    if li[-1] == 'A' and li[0] == 'B':
        lasta += 1
        firstb += 1
        ab += 1

if ab % 2 == 0:
    ab = int(ab / 2)
elif ab % 2 == 1:
    pass

# print('lasta : %d firstb : %d ab : %d cnt : %d' % (lasta, firstb, ab, cnt))
if lasta == firstb:
    cnt += lasta - ab
else:
    cnt += min(lasta, firstb)

print(cnt)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
import itertools

N = int(input())
li = []
lasta = 0
firstb = 0
ab = 0
cnt = 0

# 各入力のABの数を求める
# HeadにBもしくはTailにAを持つリストをカウント
for i in range(N):
    s = input()
    cnt += s.count('AB')
    li = list(s)
    if li[len(li)-1] == 'A' and li[0] != 'B':
        lasta += 1
    if li[0] == 'B' and li[len(li)-1] != 'A':
        firstb += 1
    if li[len(li)-1] == 'A' and li[0] == 'B':
        lasta += 1
        firstb += 1
        ab += 1

if ab % 2 == 0:
    ab = int(ab / 2)
elif ab % 2 == 1:
    pass

# print('lasta : %d firstb : %d ab : %d cnt : %d' % (lasta, firstb, ab, cnt))
if lasta == firstb:
    cnt += lasta - ab
else:
    cnt += max(lasta, firstb) - ab

print(cnt)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
import itertools

N = int(input())
li = []
lasta = 0
firstb = 0
ab = 0
cnt = 0

# 各入力のABの数を求める
for i in range(N):
    s = input()
    cnt += s.count('AB')
    li = list(s)
    if li[len(li)-1] == 'A' and li[0] != 'B':
        lasta += 1
    if li[0] == 'B' and li[len(li)-1] != 'A':
        firstb += 1
    if li[len(li)-1] == 'A' and li[0] == 'B':
        lasta += 1
        firstb += 1
        ab += 1


if lasta == firstb:
    cnt += lasta - int(ab / 2)
else:
    cnt += min(lasta, firstb)

print(cnt)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
import itertools

N = int(input())
li = []
lasta = 0
firstb = 0
ab = 0
cnt = 0

# 各入力のABの数を求める
for i in range(N):
    s = input()
    cnt += s.count('AB')
    li = list(s)
    if li[len(li)-1] == 'A' and li[0] != 'B':
        lasta += 1
    if li[0] == 'B' and li[len(li)-1] != 'A':
        firstb += 1
    if li[len(li)-1] == 'A' and li[0] == 'B':
        lasta += 1
        firstb += 1
        ab += 1


if lasta == firstb:
    cnt += lasta - ab
else:
    cnt += min(lasta, firstb)

print(cnt)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
import itertools

N = int(input())
li = []
lasta = 0
firstb = 0
cnt = 0

# 各入力のABの数を求める
for i in range(N):
    s = input()
    cnt += s.count('AB')
    li = list(s)
    if li[len(li)-1] == 'A' and li[0] != 'B':
        lasta += 1
    if li[0] == 'B' and li[len(li)-1] != 'A':
        firstb += 1
    if li[len(li)-1] == 'A' and li[0] == 'B':
        lasta += 1
        firstb += 1


if lasta == firstb:
    cnt += lasta - 1
else:
    cnt += min(lasta, firstb)

print(cnt)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
import itertools

N = int(input())
li = []
lasta = 0
firstb = 0
cnt = 0

# 各入力のABの数を求める
for i in range(N):
    s = input()
    cnt += s.count('AB')
    li = list(s)
    if li[len(li)-1] == 'A':
        lasta += 1
    if li[0] == 'B':
        firstb += 1

if lasta == firstb:
    cnt += lasta
else:
    cnt += min(lasta, firstb)

print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[input() for i in range(n)]
ans=0

count_ab=0
count_a=0
count_b=0
for i in a:
    ans+=i.count('AB')
    if i[0]=='B' and i[-1]=='A':
        count_ab+=1
    elif i[0]=='B':
        count_b+=1
    elif i[-1]=='A':
        count_a+=1

ans+=min(count_a,count_b)

z=max(count_a,count_b)-min(count_a,count_b)
ans+=min(z,count_ab)
print(ans)

Submission status: Wrong Answer

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

N = int(input())
L = [list(input().strip()) for _ in range(N)]

c = 0
a = 0
b = 0
for i in range(N):
    v = L[i]
    
    for i in range(len(v) - 1):
        if v[i] == "A" and v[i + 1] == "B":
            c += 1
    
    if v[0] == "B":
        b += 1
    if v[-1] == "A":
        a += 1
        
print(c + min(a, b))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
C - AB Substrings
https://atcoder.jp/contests/diverta2019/tasks/diverta2019_c

"""
import sys


def solve(N, words):
    eA, sB, both = 0, 0, 0
    ans = 0
    for w in words:
        ans += w.count('AB')
        if w[-1] == 'A':
            if w[0] == 'B':
                both += 1
            else:
                eA += 1
        elif w[0] == 'B':
            sB += 1

    ans += min(eA, sB)
    diff = abs(eA - sB)
    if diff:
        ans += both
    elif both > 0:
        ans += (both - 1)
    return ans


def main(args):
    N = int(input())
    words = [input() for _ in range(N)]
    ans = solve(N, words)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
C - AB Substrings
https://atcoder.jp/contests/diverta2019/tasks/diverta2019_c

"""
import sys


def solve(N, words):
    eA = []
    sB = []
    both = []
    ans = 0
    for w in words:
        ans += w.count('AB')
        if w[-1] == 'A':
            if w[0] == 'B':
                both.append(w)
            else:
                eA.append(w)
        elif w[0] == 'B':
            sB.append(w)

    ans += min(len(eA), len(sB))
    diff = abs(len(eA)-len(sB))
    if diff:
        ans += len(both)
    elif len(both) > 1:
        ans += len(both) - 1

    return ans


def main(args):
    N = int(input())
    words = [input() for _ in range(N)]
    ans = solve(N, words)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n=int(input())
mozi=[]
for i in range(n):
    mozi.append(input())
count=0
a=0
b=0
c=0
for i in mozi:
    if "AB" in i:
        count=count+1
for i in mozi:
    if i[0]=="B" and i[-1]=="A":
        a=a+1
    if i[0]!="B" and i[-1]=="A":
        b=b+1
    if i[0]=="B" and i[-1]!="A":
        c=c+1
if b!=0 and c!=0:
    count=count+int(min([b,c]))+a
elif b==0 and c!=0:
    count=count+a
elif b!=0 and c==0:
    count=count+a
elif b==0 and c==0 and a!=0:
    count=count+a-1    
print(count)

Submission status: Wrong Answer

Solution code:
import re

n = int(input().rstrip())
 
ab_count = 0
a_count = 0
b_count = 0
  
for i in range(n):
  word = input().rstrip()
  ab_count += len(re.findall("AB", word))
  if word[-1] == "A":
    a_count += 1
  if word[0] == "B":
    b_count += 1

print(ab_count + min(a_count, b_count))

Submission status: Wrong Answer

Solution code:
n = int(input().rstrip())

ab_count = 0
a_count = 0
b_count = 0

for i in range(n):
  word = input().rstrip()
  if "AB" in word:
    ab_count += 1
  if word[-1] == "A":
    a_count += 1
  if word[0] == "B":
    b_count += 1

print(ab_count + min(a_count, b_count))

Submission status: Wrong Answer

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

print(count)

Submission status: Wrong Answer

Solution code:
import re
N = int(input())
S = []
a, b, ab,c = 0, 0, 0, 0
for i in range(N):
	S.append(input())
	c += len(re.findall("AB", S[i]))
	if S[i][-1] == "A" and S[i][0] == "B":
		ab += 1
	if S[i][-1] == "A":
		a += 1
	if S[i][0] == "B":
		b += 1
if ab == a == b:
	seq = ab -1
else:
	seq = min (a,b)

print(seq + c)

Submission status: Wrong Answer

Solution code:
import re
N = int(input())
S = []
a, b, ab,c = 0, 0, 0, 0
for i in range(N):
	S.append(input())
	c += len(re.findall("AB", S[i]))
	if S[i][-1] == "A" and S[i][0] == "B":
		ab += 1
	if S[i][-1] == "A":
		a += 1
	if S[i][0] == "B":
		b += 1

if ab == a:
	seq = ab -1
else:
	seq = min (a,b)

print(seq + c)

Submission status: Wrong Answer

Solution code:
import re
N = int(input())
S = []
a, b, c = 0, 0, 0
for i in range(N):
	S.append(input())
	c += len(re.findall("AB", S[i]))
	if S[i][-1] == "A":
		a += 1
	if S[i][0] == "B":
		b += 1
seq = 0
if a == b and a > 0:
	seq = a-1
elif a != b:
	seq = min(a,b) 

print(seq + c)

Submission status: Wrong Answer

Solution code:
import re
N = int(input())
S = []
a, b, c = 0, 0, 0
for i in range(N):
	S.append(input())
	c += len(re.findall("AB", S[i]))
	if S[i][-1] == "A":
		a += 1
	if S[i][0] == "B":
		b += 1

print(min(a,b) + c)

Submission status: Wrong Answer

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

if count!=0:
    count+=min(a,b)
elif a+b==0:
    count-=1
print(count)

Submission status: Wrong Answer

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

count+=min(a,b)
if a==n and b==n:
    count-=1
print(count)

Submission status: Wrong Answer

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


count+=min(a,b)
if a==n and b==n:
    count-=1
print(count)

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=0
b=0
count=0
for i in range(n):
    line=input()
    if line in 'AB':
        count+=1
    elif line[0]=='B':
        b+=1
    elif line[-1]=='A':
        a+=1


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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=0
b=0
count=0
for i in range(n):
    line=input()
    if line in 'AB':
        count+=1
    if line[0]=='B':
        b+=1
    if line[-1]=='A':
        a+=1


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

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
c1 = 0
c2 = 0
c3 = 0
ans = 0
for _ in range(N):
    s = input()
    ans += s.count("AB")
    if (s[0]=="B") & (s[1]=="A"):
        c1 += 1
    elif s[0] == "B":
        c2 += 1
    elif s[-1]=="A":
        c3 += 1

if c1== 0:
    print(ans + min(c2, c3))
elif (c1 > 0) & (max(c2, c3) == 0):
    print(ans + c1 - 1)
elif (c1 > 0) & (max(c2, c3) > 0):
    print(ans + c1 + min(c2, c3))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [list(input()) for i in range(n)]
cnt = 0
cnt_A=0
cnt_B=0
cnt_C=0
for i in range(n):
  if "AB" in a[i]:
    cnt+= 1
for i in range(n):
  if a[i][-1] == "A" and a[i][0] =="B":
    cnt_C+=1  
  elif a[i][-1] == "A" and a[i][0] !="B":
    cnt_A +=1
  elif a[i][0] =="B"and a[i][-1] !="A":
    cnt_B+=1

print(cnt+min(cnt_A+cnt_C,cnt_B+cnt_C))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [list(input()) for i in range(n)]
cnt = 0
cnt_A=0
cnt_B=0
cnt_C=0
for i in range(n):
  if "AB" in a[i]:
    cnt+= 1
for i in range(n):
  if a[i][-1] == "A" and a[i][0] =="B":
    cnt_C+=1  
  elif a[i][-1] == "A" and a[i][0] !="B":
    cnt_A +=1
  elif a[i][0] =="B"and a[i][-1] !="A":
    cnt_B+=1

print(cnt+cnt_A+cnt_B+cnt_C)

Submission status: Wrong Answer

Solution code:
def cntAB(s):
  sl = list(s)
  cnt = 0
  for i, c in enumerate(sl):
    try:
      if c == 'A' and sl[i+1] == 'B':
        cnt += 1
    except:
      pass

  return cnt

N = int(input())
cnt = 0
xA = 0
Bx = 0
BA = 0
for i in range(N):
  s = input()
  cnt += cntAB(s)
  if s[0] == 'B' and s[-1] == 'A':
    BA += 1
  if s[0] == 'B':
    Bx += 1
  if s[-1] == 'A':
    xA += 1

if BA == Bx and BA == xA:
  cnt -= 1

print(cnt + min(Bx, xA))

Submission status: Wrong Answer

Solution code:
def cntAB(s):
  sl = list(s)
  cnt = 0
  for i, c in enumerate(sl):
    try:
      if c == 'A' and sl[i+1] == 'B':
        cnt += 1
    except:
      pass

  return cnt

N = int(input())
cnt = 0
xA = 0
Bx = 0
BA = 0
for i in range(N):
  s = input()
  cnt += cntAB(s)
  if s[0] == 'B' and s[-1] == 'A':
    BA += 1
  elif s[0] == 'B':
    Bx += 1
  elif s[-1] == 'A':
    xA += 1

if xA + Bx == 0:
  cnt -= 1
print(cnt + BA + min(Bx, xA))

Submission status: Wrong Answer

Solution code:
def cntAB(s):
  sl = list(s)
  cnt = 0
  for i, c in enumerate(sl):
    try:
      if c == 'A' and sl[i+1] == 'B':
        cnt += 1
    except:
      pass

  return cnt

N = int(input())
cnt = 0
xA = 0
Bx = 0
BA = 0
for i in range(N):
  s = input()
  cnt += cntAB(s)
  if s[0] == 'B' and s[-1] == 'A':
    BA += 1
  elif s[0] == 'B':
    Bx += 1
  elif s[-1] == 'A':
    xA += 1

if xA + Bx == 0:
  cnt -= 1
print(cnt + min(Bx, xA))

Submission status: Wrong Answer

Solution code:
def cntAB(s):
  sl = list(s)
  cnt = 0
  for i, c in enumerate(sl):
    try:
      if c == 'A' and sl[i+1] == 'B':
        cnt += 1
    except:
      pass
  
  return cnt

N = int(input())
cnt = 0
xA = 0
Bx = 0
for i in range(N):
  s = input()
  cnt += cntAB(s)
  if s[0] == 'B':
    Bx += 1
  if s[-1] == 'A':
    xA += 1

if Bx == N and xA == N:
  cnt -= 1


print(cnt + min(Bx, xA))

Submission status: Wrong Answer

Solution code:
def cntAB(s):
  sl = list(s)
  cnt = 0
  for i, c in enumerate(sl):
    try:
      if c == 'A' and sl[i+1] == 'B':
        cnt += 1
    except:
      pass
  
  return cnt

N = int(input())
cnt = 0
xA = 0
Bx = 0
for i in range(N):
  s = input()
  cnt += cntAB(s)
  if s[0] == 'B':
    Bx += 1
  if s[-1] == 'A':
    xA += 1

print(cnt + min(Bx, xA))

Submission status: Wrong Answer

Solution code:
N = int(input())

ends_with_A = 0
starts_with_B = 0
contains_AB = 0

for i in range(N):
  line = input()
  if line[0] == 'B':
    starts_with_B += 1
  if line[-1] == 'A':
    ends_with_A += 1
  contains_AB += line.count('AB')

print(contains_AB + min(ends_with_A, starts_with_B))

Submission status: Wrong Answer

Solution code:
class Solver:
    def __init__(self, num):
        self.n = num
        self.ans = 0

        # Case 0: 文字列中にABが含まれている
        self.case_0 = 0

        # Case 1: 文字列が"B***A"
        self.case_1 = 0

        # Case 2: 文字列が"B***-"
        self.case_2 = 0

        # Case 3: 文字列が"-***A"
        self.case_3 = 0

        # Case 4: 文字列が"-***-"
        # そもそもカウントする必要がない
        # self.case_4 = 0

        self.solve()
    
    def solve(self):
        for i in range(self.n):
            sen = input()
            if sen.find("AB") != -1:
                self.case_0 = self.case_0 + 1
            if sen[0] == "B" and sen[-1] == "A":
                self.case_1 = self.case_1 + 1
            elif sen[0] == "B" and sen[-1] != "A":
                self.case_2 = self.case_2 + 1
            elif sen[0] != "B" and sen[-1] == "A":
                self.case_3 = self.case_3 + 1
    

        if self.case_1 == 0:
            self.ans = self.ans + min(self.case_3, self.case_2)
        else:
            if self.case_3 + self.case_2 > 0:
                self.ans = self.ans + self.case_1 + min(self.case_3, self.case_2)
            elif self.case_3 + self.case_2 == 0:
                self.ans = self.ans + self.case_1 - 1

        self.ans = self.ans + self.case_0

        print(str(self.ans))

def main():
    n=int(input())
    Solver(n)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
class Solver:
    def __init__(self, num):
        self.n = num
        self.ans = 0

        # Case 0: 文字列中にABが含まれている
        self.case_0 = 0

        # Case 1: 文字列が"B***A"
        self.case_1 = 0

        # Case 2: 文字列が"B***-"
        self.case_2 = 0

        # Case 3: 文字列が"-***A"
        self.case_3 = 0

        # Case 4: 文字列が"-***-"
        # そもそもカウントする必要がない
        # self.case_4 = 0

        self.solve()
    
    def solve(self):
        for i in range(self.n):
            sen = input()
            if sen.find("AB") != -1:
                self.case_0 = self.case_0 + 1
            if sen[0] == "B" and sen[-1] == "A":
                self.case_1 = self.case_1 + 1
            elif sen[0] == "B":
                self.case_2 = self.case_2 + 1
            elif sen[-1] == "A":
                self.case_3 = self.case_3 + 1
    

        if self.case_1 == 0:
            self.ans = self.ans + min(self.case_3, self.case_2)
        else:
            if self.case_3 + self.case_2 > 0:
                self.ans = self.ans + self.case_1 + min(self.case_3, self.case_2)
            elif self.case_3 + self.case_2 == 0:
                self.ans = self.ans + self.case_1 - 1

        self.ans = self.ans + self.case_0

        print(str(self.ans))

def main():
    n=int(input())
    Solver(n)



if __name__ == "__main__":
    main()