Problem:


Score : 400 points

Problem Statement

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

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

Constraints

1 \leq N \leq 10^{4}

2 \leq |s_i| \leq 10

s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

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

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4



Sample input:
3
ABCA
XBAZ
BAD


Sample output:
2

Submission status: Wrong Answer

Solution code:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from math import factorial, sqrt, ceil, gcd
from functools import lru_cache, reduce
from decimal import Decimal
from operator import mul

INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)


# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 組合せnCr
def combinations_count(n, r):
    if n < r:
        return 0
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# 素数判定
def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True


# limit以下の素数を列挙
def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P

# 同じものを含む順列
def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        # set（集合）型で重複を削除、ソート
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

# ここから書き始める
n = int(input())
s = [input() for i in range(n)]

cnt1 = 0
cnt2 = 0
cnt3 = 0
ans = 0
for i in s:
    ans += i.count("AB")
    if i[0] == "B" and i[-1] == "A":
        cnt1 += 1
    elif i[0] == "B":
        cnt2 += 1
    elif i[-1] == "A":
        cnt3 += 1
# print("cnt1 =", cnt1)
# print("cnt2 =", cnt2)
# print("cnt3 =", cnt3)
# print("ans =", ans)

ans += max(cnt1 - 1, 0) + min(cnt2, cnt3)
if cnt2 != cnt3:
    ans += 1
print(ans)
# ans += min(cnt2, cnt3)
# cnt2 -= min(cnt2, cnt3)
# cnt3 -= min(cnt2, cnt3)
# ans += min(max(cnt2, cnt3), cnt1)
# cnt1 -= min(max(cnt2, cnt3), cnt1)
# ans += cnt1 // 2
# print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 23 13:27:43 2020

@author: NEC-PCuser
"""

N = int(input())

ans = 0
a_count = 0
b_count = 0
ab_count = 0
for i in range(N):
    s = input()
    if (s[0] == "B"):
        b_count += 1
    if (s[len(s) - 1] == "A"):
        a_count += 1
    if (s[0] == "B" and s[len(s) - 1] == "A"):
        ab_count += 1
    ans += s.count("AB")
    
    
plus = min([a_count, b_count])
    
if (plus == ab_count):
    plus -= 1

print(ans + plus)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 23 13:27:43 2020

@author: NEC-PCuser
"""

N = int(input())

ans = 0
a_count = 0
b_count = 0
for i in range(N):
    s = input()
    if (s[0] == "B"):
        b_count += 1
    if (s[len(s) - 1] == "A"):
        a_count += 1
    ans += s.count("AB")
    
    
plus = min([a_count, b_count])
    
if (a_count == b_count):
    plus -= 1

print(ans + plus)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 23 13:27:43 2020

@author: NEC-PCuser
"""

N = int(input())

ans = 0
a_count = 0
b_count = 0
for i in range(N):
    s = input()
    if (s[0] == "B"):
        b_count += 1
    if (s[len(s) - 1] == "A"):
        a_count += 1
    ans += s.count("AB")
    
    
plus = min([a_count, b_count])
    
if (plus == N):
    plus -= 1

print(ans + plus)

Submission status: Wrong Answer

Solution code:
def run():
    n = int(input())
    s = [input() for _ in range(n)]
    ac, bc, ans = 0, 0, 0
    sn = n
    for line in s:
        count = line.count('AB')
        if count == n // 2:
            ans += count
            sn -= 1
        else:
            ac += 1 if line.endswith('A') else 0
            bc += 1 if line.startswith('B') else 0
            ans += line.count('AB')
    ac = min(ac, bc)
    if ac == sn:
        ac -= 1
    print(ans + ac)

if __name__ == '__main__':
    run()

Submission status: Wrong Answer

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

if __name__ == '__main__':
    run()

Submission status: Wrong Answer

Solution code:
def run():
    n = int(input())
    s = [input() for s in range(n)]
    ac, bc, ans = 0, 0, 0
    for line in s:
        ac += 1 if line.endswith('A') else 0
        bc += 1 if line.startswith('B') else 0
        ans += line.count('AB')
    ac = min(ac, bc)
    if ac == n:
        ac -= 1
    print(ans + ac)

if __name__ == '__main__':
    run()

Submission status: Wrong Answer

Solution code:
def run():
    n = int(input())
    s = [input() for s in range(n)]
    ac, bc, ans = 0, 0, 0
    for line in s:
        ac += 1 if line.endswith('A') else 0
        bc += 1 if line.startswith('B') else 0
        ans += line.count('AB')
    if ac == n:
        ac -= 1
    print(ans + ac)

if __name__ == '__main__':
    run()

Submission status: Wrong Answer

Solution code:
def run():
    n = int(input())
    s = [input() for s in range(n)]
    ac, bc, ans = 0, 0, 0
    for line in s:
        ac += 1 if line.endswith('A') else 0
        bc += 1 if line.startswith('B') else 0
        ans += line.count('AB')
    t = min(ac, bc)
    if t == n:
        t -= 1
    print(ans + t)

if __name__ == '__main__':
    run()

Submission status: Wrong Answer

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')
AtoZ = "abcdefghijklmnopqrstuvwxyz"

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  divisors.sort()
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############
N = I()
data = SLs(N)

data = [d.replace("AB","#") for d in data]
ans = sum(Counter(d)["#"] if "#" in d else 0 for d in data)
ab = [(int(d[0]=="B"),int(d[-1]=="A")) for d in data]

dic = DD(int)
for x in ab:
  dic[x] += 1
if dic[(1,1)]==0:
  print(ans+min(dic[(1,0)],dic[(0,1)]))
else:
  print(ans+dic[(1,1)]-1+min(dic[(1,0)],dic[(0,1)])+1)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

Submission status: Wrong Answer

Solution code:
N = int(input())

As = 0
Bs = 0
ABs = 0

for i in range (0, N):
	K = str(input())
	ABs+= K.count('AB')
	if K[0] == 'B':
		Bs+=1
	if K[-1] == 'A':
		As+=1

if As == Bs and As == N:
	print(ABs+N-1)
else:
	print(ABs+min(As, Bs))

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
dic = {'A':0, 'B':0, 'BA':0}
for i in range(N):
    tmp = input()
    if 'AB' in tmp:
        ans += tmp.count('AB')
    tmp = tmp.replace('AB','ZZ')
    if tmp[-1] == 'A' and tmp[0] == 'B':
        dic['BA'] += 1
    elif tmp[0] == 'B':
        dic['B'] += 1
    elif tmp[-1] == 'A':
        dic['A'] += 1

if dic['B'] >= 1 and dic['A'] >= 1 and dic['BA'] >= 1:
    ans += dic['BA'] + 1
    dic['BA'] = 0
    dic['B'] -= 1
    dic['A'] -= 1
if dic['BA'] >= 1:
    if dic['A'] == 0 and dic['B'] == 0:
        ans += dic['BA'] - 1
    elif dic['A'] == 0 or dic['B'] == 0:
        ans += dic['BA']

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
dic = {'A':0, 'B':0, 'BA':0}
for i in range(N):
    tmp = input()
    if 'AB' in tmp:
        ans += tmp.count('AB')
    tmp = tmp.replace('AB','ZZ')
    if tmp[-1] == 'A' and tmp[0] == 'B':
        dic['BA'] += 1
    elif tmp[0] == 'B':
        dic['B'] += 1
    elif tmp[-1] == 'A':
        dic['A'] += 1
while True:
    if dic['B'] >= 1 and dic['A'] >= 1 and dic['BA'] >= 1:
        dic['BA'] -= 1
        dic['B'] -= 1
        dic['A'] -= 1
        ans += 2
    if dic['BA'] == 0:
        ans += min(dic['B'],dic['A'])
        break
    if dic['A'] == 0 and dic['B'] == 0:
        ans += dic['BA'] - 1
        break
    if dic['A'] == 0 or dic['B'] == 0:
        ans += dic['BA']
        break
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
dic = {'A':0, 'B':0, 'BA':0}
for i in range(N):
    tmp = input()
    if 'AB' in tmp:
        ans += tmp.count('AB')
    tmp = tmp.replace('AB','ZZ')
    if tmp[-1] == 'A' and tmp[0] == 'B':
        dic['BA'] += 1
    elif tmp[0] == 'B':
        dic['B'] += 1
    elif tmp[-1] == 'A':
        dic['A'] += 1
while True:
    if dic['B'] >= 1 and dic['A'] >= 1 and dic['BA'] >= 1:
        dic['BA'] -= 1
        dic['B'] -= 1
        dic['A'] -= 1
        ans += 2
    if dic['BA'] == 0:
        ans += min(dic['B'],dic['A'])
        break
    if dic['A'] == 0 or dic['B'] == 0:
        ans += dic['BA']
        break
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
dic = {'A':0, 'B':0, 'BA':0}
for i in range(N):
    tmp = input()
    if 'AB' in tmp:
        ans += tmp.count('AB')
    tmp = tmp.replace('AB','ZZ')
    if tmp[-1] == 'A' and tmp[0] == 'B':
        dic['BA'] += 1
    elif tmp[0] == 'B':
        dic['B'] += 1
    elif tmp[-1] == 'A':
        dic['A'] += 1
while True:
    if dic['B'] >= 1 and dic['A'] >= 1:
        dic['BA'] -= 1
        dic['B'] -= 1
        dic['A'] -= 1
        ans += 2
    if dic['BA'] == 0:
        ans += min(dic['B'],dic['A'])
        break
    if dic['A'] == 0 or dic['B'] == 0:
        ans += dic['BA']
        break
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
dic = {'A':0, 'B':0, 'BA':0}
for i in range(N):
    tmp = input()
    if 'AB' in tmp:
        ans += 1
    tmp = tmp.replace('AB','ZZ')
    if tmp[-1] == 'A' and tmp[0] == 'B':
        dic['BA'] += 1
    elif tmp[0] == 'B':
        dic['B'] += 1
    elif tmp[-1] == 'A':
        dic['A'] += 1
while True:
    if dic['B'] >= 1 and dic['A'] >= 1:
        dic['BA'] -= 1
        dic['B'] -= 1
        dic['A'] -= 1
        ans += 2
    if dic['BA'] == 0:
        ans += min(dic['B'],dic['A'])
        break
    if dic['A'] == 0 or dic['B'] == 0:
        ans += dic['BA']
        break
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = 0
B = 0
AB = 0
ans = 0

for _ in range(N):
    S = list(input())

    for i in range(len(S)):
        try:
            if S[i] == 'A' and S[i+1] == 'B':
                ans += 1
        except:
            pass
    if S[0] == 'B':
        B += 1
        if S[-1] == 'A':
            A += 1
            AB += 1
    elif S[-1] == 'A':
        A += 1

if A == B and A ==AB:
    print(A-1+ans)
else:
    print(min(A,B)+ans)

Submission status: Wrong Answer

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


print(m())

Submission status: Wrong Answer

Solution code:
def m():
    N = int(input())
    S = [input() for _ in range(N)]
    A = B = 0
    ans = 0
    for s in S:
        ans += s.count('AB')
    if N == 1:
        return ans

    for s in S:
        if s[0] == 'B': B += 1
        if s[-1] == 'A': A += 1
    ans += min(A, B)
    return ans


print(m())

Submission status: Wrong Answer

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

print(m())

Submission status: Wrong Answer

Solution code:
def solve():
    N = int(input())
    a = 0
    b = 0
    ab = 0
    ans = 0
    for _ in range(N):
        s = input()
        if s[-1] == 'A' and s[0] == 'B':
            ab += 1
        elif s[-1] == 'A':
            a += 1
        elif s[0] == 'B':
            b += 1
        ans += s.count('AB')
    ans += min(a, b)
    if ab != 0:
        m = min(a, b)
        a -= m
        b -= m
        if a > 0 or b > 0:
            ans += ab
        else:
            ans += ab - 1
            
    return ans
    
print(solve())

Submission status: Wrong Answer

Solution code:
def solve():
    N = int(input())
    aend = 0
    bstart = 0
    ab = 0
    ans = 0
    for _ in range(N):
        s = input()
        if s[-1] == 'A':
            aend += 1
        if s[0] == 'B':
            bstart += 1
        if s[-1] == 'A' and s[0] == 'B':
         	ab = 1
        ans += s.count('AB')
    
    ans += max(min(aend-ab, bstart), min(aend, bstart-ab))
    return ans
    
print(solve())

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N = int(input())
    s = [input() for _ in range(N)]
    a = 0
    b = 0
    c = 0#a&b
    ans = 0
    for i in range(N):
        if s[i][0] == 'B':
            if s[i][-1] == 'A':
                c += 1
            else:
                b += 1
        elif s[i][-1] == 'A':
            a += 1
        ans += s[i].count('AB')
    if c == 0:
        print(ans + min(a,b))
    else:
        if a == 0 or b == 0:
            print(ans + c)
        else:
            print(ans + (c-1) + min(a+1,b+1))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N = int(input())
    s = [input() for _ in range(N)]
    a = 0
    b = 0
    c = 0#a&b
    ans = 0
    for i in range(N):
        if s[i][0] == 'B':
            if s[i][-1] == 'A':
                c += 1
            else:
                b += 1
        elif s[i][-1] == 'A':
            a += 1
        ans += s[i].count('AB')
    if c == 0:
        print(ans + min(a,b))
    else:
        print(ans + (c-1) + min(a+1,b+1))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N = int(input())
    s = [input() for _ in range(N)]
    a = 0
    b = 0
    ans = 0
    for i in range(N):
        if s[i][0] == 'B':
            b += 1
        if s[i][-1] == 'A':
            a += 1
        ans += s[i].count('AB')
    print(ans + min(a,b,N-1))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
# N, K = [int(i) for i in input().split()]
countA = 0
countB = 0
countAB = 0
countBA = 0
for i in range(N):
    s = input()
    countAB += s.count('AB')
    if s[0] == 'B':
        countB += 1
    if s[-1] == 'A':
        countA += 1
    if s[0] == 'B' and s[-1] == 'A':
        countBA += 1
if countA == countBA and countB == countBA:
    print(countAB + countBA - 1)
else:
    print(countAB + min(countA, countB))

Submission status: Wrong Answer

Solution code:
N = int(input())
# N, K = [int(i) for i in input().split()]
countA = 0
countB = 0
countAB = 0
for i in range(N):
    s = input()
    countAB += s.count('AB')
    if s[0] == 'B':
        countB += 1
    if s[-1] == 'A':
        countA += 1
print(countAB + min(countA, countB))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [0]*n
for i in range(n):
    s[i] = input()
c = 0
c_s = [0]*n
c_e = [0]*n
j = 0
for S in s:
    if S[0] == "B":
        c_s[j] = 1
    if S[-1] == "A":
        c_e[j] = 1
    j += 1
    for i in range(len(S)-1):
        if S[i] == "A" and S[i+1] == "B":
            c += 1
m = 0
if c_e == c_s:
    m = 1
    for i in range(n):
        if c_s[i] == 0 and c_e[i] == 0:
            pass
        elif c_s[i] == 1 and c_e[i] == 1:
            pass
        else:
            m = 0
            break
ans = c + min(sum(c_s),sum(c_e)) - m
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
ans, c, h, t = 0, 0, 0, 0

for x in sys.stdin.readlines():
    x = x.rstrip()
    ans += x.count("AB")
    if x[-1] == "A" and x[0] == "B":
        c += 1
    elif x[-1] == "A":
        t += 1
    elif x[0] == "B":
        h += 1
if t or h:
    print(ans + min(t, h) + c)
else:
    print(ans + min(c-1, 0))

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
ans, c, h, t = 0, 0, 0, 0

for x in sys.stdin.readlines():
    x = x.rstrip()
    ans += x.count("AB")
    if x[-1] == "A" and x[0] == "B":
        c += 1
    elif x[-1] == "A":
        t += 1
    elif x[0] == "B":
        h += 1
if t or h:
    print(ans + min(t, h) + c)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
ans, c, h, t = 0, 0, 0, 0

for x in sys.stdin.readlines():
    x = x.rstrip()
    ans += x.count("AB")
    if x[-1] == "A" and x[0] == "B":
        c += 1
    elif x[-1] == "A":
        t += 1
    elif x[0] == "B":
        h += 1
print(ans + min(t, h) + c)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input() for _ in range(n))
cou = 0
for i in s:
    cou += i.count('AB')
a = 0
b = 0
both = 0
for i in s:
    if i[0]=="B":
        b += 1
    if i[-1]=="A":
        a += 1
    if i[0]=="B" and i[-1]=="A":
        both += 1
if min(a,b)==both and a==b:
    print(max(cou+min(a,b)-1,0))
else:
    print(cou+min(a,b))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input() for _ in range(n))
cou = 0
for i in s:
    cou += i.count('AB')
a = 0
b = 0
both = 0
for i in s:
    if i[0]=="B":
        b += 1
    if i[-1]=="A":
        a += 1
    if i[0]=="B" and i[-1]=="A":
        both += 1
if min(a,b)==both  :
    print(cou+min(a,b)-1)
else:
    print(cou+min(a,b))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input() for _ in range(n))
cou = 0
for i in s:
    cou += i.count('AB')
a = 0
b = 0
both = 0
for i in s:
    if i[0]=="B":
        b += 1
    if i[-1]=="A":
        a += 1
    if i[0]=="B" and i[-1]=="A":
        both += 1
if min(a,b)==both and a==b:
    print(cou+min(a,b)-1)
else:
    print(cou+min(a,b))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input() for _ in range(n))
cou = 0
for i in s:
    cou += i.count('AB')
a = 0
b = 0
both = 0
for i in s:
    if i[0]=="B":
        b += 1
    if i[-1]=="A":
        a += 1
    if i[0]=="B" and i[-1]=="A":
        both += 1
if min(a,b)>both:
    print(cou+min(a,b))
else:
    print(cou+min(a,b)-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input() for _ in range(n))
cou = 0
for i in s:
    cou += i.count('AB')
a = 0
b = 0
both = 0
for i in s:
    if i[0]=="B":
        b += 1
    if i[-1]=="A":
        a += 1
    if i[0]=="B" and i[-1]=="A":
        both += 1
if max(a,b)-min(a,b)>=min(a,b)-both:
    print(cou+min(a,b))
else:
    print(cou+min(a,b)-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input() for _ in range(n))
cou = 0
for i in s:
    cou += i.count('AB')
a = 0
b = 0
both = 0
for i in s:
    if i[0]=="B":
        b += 1
    if i[-1]=="A":
        a += 1
    if i[0]=="B" and i[-1]=="A":
        both += 1
if min(a,b)>=both:
    print(cou+min(a,b))
else:
    print(cou+min(a,b)-1)

Submission status: Wrong Answer

Solution code:
#import sys
#import numpy as np
#import math
#import itertools
#from fractions import Fraction
#import itertools
from collections import deque
#import heapq
#from fractions  import gcd
#input=sys.stdin.readline
#import bisect

n=int(input())
s=[input() for _ in range(n)]
ans=0
bn=an=0
d=0
for i in range(n):
    ans+=s[i].count("AB")
    if s[i][0]=="B":
        an+=1
        if s[i][-1]=="A":
            d+=1
    if s[i][-1]=="A":
        bn+=1
if bn==an==d:
    print(ans+d-1)
else:
    print(ans+min(an,bn))

Submission status: Wrong Answer

Solution code:
#import sys
#import numpy as np
#import math
#import itertools
#from fractions import Fraction
#import itertools
from collections import deque
#import heapq
#from fractions  import gcd
#input=sys.stdin.readline
#import bisect

n=int(input())
s=[input() for _ in range(n)]
ans=0
bn=an=0
d=0
for i in range(n):
    ans+=s[i].count("AB")
    if s[i][0]=="B":
        an+=1
        if s[i][-1]=="B":
            d+=1
    if s[i][-1]=="A":
        bn+=1
if bn==an==d:
    print(ans+d-1)
else:
    print(ans+min(an,bn))

Submission status: Wrong Answer

Solution code:
import sys
N = int(sys.stdin.readline().rstrip())

ans = 0
cnt_start_b = 0
cnt_end_a = 0
cnt_start_b_end_a = 0

for _ in range(N):
    s = sys.stdin.readline().rstrip()
    ans += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        cnt_start_b_end_a += 1
    elif s[0] == "B":
        cnt_start_b += 1
    elif s[-1] == "A":
        cnt_end_a += 1

ans += min(cnt_end_a, cnt_start_b)
if cnt_start_b_end_a > 0:
    ans += cnt_start_b_end_a - 1
    if abs(cnt_start_b - cnt_end_a) > 0:
        ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N = int(sys.stdin.readline().rstrip())

ans = 0
cnt_start_b = 0
cnt_end_a = 0
cnt_start_b_end_a = 0

for _ in range(N):
    s = sys.stdin.readline().rstrip()
    ans += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        cnt_start_b_end_a += 1
    elif s[0] == "B":
        cnt_start_b += 1
    elif s[-1] == "A":
        cnt_end_a += 1

my_min = min(cnt_end_a, cnt_start_b, cnt_start_b_end_a)
ans += my_min * 2

cnt_end_a -= my_min
cnt_start_b -= my_min
cnt_start_b_end_a -= my_min

if cnt_start_b_end_a == 0:
    ans += min(cnt_end_a, cnt_start_b)
else:
    my_max = min(1, max(cnt_end_a, cnt_start_b))
    ans += cnt_start_b_end_a - 1 + my_max

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N = int(sys.stdin.readline().rstrip())

ans = 0
cnt_start_b = 0
cnt_end_a = 0
cnt_start_b_end_a = 0

for _ in range(N):
    s = sys.stdin.readline().rstrip()
    ans += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        cnt_start_b_end_a += 1
    elif s[0] == "B":
        cnt_start_b += 1
    elif s[-1] == "A":
        cnt_end_a += 1

ans += min(cnt_end_a, cnt_start_b)
diff = abs(cnt_end_a - cnt_start_b) 
ans += min(diff, cnt_start_b_end_a)
if cnt_end_a > cnt_start_b:
    ans += max(0, cnt_start_b_end_a - diff)
else:
    ans += max(0, cnt_start_b_end_a - diff - 1)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N = int(sys.stdin.readline().rstrip())

ans = 0
cnt_start_b = 0
cnt_end_a = 0
cnt_start_b_end_a = 0

for _ in range(N):
    s = sys.stdin.readline().rstrip()
    ans += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        cnt_start_b_end_a += 1
    elif s[0] == "B":
        cnt_start_b += 1
    elif s[-1] == "A":
        cnt_end_a += 1

ans += min(cnt_end_a, cnt_start_b)
diff = abs(cnt_start_b - cnt_end_a)
ans += min(diff, cnt_start_b_end_a)
ans += max(0, cnt_start_b_end_a - diff - 1)
if cnt_end_a > cnt_start_b and cnt_start_b_end_a - diff > 0:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N = int(sys.stdin.readline().rstrip())

ans = 0
cnt_start_b = 0
cnt_end_a = 0
cnt_start_b_end_a = 0

for _ in range(N):
    s = sys.stdin.readline().rstrip()
    if "AB" in s:
        ans += 1
    if s[0] == "B" and s[-1] == "A":
        cnt_start_b_end_a += 1
    elif s[0] == "B":
        cnt_start_b += 1
    elif s[-1] == "A":
        cnt_end_a += 1

ans += min(cnt_end_a, cnt_start_b)
diff = abs(cnt_start_b - cnt_end_a)
ans += min(diff, cnt_start_b_end_a)
ans += max(0, cnt_start_b_end_a - diff - 1)
if cnt_end_a > cnt_start_b and cnt_start_b_end_a - diff > 0:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N = int(sys.stdin.readline().rstrip())

ans = 0
cnt_start_b = 0
cnt_end_a = 0
cnt_start_b_end_a = 0

for _ in range(N):
    s = sys.stdin.readline().rstrip()
    if "AB" in s:
        ans += 1
    if s[0] == "B" and s[-1] == "A":
        cnt_start_b_end_a += 1
    elif s[0] == "B":
        cnt_start_b += 1
    elif s[-1] == "A":
        cnt_end_a += 1

ans += min(cnt_end_a, cnt_start_b)
diff = abs(cnt_start_b - cnt_end_a)
ans += min(diff, cnt_start_b_end_a)
ans += max(0, cnt_start_b_end_a - diff - 1)
if cnt_end_a > cnt_start_b:
    ans += 1
print(ans)