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:
def ab_count(s):
    n = len(s)
    cnt = 0
    for i in range(n-1):
        if s[i]=="A" and s[i+1]=="B": cnt += 1
    return cnt

n = int(input())
a = 0 ; b = 0 ; ans = 0
for i in range(n):
    s = input()
    ans += ab_count(s)
    if s.startswith("B"): b += 1
    if s.endswith("A"): a += 1

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

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
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 :
      back -= 1
  is_a = 0
  is_b = 0
  f_b = 0
if front >= back :
  is_ab += back
elif back > front :
  is_ab += front
print(is_ab)

Submission status: Wrong Answer

Solution code:
def check(S):
    ans = 0
    for i in range(1,len(S)):
        if S[i-1] == 'A' and S[i] == 'B':
            ans += 1
    return ans

N = int(input())
s = []
a = 0
b = 0
ab = 0

ans = 0

for i in range(N):
    S = input()
    ans += check(S)
    if S[0] == 'B' or S[-1] == 'A':
        s.append(S)
        if S[0] == 'B' and S[-1] != 'A':
            b += 1
        elif S[0] != 'B' and S[-1] == 'A':
            a += 1
        else:
            ab += 1

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

abc = abs(a - b)

ans += min(abc, ab)


print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
lis = [input() for _ in range(n)]
cnt = sum([l.count('AB') for l in lis])
ab_cnt = sum([l[-1] == 'A' and l[0] == 'B' for l in lis])
a_cnt = sum([l[-1] == 'A' for l in lis]) - ab_cnt
b_cnt = sum([l[0] == 'B' for l in lis]) - ab_cnt
if ab_cnt:
  cnt += ab_cnt - 1
  a_cnt += 1
  b_cnt += 1
print(cnt + min(a_cnt, b_cnt) - (ab_cnt == n))

Submission status: Wrong Answer

Solution code:
# coding:utf-8


def main():
    N = int(input())
    s_list = []
    for i in range(N):
        s_list.append(input().rstrip())

    ans = 0
    a_last = 0
    ab_count = 0
    b_start = 0
    for si in s_list:
        if si[0] == "B" and si[-1] == "A":
            ab_count += 1
        elif si[0] == "B":
            b_start += 1
        elif si[-1] == "A":
            a_last += 1

        for i in range(len(si)-1):
            if si[i] == "A" and si[i+1] == "B":
                ans += 1

    if ab_count > 0:
        ans += ab_count - 1
        ans = ans + min(a_last, b_start) + 1
    else:
        ans = ans + min(a_last, b_start)
    # print(ab_count, a_last, b_start)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#問題C
N = int(input())
s = []

naka=0
a_kazu=0
b_kazu=0
b_a_kazu=0
for i in range(N):
    a = str(input())
    if "AB" in a:
        naka+=1
    if a[0]=="B" and a[-1]=="A":
        b_a_kazu+=1
    elif a[0]=="B":
        b_kazu+=1
    elif a[-1]=="A":
        a_kazu+=1
if b_a_kazu>0:
    a_kazu+=1
    b_kazu+=1
    b_a_kazu-=1
print(naka+b_a_kazu+min(a_kazu, b_kazu))

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

Submission status: Wrong Answer

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

ans = 0

# 各文字列でABの出現数
for s in strs:
    ans += s.count('AB')

# b ではじまり a で終わるもの
starts_and_ends = set(s for s in strs if s.startswith('B') and s.endswith('A'))

# b ではじまるもの
starts_with_b = set(s for s in strs if s.startswith('B')) - starts_and_ends

# a で終わるもの
end_with_a = set(s for s in strs if s.endswith('A')) - starts_and_ends

# if len(starts_with_b) == 0 or len(end_with_a) == 0:
#     print(ans)
# else:

# 先頭を一個切り出す。
count = [len(end_with_a), len(starts_with_b), len(starts_and_ends)]

# if len(end_with_a - starts_and_ends) > 0:
#     count[0] -= 1
#     count[1] -= 1
# else:
#     count[0] -= 1
#     count[1] -= 1
#     count[2] -= 1

if count[0] == 0 and count[2] > 0:
    count[0] = 1
    count[2] -= 1

for i in range(count[2]):
    count[0] += 1
    count[1] += 1

print(ans + min(count[0], count[1]))

Submission status: Wrong Answer

Solution code:
N = int(input())

cnt = 0
cnt_BA = 0
cnt_A = 0
cnt_B = 0

for i in range(N) : 
    tmp = (input())
    cnt += tmp.count("AB")
#    tmp = tmp[0] +tmp[-1]
#    if tmp == "BA" : 
#        cnt_BA += 1
    if tmp[-1] == "A" : 
        cnt_A += 1
    if tmp[0] == "B" : 
        cnt_B += 1
    
m = min(cnt_A, cnt_B)

if m != N : 
    print(m+cnt)
else : 
    print(m-1+cnt)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
s = [ns() for _ in range(n)]

ans = 0

x = y = z = 0

for si in s:
    # 内部のABカウント
    ans += si.count('AB')

    # 右端A かつ 左端B ... Z
    if si[:1] == 'B' and si[-1:] == 'A':
        z += 1

    # 右端A ... X
    elif si[-1:] == 'A':
        x += 1

    # 左端B ... Y
    elif si[:1] == 'B':
        y += 1

if z <= x and z <= y:
    ans += 2*z
    ans += min(x,y) - z

else:
    ans += min(z, x)
    ans += min(z, y)

    if z >= x and z >= y:
        if z - max(x,y) <= max(x,y) - min(x,y):
            ans += z - max(x,y)
        else:
            ans += max(x,y) - min(x,y)
            ans += 2 * ((z - max(x,y) - (max(x,y) - min(x,y))) // 3)
            if (z - max(x,y) - (max(x,y) - min(x,y))) % 3 == 2:
                ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
n_ab = 0
n_b = 0
n_a = 0
for i in range(N):
    S = input()
    # 前後情報とABの数だけ調べておく
    # for i in range(len(S)-1):
    #     if S[i]+S[i+1] == "AB":
    #         n_ab += 1
    n_ab += S.count("AB")
    if S[-1] == "A":
        n_a += 1
    if S[0] == "B":
        n_b += 1
# print(Ss)
print(int(n_ab+min(n_b,n_a)))

Submission status: Wrong Answer

Solution code:
N = int(input())
S_tail_a_head_b = []
S_tail_a = []
S_head_b = []
S_ab = []
for _ in range(N):
    s = input()
    if s.find('AB') == -1 and s[0] != 'B' and s[-1] != 'A':
        continue
    elif s[0] == 'B' and s[-1] == 'A':
        S_tail_a_head_b.append(s)
    elif s[0] == 'B':
        S_head_b.append(s)
    elif s[-1] == 'A':
        S_tail_a.append(s)
    else:
        S_ab.append(s)

S = ''.join(S_ab)
tail_a = ''
head_b = ''
for i in range(max(len(S_head_b), len(S_tail_a))):
    if len(S_head_b) > i and len(S_tail_a) > i:
        S += S_tail_a[i] + S_head_b[i]
    elif len(S_head_b) <= i:
        tail_a = ''.join(S_tail_a[-1])
        break
    else:
        head_b = ''.join(S_head_b[-1])
        break

S += tail_a + ''.join(S_tail_a_head_b) + head_b

print(S.count('AB'))

Submission status: Wrong Answer

Solution code:
n = int(input())
hoge = []
for i in range(0,n):
	a = input()
	l = [a[i:i+1] for i in range(len(a))]
	hoge.append(l)

lenlst = []
for x in hoge:
    lenlst.append(len(x))

cnt = 0
cnt_A = 0
cnt_B = 0

for i in range(len(hoge)):
	if hoge[i][0] == 'B':
		cnt_B += 1
	if hoge[i][-1] == 'A':
		cnt_A += 1
	for j in range(lenlst[i]-1):
		if hoge[i][j] == 'A' and hoge[i][j+1] == 'B':
			cnt += 1

print(cnt + min(cnt_A,cnt_B))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def solve():
    N = int(input())
    ans = 0
    endA = 0
    startB = 0
    ryouhou = 0
    # Aで終わるs1と、Bで始まるs2を連結させれば1個増える
    for i in range(N):
        s = input()
        if s[-1]=="A" and s[0]=="B":
            ryouhou += 1
        elif s[-1]=="A":
            endA += 1
        elif s[0]=="B":
            startB += 1
        
        ans += s.count("AB")
    

    if endA>=1 and startB>=1 and ryouhou>=1:
        ans += min(endA-1, startB-1)
        ans += ryouhou + 1
        print(ans)
        return
    elif ((endA>=1 and startB==0 and ryouhou>=1) or
          (endA==0 and startB>=1 and ryouhou>=1)):
        ans += ryouhou
        print(ans)
        return
    else:
        ans += min(endA, startB)
        rest = abs(max(endA, startB) - min(endA, startB))
        
        ans += min(rest, ryouhou)
        rest = ryouhou - rest
        if rest <= 0:
            print(ans)
            return
        else:
            ans += rest//2
            print(ans)
            return

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [str(input()) for i in range(N)]
cnt = 0
yobiflag = 0
bflag = 0
aflag = 0
for i in range(N):
    flag=False
    if a[i][0] == "B":
        if a[i][-1] == "A":
            yobiflag += 1
            bflag += 1
            aflag += 1
        else:
            bflag += 1
    else:
        if a[i][-1] == "A":
            aflag += 1

    for j in range(len(a[i])):
        # print(a[i][j])
        if a[i][j] == "B" and flag:
            cnt += 1
        flag = False

        if a[i][j] == "A":
            flag = True

if yobiflag == N:
    aflag -= 1
# print(cnt)
# print(aflag)
# print(bflag)
print(cnt + min(aflag, bflag, ))

Submission status: Wrong Answer

Solution code:
n=int(input())
count=0
s=[]
a=0
b=0
ba=0
for i in range(n):
    s1=input()
    count+=s1.count('AB')
    if s1[0]=='B' and s1[-1]=='A':
        ba+=1
    elif s1[0]=='B':
        b+=1
    elif s1[-1]=='A':
        a+=1
if ba==0:
    if a>0 and b>0:
        count+=1
else:
    if a>=1 and b>=1:
        count+=ba+min(a,b)
    elif a>0 or b>0:
        count+=ba
print(count)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def AB_check(text):
    cnt = 0
    for i in range(len(text)-1):
        if text[i:i+2] == "AB":
            cnt+=1
    return cnt
N = int(input())
s = []
for _ in range(N):
    s.append(input())
AB_direct = 0
B_head = 0
A_tail = 0
dupl = 0
for text in s:
    if "AB" in text:
        AB_direct+=AB_check(text)
    if text[0] == "B":
        B_head += 1
    if text[-1] == "A":
        A_tail += 1
    if text[0] == "B" and text[-1] == "A":
        dupl += 1
print(AB_direct + min(B_head,A_tail)-dupl//2)

Submission status: Wrong Answer

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

ans = 0
A = 0
B = 0
BA = 0
for i in s:
    ans += i.count("AB")
    if i[0:1] != "B" and i[-1:] == "A":
        A += 1
    elif i[0:1] == "B" and i[-1:] != "A":
        B += 1
    elif i[0:1] == "B" and i[-1:] == "A":
        BA += 1

#print(ans)
#print(A,B,BA)

while 1:
    if BA == 0 or A == B:
        break
    if A >= B:
        B += 1
    else:
        A += 1
    BA -= 1
#print(A,B,BA)
if A == 0 and B == 0:
    BA -= 1
print(ans + min(A,B) + BA)

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 b_a <= b:
    ans += a + b_a
else:
    ans += a * 2 + b_a - 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
h = []
count = 0
countA = 0
countB = 0
countC = 0
for i in range(n):
    a = input()
    if a[len(a)-1] == "A":
        countA += 1
        
    if a[0] == "B":
        countB += 1
        
    if a[len(a)-1] == "A" and a[0] == "B":
        countC += 1
 
    for j in range(len(a) - 1):
        if a[j] == "A" and a[j+1] =="B":
            count += 1
    
x = min(countA,countB)

if countA == countB == countC:
    print(count + x - 1)
else:
    print(count + x)

Submission status: Wrong Answer

Solution code:
import sys
# import bisect
# import math
# import itertools
# import numpy as np

# import collections

"""Template"""


class IP:
    """
    入力を取得するクラス
    """

    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        """
        Some String LineS
        :param n: 行数
        :return: list
        """
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        # 参考
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        # https://atcoder.jp/contests/abc105/submissions/4088632
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x // y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)

    def gcd(self, x, y):
        if y == 0:
            return x
        else:
            return self.gcd(y, x % y)


"""ここからメインコード"""


def main():
    # 1文字に省略
    r, e, p = range, enumerate, print
    ip = IP()
    id = Idea()
    mod = 10 ** 9 + 7

    """この下から書いてね"""
    n = ip.I()
    ans = 0
    a = 0
    b = 0
    c = 0
    for i in range(n):
        o = input()
        res = o.count("AB")
        if res == -1:
            pass
        else:
            ans += res
        if o[-1] == 'A':
            a += 1
        if o[0] == 'B':
            b += 1
        if o[-1] == 'A' and o[0] == 'B':
            c += 1
    if min(a, b) == n:
        ans += min(a, b) - 1
    elif min(a, b) == c:
        ans += min(a, b) - 1
    else:
        ans += min(a, b)
    p(ans)


main()

Submission status: Wrong Answer

Solution code:
N = int(input())

S = []
for i in range (N):
    S.append(input())

ab_count = 0
bottom_a = 0
top_b = 0
for s in S:
    if s[-1] == 'A':
        bottom_a += 1
    if s[0] == 'B':
        top_b += 1 
    for i in range(1,len(s)):
        if s[i-1] == 'A' and s[i] == 'B':
            ab_count += 1

print(ab_count +  min(bottom_a, top_b))

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
a =[0 for i in range(n)]
b = [0 for i in range(n)]
c = [0 for i in range(n)]
for i in range(n):
    s = input()
    q = len(s)
    for j in range(q):
        if j == 0 and s[j] == 'B':
            b[i] = 1
        elif j == q-1 and s[j] == 'A':
            a[i] = 1
        if j > 0:
            if s[j] == 'B' and s[j-1] == 'A':
                ans += 1
    if b[i] == 1 and a[i] == 1:
        c[i] = 1
        a[i] = 0
        b[i] = 0



A = sum(a)
B = sum(b)
C = sum(c)
if C >= 1:
    ans += C-1
    if A >= 1 and B >= 1:
        ans += 2 + min(A-1,B-1)
    elif A == 0 and B == 0:
        pass
    else:
        ans += 1
else:
    ans += min(A,B)

print(ans)
print(A,B,C)

Submission status: Wrong Answer

Solution code:
N = int(input())

ss = [0] * N

a = 65
b = 66

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
list1 = []
count1 = 0
check1 = 0
taila = 0
headb = 0
same1 = 0
for i in range(N):
    list1.append(input())
    if list1[i][0] == "B":
         headb +=1
    if list1[i][len(list1[i])-1] == "A":
         taila +=1
    if (list1[i][len(list1[i])-1] == "A" and list1[i][0] == "B"):
        same1 +=1  
    for j in list1[i]:
        if j == "A":
            check1 = 1
        if (check1 ==1 and j =="B"):
            count1 += 1
            check1 = 0
        if (j != "A" and j != "B"):
            check1 = 0
if max(headb,taila) > same1:
    p1 = min(headb,taila)
else:
    p1 = same1 -1

print(count1+p1)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = 0
t = 0
u = 0
v = 0
for i in range(N):
    m = input()
    u += m.count("AB")
    if m[0] == "B" and m[-1] == "A":
        v += 1
    else:
        if m[0] == "B":
            s += 1
        if m[-1] == "A":
            t += 1
if s == 0 and t == 0:
    u += v - 1
    print(u)
elif s == 0 or t == 0:
    u += v
    print(u)
else: 
    u += min(s - 1, t - 1) + v + 1
    print(u)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
ans,a,b,c = 0,0,0,0
for i in range(N):
  S = input()
  ans += S.count("AB")
  if S[0] == 'B' and S[-1] == 'A':
    c += 1
  if S[0] == 'B':
    b += 1
  if S[-1] == 'A':
    a += 1
print(ans + (min(a,b) if c != a or c != b else a - 1))

Submission status: Wrong Answer

Solution code:
N = int(input())
s = []
ab_num = 0
b_start = 0
a_end = 0
ba_word = 0
for i in range(N):
    tmp = input()
    s.append(tmp)
    rep = tmp.replace("AB", "")
    ab_num += (len(tmp) - len(rep)) // 2
    if tmp[0] == "B":
        b_start += 1
    if tmp[-1] == "A":
        a_end += 1
    if tmp[0] == "B" and tmp[-1] == "A":
        ba_word += 1
if b_start == a_end == ba_word:
    print(ab_num + min(a_end, b_start) - 1)
else:
    print(ab_num + min(a_end, b_start))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
first_c = []
last_c = []
XA = 0
BA = 0
BX = 0

ans = 0

for s in S:
    first_c.append(s[0])
    last_c.append(s[len(s)-1])
    ans += s.count("AB")

for i in range(len(S)):
    if first_c[i] == "B" and last_c[i] == "A":
        BA += 1
    elif first_c[i] == "B" and last_c[i] != "A":
        BX += 1
    elif first_c[i] != "B" and last_c[i] == "A":
        XA += 1

if BX > 0 and XA > 0:
    ans += min(BX, XA) + BA
elif BX > 0 or XA > 0:
    ans += BA
else:
    ans += BA-1

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = []
count = 0
countA = 0
countB = 0
countAB = 0
for i in range(N):
	S_in = str(input())
	for j in range(len(S_in)-1):
		if S_in[j:j+2] == 'AB':
			count += 1
	if (S_in[0] == 'B') & (S_in[-1] == 'A'):
		countAB	+= 1
	elif S_in[0] == 'B':
		countB += 1
	elif S_in[-1] == 'A':
		countA += 1
	S.append(S_in)
if (countA == 0) & (countB == 0):
	print(count + countAB - 1)
else:
	print(count + countAB + min(countA, countB))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[]
for i in range(n):
    s.append(str(input()))
d=0
freea=0
freeb=0
fabin=False
for i in range(n):
    c=0
    for j in range(len(s[i])-1):
        if s[i][j]=="A" and s[i][j+1]=="B":
            c+=1
    if s[i][0]=="B":
        if s[i][-1]=="A":
            if fabin:
                d = d + c + 1
            elif freeb>0:
                d = d+c+1
                freeb=freeb-1
            else:
                d=d+c
        else:
            if freea>0:
                d=d+c+1
                freea = freea -1
            else:
                d=d+c
                freeb = freeb +1
    else:
        if s[i][-1]=="A":
            if freeb>0:
                d=d+c+1
                freeb=freeb-1
            else:
                d=d+c
                freea=freea+1
        else:
            d=d+c
    for j in range(len(s[i])-1):
        if s[i][j]=="A" and s[i][j+1]!="B":
            freea+=1
        if s[i][j]!="A" and s[i][j+1]=="B":
            freeb+=1
    fabin=fabin or "AB" in s[i]
print(d)

Submission status: Wrong Answer

Solution code:

# coding: utf-8

# In[5]:

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


# In[12]:

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


# In[ ]:

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
# f1: B....X
# f2: X....A
# f3: B....A
f1, f2, f3 = 0, 0, 0
ans = 0
for i in range(n):
    t = input(); s.append(t)
    ans += t.count("AB")
    if t[0] == "B" and t[-1] != "A":
        f1 += 1
    if t[0] != "B" and t[-1] == "A":
        f2 += 1
    if t[0] == "B" and t[-1] == "A":
        f3 += 1
# f1: B....X
# f2: X....A
# f3: B....A
if f1 == 0:
    ans += min(f2, f3)
    if f2 == 0:
        ans += max(0, f3-1)
    else:
        t = min(f2, f3)
        f2 -= t
        f3 -= t
        ans += f3

elif f2 == 0:
    ans += min(f1, f3)
elif f3 == 0:
    ans += min(f1, f2)
else:
    ans += min(f2, f3)
    t = min(f2, f3)
    f2 -= t
    f3 -= t

    if f3 == 0:
        f2 += 1
        ans += min(f1, f2)
    elif f2 == 0:
        ans += f3
        ans += 1

print(ans)

Submission status: Wrong Answer

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

hd = 0
tl = 0
bt = 0
ans = 0
for s in S:
    for i in range(len(S)-1):
        if s[i:i+2] == 'AB':
            ans += 1
    if s[0] == 'B' and s[-1] == 'A':
        bt += 1
    elif s[0] == 'B':
        hd += 1
    elif s[-1] == 'A':
        tl += 1

ps = min(hd, tl, bt)

#print(hd, tl, bt, ps)

hd -= ps
tl -= ps
bt -= ps
ans += ps*2

if bt > 0 and ps > 0:
    ans += bt*2
elif bt > 0:
    ans += bt-1
    if hd or tl:
        ans += 1
else:
    ans += min(hd, tl)

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=0
d={"a":0,"b":0,"w":0}
for i in range(n):
    s=input()
    if s[0]=="B" and s[-1]=="A": d["w"]+=1
    elif s[0]=="B" : d["b"]+=1
    elif s[-1]=="A" : d["a"]+=1
    ans+=s.count("AB")
if d["a"]==0 and d["w"]>0:
    d["a"]=1
    d["w"]-=1
if d["b"]>0:
    ans+=1
    d["b"]-=1
d["a"]-=1
if d["w"]>0: ans+=d["w"]
ans+=max(0,min(d["a"],d["b"]))
print(ans)

Submission status: Wrong Answer

Solution code:
print("azarashi")

Submission status: Wrong Answer

Solution code:


n = int(input())

cnt = 0
a_flag = 0
b_flag = 0


s = [0]*n
for i in range(n):
    s[i] = input()

    for j in range(len(s[i])-1):
        if s[i][j:j+2] == 'AB':
            cnt += 1
    if s[i][0] == 'B':
        b_flag += 1
    if s[i][-1] == 'A':
        a_flag += 1        


print(cnt + min(a_flag, b_flag))

Submission status: Wrong Answer

Solution code:
N = int(input())
num_tail_A = 0
num_head_B = 0
count = 0
for i in range(N):
  s = input()
  if('AB' in s):
    count += 1
  if(s[len(s)-1]  in 'A'):
    num_tail_A += 1
  if(s[0] in 'B'):
    num_head_B += 1

result = count + min(num_tail_A, num_head_B)

print(result)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

count_AB = 0
lefter_B = 0
righter_A = 0
for i in range(N):
    count_AB += S[i].count('AB')
    if S[i][0] == 'B':
        lefter_B += 1
    if S[i][len(S[i])-1] == 'A':
        righter_A += 1       
if min(lefter_B,righter_A) == len(S):
    Appearing_AB = min(lefter_B,righter_A) - 1
else:
    Appearing_AB = min(lefter_B,righter_A)


print(count_AB + Appearing_AB)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = []
for n in range(N):
    L.append(input())
AB = 0
A = 0
B = 0
C = 0
for n in range(N):
    if "AB" in L[n]:
        AB += 1
    if L[n][-1:] == "A":
        if L[n][0] == "B":
            C += 1
        else:
            A += 1
    elif L[n][0] == "B":
        B += 1
ans = 0
if A or B:
    ans = AB + C + min(A,B) 
else:
    ans = AB + C-1
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# input
a = int(input())
s = 0
bc = 0
ac = 0
tc = 0
for i in range(a):
    t = input()
    s += t.count('AB')
    if t[:1] == 'B':
        bc += 1
    if t[-1:] == 'A':
        ac += 1
    if t[:1] == 'B' or t[-1:] == 'A':
        tc += 1
if min(bc, ac) == tc:
    o = s + min(bc, ac)-1
else:
    o = s + min(bc, ac)
print(o)

Submission status: Wrong Answer

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

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

for i in range(n):
  if s[i][0:1] == 'B':
    b += 1
  if s[i][-1:] == 'A':
    a += 1
  if s[i][0:1] == 'B' and s[i][-1:] == 'A':
    ab += 1

  c += s[i].count('AB')

c += min(min(a, b), n-1)

if ab == a and ab == b and ab < n and c > 0:
  c -= 1

print(c)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline

N = int(readline())
S_list = [readline().rstrip() for i in range(N)]

count = 0
head_B = 0
tail_A = 0
AxB = 0
for s in S_list:
    for i in range(len(s)):
        if s[i:i+2] == 'AB':
            count += 1
    if s[0] == 'B':
        head_B += 1
    if s[-1] == 'A':
        tail_A += 1
    if (s[0] == 'B') != (s[-1] == 'A'):
        AxB += 1

if head_B == tail_A:
    if AxB > 0:
        print(count + head_B)
    else:
        print(count + head_B - 1)
else:
    print(count + min(head_B, tail_A))

Submission status: Wrong Answer

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

ab_count = 0
last_a_and_first_b_count = 0
last_a_count = 0
first_b_count = 0
for s in sn:
    ab_count += s.count('AB')
    if s[-1] == 'A' and s[0] == 'B':
        last_a_and_first_b_count += 1
    else:
        if s[-1] == 'A':
            last_a_count += 1
        if s[0] == 'B':
            first_b_count += 1

v = min(last_a_count, first_b_count) + last_a_and_first_b_count

print(ab_count + v)