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:
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":
                if sen[-1] == "A":
                    self.case_1 = self.case_1 + 1
                    continue
                else:
                    self.case_2 = self.case_2 + 1
                    continue
            if 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:
N = int(input())
word = []
num = []
a = []
count = 0
type_A = 0
type_B = 0
type_C = 0
for i in range(N):
  word.append(input())
for i in range(len(word)):
  text = word[i]
  AB = word[i].count('AB')
  count += AB
  if text[0] == 'B' and text[-1] == 'A':
    type_C += 1
  elif text[-1] == 'A':
    type_A += 1
  elif text[0] == 'B':
    type_B += 1
num.append(type_A)
num.append(type_B)
num.append(type_C)
a = sorted(num)
if a[0] == type_C:
  print(a[0]*2+a[1]-a[0]+count)
else:
  k = type_C - a[0] 
  m = max(type_A - a[0],type_B - a[0])
  if k > m and m != 0:
    print(a[0]*2+k+count)
  elif k > m and m == 0:
    print(a[0]*2+k-1+count)
  elif k < m or k == m:
    print(a[0]*2+k+count)

Submission status: Wrong Answer

Solution code:
N = int(input())
s_list =[]

for i in range(N):
    s_list.append(input())

cnt = 0
cnt1 = 0

cnt2 = 0
cnt3 = 0
BA = 0
nA = 0
Bn = 0

for s in s_list:
    cnt += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        BA += 1
    elif s[0] != 'B' and s[-1] == 'A':
        nA += 1
    elif s[0] == 'B' and s[-1] != 'A':
        Bn += 1

if nA > Bn:
    cnt1 = Bn # nABn
    defference = nA - Bn
else:
    cnt1 = nA # nABn
    defference = Bn - nA

if defference > BA:
    cnt2 = BA
else:
    cnt2 = defference
    BA = BA - defference
    if BA > 1:
        cnt3 = BA - 1

print(cnt + cnt1 + cnt2 +cnt3)

Submission status: Wrong Answer

Solution code:
N = int(input())
s_list =[]

for i in range(N):
    s_list.append(input())

cnt = 0
cnt1 = 0

cnt2 = 0
cnt3 = 0
BA = 0
nA = 0
Bn = 0

for s in s_list:
    cnt += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        BA += 1
    elif s[0] != 'B' and s[-1] == 'A':
        nA += 1
    elif s[0] == 'B' and s[-1] != 'A':
        Bn += 1

if nA > Bn:
    cnt1 = Bn # nABn
    defference = nA - Bn
else:
    cnt1 = nA # nABn
    defference = Bn - nA

if defference > BA:
    cnt2 = BA
else:
    cnt2 = defference
    BA = BA - defference
    cnt3 = BA//2

print(cnt + cnt1 + cnt2 +cnt3)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = []
s_cnt = 0
for a in range(N):
  s_str = input()
  s.append(s_str)
for i in range(0,N):
  s_cnt_i = s[i].count("AB")
  s_cnt += s_cnt_i
  i_val = list(set(list(s[i])))
  i_val_cnt_b = 0
  i_val_cnt_a = 0
  if i_val[0] == "b":
    i_val_cnt_b += 1
  if i_val[-1] == "a":
    i_val_cnt_a += 1
if i_val_cnt_a >= i_val_cnt_b:
  print(i_val_cnt_a + s_cnt)
else:
  print(i_val_cnt_b + s_cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
strings_list = [input() for i in range(n)]
ab_count = 0
start_b = 0
end_a = 0
for i in range(n):
  ab_count += strings_list[i].count('AB')
  if strings_list[i][0] == 'B':
    start_b += 1
  if strings_list[i][-1] == 'A':
    end_a += 1
    
print(ab_count+min(start_b,end_a))

Submission status: Wrong Answer

Solution code:
n = int(input())
first_b_num = 0
last_a_num = 0
ab_num = 0
 
for _ in range(n):
    s_list = list(input())
    for index,s in enumerate(s_list):
        if(index == 0):
            if(s == 'B'):
                first_b_num += 1
        else:
            if(s_list[index-1]+s == 'AB'):
                ab_num += 1
    if(s_list[index] == 'A'):
        last_a_num += 1
if(first_b_num == last_a_num):
    print(ab_num + first_b_num)
else:
    print(ab_num + min(first_b_num,last_a_num))

Submission status: Wrong Answer

Solution code:
n = int(input())

ans, tailA, headB, overlap = 0, 0, 0, 0
for _ in range(n):
    s = input()
    ans += 1 * ('AB' in s)
    tailA += 1 * (s[-1] == 'A')
    headB += 1 * (s[0] == 'B')
    overlap += 1 * (s[-1] == 'A' and s[0] == 'B')

tailA -= overlap
headB -= overlap

if overlap == 0:
    ans += min(tailA, headB)
else:
    if tailA + headB > 0:
        ans += overlap + min(tailA, headB)
    else:
        ans += overlap - 1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

ans, tailA, headB, overlap = 0, 0, 0, 0
for _ in range(n):
    s = input()
    ans += 1 * ('AB' in s)
    tailA += 1 * (s[-1] == 'A')
    headB += 1 * (s[0] == 'B')
    overlap += 1 * (s[-1] == 'A' and s[0] == 'B')

if overlap > 0:
    tailA -= overlap
    headB -= overlap
    ans += overlap - 1
    if tailA > 0:
        ans += 1
        tailA -= 1
    if headB > 0:
        ans += 1
        headB -= 1

ans += min(tailA, headB)
print(ans)

Submission status: Wrong Answer

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

n = int(input())
a = 0
b = 0
ba = 0
ans = 0

for i in range(n):
  s = input().strip()
  leng = len(s)
  if s[0] == 'B' and s[-1] == 'A':
    ba += 1
    a += 1
    b += 1
  elif s[-1] == 'A':
    a += 1
  elif s[0] == 'B':
    b += 1
  for j in range(leng - 1):
    if s[j] == 'A' and s[j+1] == 'B':
      ans += 1

if ba == b == a:
  ans += min(b,a) - 1
else:
  ans += min(b,a)
print(ans)

Submission status: Wrong Answer

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

n = int(input())
a = 0
b = 0
ba = 0
ans = 0

for i in range(n):
  s = input().strip()
  leng = len(s)
  if s[0] == 'B' and s[-1] == 'A':
    ba += 1
    a += 1
    b += 1
  elif s[-1] == 'A':
    a += 1
  elif s[0] == 'B':
    b += 1
  for j in range(leng - 1):
    if s[j] == 'A' and s[j+1] == 'B':
      ans += 1

if ba > 0 and b == a:
  ans += min(b,a) - 1
else:
  ans += min(b,a)
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import exit

N = int(input())

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

count = 0
a_count = 0
b_count = 0
a_b_count = 0

for s in S:
	if s[0] == 'B' and not s[-1] == 'A':
		b_count += 1
	if not s[0] == 'B' and s[-1] == 'A':
		a_count += 1
	if s[0] == 'B' and s[-1] == 'A':
		a_b_count += 1
	for i in range(len(s) - 1):
		if s[i] == 'A':
			if s[i + 1] == 'B':
				count += 1

if a_b_count == 0:
	print(count + min(a_count, b_count))
else:
	a_count += 1
	b_count += 1
	print(count + min(a_count, b_count) + a_b_count - 1)

Submission status: Wrong Answer

Solution code:
from sys import exit

N = int(input())

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

count = 0
a_count = 0
b_count = 0

for s in S:
	if s[0] == 'B':
		a_count += 1
	if s[-1] == 'A':
		b_count += 1
	for i in range(len(s) - 1):
		if s[i] == 'A':
			if s[i + 1] == 'B':
				count += 1

print(count + min(a_count, b_count))

Submission status: Wrong Answer

Solution code:
N=int(input())
a_list = [input() for i in range(N)]
result = 0
a_number = 0
b_number = 0
for i in a_list:
	if i[0] == 'B':
		b_number+=1
	if i[-1] == 'A':
		a_number+=1
	result+=i.count('AB')
print(result+min(a_number,b_number))

Submission status: Wrong Answer

Solution code:
import sys,heapq
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False
def ddprint(x):
  if DBG:
    print(x)

ab = 0
leftb = 0
righta = 0
lbra = 0
n = inn()
for i in range(n):
    s = input()
    if s[0] == 'B' and s[-1] == 'A':
        lbra += 1
    if s[0] == 'B':
        leftb += 1
    if s[-1] == 'A':
        righta += 1
    for j in range(len(s)-1):
        if s[j:j+2] == 'AB':
            ab += 1

if DBG:
    print("ab {} l {} r {} lbra {} ".format(ab, leftb, righta, lbra))
print(ab + (lbra-1 if lbra==leftb and leftb==righta else min([leftb, righta])))

Submission status: Wrong Answer

Solution code:
import sys,heapq
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False
def ddprint(x):
  if DBG:
    print(x)

ab = 0
leftb = 0
righta = 0
lbra = 0
n = inn()
for i in range(n):
    s = input()
    if s[0] == 'B' and s[-1] == 'A':
        lbra += 1
    elif s[0] == 'B':
        leftb += 1
    elif s[-1] == 'A':
        righta += 1
    for j in range(len(s)-1):
        if s[j:j+2] == 'AB':
            ab += 1

if DBG:
    print("ab {} l {} r {} lbra {} ".format(ab, leftb, righta, lbra))
print(ab + min([leftb, righta]) + \
        (0 if lbra==0 else (lbra - 1 + (0 if leftb==righta else 1))))

Submission status: Wrong Answer

Solution code:
import sys,heapq
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True # and False
def ddprint(x):
  if DBG:
    print(x)

ab = 0
leftb = 0
righta = 0
n = inn()
for i in range(n):
    s = input()
    if s[0] == 'B':
        leftb += 1
    if s[-1] == 'A':
        righta += 1
    for j in range(len(s)-1):
        if s[j:j+2] == 'AB':
            ab += 1

print(ab + min([len(s)-1, leftb, righta]))

Submission status: Wrong Answer

Solution code:
N = int(input())
inside_str = 0
tail_a = 0
head_b = 0
doubleAB = 0
 
for i in range(N):
  str = input()
  inside_str = inside_str + str.count('AB')
  
  if(str[-1] == 'A' and str[0] == 'B'):
  	doubleAB = doubleAB + 1
  elif (str[-1] == 'A'):
    tail_a = tail_a + 1
  elif (str[0] == 'B'):
    head_b = head_b + 1

    
if(tail_a == 0 and head_b == 0):
  total_cand = inside_str + doubleAB - 1
else:
  total_cand = inside_str + doubleAB + min(tail_a, head_b)
  
print(total_cand)

Submission status: Wrong Answer

Solution code:
N = int(input())
inside_str = 0
tail_a = 0
head_b = 0
 
doubleAB = 0
 
for i in range(N):
  str = input()
  inside_str = inside_str + str.count('AB')
  
  if(str[-1] == 'A' and str[0] == 'B'):
  	doubleAB = doubleAB + 1
  elif (str[-1] == 'A'):
    tail_a = tail_a + 1
  elif (str[0] == 'B'):
    head_b = head_b + 1
 
if(tail_a == 0 and head_b == 0):
  total_cand = inside_str + doubleAB - 1
else:
  total_cand = inside_str + doubleAB + min(tail_a, head_b)

Submission status: Wrong Answer

Solution code:

N = int(input())
inside_str = 0
tail_a = 0
head_b = 0
 
doubleAB = 0
 
for i in range(N):
  str = input()
  inside_str = inside_str + str.count('AB')
  
  if(str[-1] == 'A' and str[0] == 'B'):
  	doubleAB = doubleAB + 1
  elif (str[-1] == 'A'):
    tail_a = tail_a + 1
  elif (str[0] == 'B'):
    head_b = head_b + 1

 
total_cand = inside_str + min(tail_a, head_b)
 
if(doubleAB):
  total_cand = total_cand + doubleAB -1

Submission status: Wrong Answer

Solution code:
n = int(input())
l = []
cnt = 0
te = [0,0,0]
for i in range(n):
    l.append(input())
for i in l:
    if i[0] == "B" and i[-1] == "A":
        te[0] += 1
    elif i[0] == "B":
        te[1] += 1
    elif i[-1] == "A":
        te[2] += 1
    for j in range(len(i)-1):
        if i[j] == "A" and i[j+1] == "B":
            cnt += 1
if te[0] != 0:
    te[0] -= 1
if te[1]>0:
    te[0] += 1
    te[1] -= 1
if te[2]>0:
    te[0] += 1
    te[2] -= 1
print(cnt+te[0]+min(te[1],te[2]))

Submission status: Wrong Answer

Solution code:
n = input()

ans = 0

x = y = z = 0
for _ in xrange(n):
    s = raw_input().strip()
    for i in xrange(len(s) - 1):
        if s[i] == 'A' and s[i + 1] == 'B': ans += 1

    if s[0] == 'B' and s[-1] == 'A': z += 1
    elif s[0] == 'B': y += 1
    elif s[-1] == 'A': x += 1

m = min(x, y)
ans += m + z

if x == 0 and y == 0: ans -= 1
ans = max(0, ans)
print ans

Submission status: Wrong Answer

Solution code:
n = input()

ans = 0

x = y = z = 0
for _ in xrange(n):
    s = raw_input().strip()
    for i in xrange(len(s) - 1):
        if s[i] == 'A' and s[i + 1] == 'B': ans += 1

    if s[0] == 'B' and s[-1] == 'A': z += 1
    elif s[0] == 'B': y += 1
    elif s[-1] == 'A': x += 1

m = min(x, y)
ans += m + z

if x == 0 and y == 0: ans -= 1

print ans

Submission status: Wrong Answer

Solution code:
n = int(input())
s_list = []
c = 0
ca = 0
cb = 0
cc = 0
for i in range(n):
  d = list(input())
  for k in range(len(d)-1):
    if d[k] == 'A' and d[k+1] == 'B':
      c += 1
  if d[len(d)-1] == 'A':
    ca += 1
  if d[0] == 'B':
    cb += 1
  if d[0] == 'B' and d[len(d)-1] == 'A':
    cc += 1
print(c + min(ca,cb) - cc)

Submission status: Wrong Answer

Solution code:
n = input()

ans = 0
l = []

eA, sB, eAsB = [], [], []
for _ in xrange(n):
    s = raw_input()
    for i in xrange(len(s) - 1):
        if s[i] == 'A' and s[i + 1] == 'B': ans += 1

    if s[0] == 'B' and s[-1] == 'A': eAsB.append(s)
    elif s[0] == 'B': sB.append(s)
    elif s[-1] == 'A': eA.append(s)

x, y, z = len(eA), len(sB), len(eAsB)

ans += min(x, y) + z

print ans

Submission status: Wrong Answer

Solution code:
N = int(input())
s = 0
front_b = 0
tail_a = 0
has_a_b = 0
for i in range(N):
    t = input()
    s += t.count('AB')
    if t[0] == 'B' and t[len(t) -1] == 'A':
        has_a_b += 1
    elif t[0] == 'B':
        front_b += 1
    elif t[len(t) -1] == 'A':
        tail_a += 1
      
res = s
if has_a_b > 0:
    if front_b > 0 and tail_a > 0:
        res += has_a_b + 1 + min(front_b-1, tail_a-1)
    elif front_b > 0 or tail_a > 0:
        res += has_a_b
    else:
        pass
else:
    res += min(front_b, tail_a)

print(res)

Submission status: Wrong Answer

Solution code:
n = int(input())
s_list = []
c = 0
ca = 0
cb = 0
for i in range(n):
  d = list(input())
  for k in range(len(d)-1):
    if d[k] == 'A' and d[k+1] == 'B':
      c += 1
  if d[len(d)-1] == 'A':
    ca += 1
  if d[0] == 'B':
    cb += 1
print(c + min(ca,cb))

Submission status: Wrong Answer

Solution code:
def count(a):
    k = 0
    for i in range(len(a)-1):
        if a[i]=='A' and a[i+1]=='B':
            k = k+1
    return k
def Substrings(n,S):   
    res = 0
    m = 0
    y = 0
    if list(set(S))==['BA']:
        return n-1 
    else:
        for i in range(len(S)):
            res = count(S[i]) + res
            if S[i][-1:]=='A':
                m = m+1
            if S[i][:1]=='B':
                y = y+1 
        return res+min(m,y)    
n = int(input())
S = [input() for i in range(n)]
print(Substrings(n,S))

Submission status: Wrong Answer

Solution code:
def count(a):
    k = 0
    for i in range(len(a)-1):
        if a[i]=='A' and a[i+1]=='B':
            k = k+1
    return k
def Substrings(n,S):   
    res = 0
    m = 0
    y = 0
    t = 0
    if list(set(S))==['BA']:
        return n-1 
    else:
        for i in range(len(S)):
            res = count(S[i]) + res
            if S[i][-1:]=='A':
                m = m+1
            if S[i][:1]=='B':
                y = y+1  
            return res+min(m,y)
    
n = int(input())
S = [input() for i in range(n)]
print(Substrings(n,S))

Submission status: Wrong Answer

Solution code:
n = int(input())
ans, a, b = 0, 0, 0

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

ans += min(a, b, n-1)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans, a, b = 0, 0, 0

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
ab=ba=a=b=0
for i in range(N):
    s=str(input())

    ab += s.count('AB')

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

if a > ba+b:
    d = b+ba
elif b > ba+a:
    d = a+ba
else:
    d = (a+b+ba)//2

print(ab+d)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
XX,XA,BX,BA=[],[],[],[]
n=int(input())
for _ in [0]*n:
    si=input()
    if si[0]=="B":
        if si[-1]=="A":
            BA.append(si)
        else:
            BX.append(si)
    else:
        if si[-1]=="A":
            XA.append(si)
        else:
            XX.append(si)
s=""
for i in range(min(len(XA),len(BX))):
    s=s+XA[i]+BX[i]
if len(XA)==min(len(XA),len(BX)):
    s=s+"".join(BA)
    s=s+"".join(BX[len(XA):])
else:
    s=s+"".join(XA[len(BX):])
    s=s+"".join(BA)
s=s+"".join(XX)
ans=0
for i in range(len(s)-1):
    if s[i:i+2]=="AB":
        ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
ab=ba=a=b=0
for i in range(N):
    s=str(input())

    ab += s.count('AB')

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

d = (a+b+ba)//2

print(ab+d)

Submission status: Wrong Answer

Solution code:
N = int(input())
Ss=[input() for s in range(N)]
count = [s.count("AB") for s in Ss]
headcount = len([s for s in Ss if s[0]=="B" and s[-1]!="A"])
outcount = len([s for s in Ss if s[-1]=="A" and s[0]!="B"])
multicount = len([s for s in Ss if s[0]=="B" and s[-1]=="A"])
if outcount==0 and headcount==0:
    if multicount>0:
        print(sum(count))
    else:
        print(sum(count)+multicount-1)
else:
    print(sum(count)+min(headcount,outcount)+multicount)

Submission status: Wrong Answer

Solution code:
N = int(input())
Ss=[input() for s in range(N)]
count = [s.count("AB") for s in Ss if "AB" in s]
headcount = len([s for s in Ss if s[0]=="B" and s[-1]!="A"])
outcount = len([s for s in Ss if s[-1]=="A" and s[0]!="B"])
multicount = len([s for s in Ss if s[0]=="B" and s[-1]=="A"])
if outcount==0 and headcount==0:
    print(sum(count)+multicount-1)
elif multicount==0:
    print(sum(count) + min(headcount, outcount))
else:
    print(sum(count)+min(headcount,outcount)+multicount)

Submission status: Wrong Answer

Solution code:
N = int(input())
Ss=[input() for s in range(N)]
count = [s.count("AB") for s in Ss if "AB" in s]
headcount = len([s for s in Ss if s[0]=="B" and s[-1]!="A"])
outcount = len([s for s in Ss if s[-1]=="A" and s[0]!="B"])
multicount = len([s for s in Ss if s[0]=="B" and s[-1]=="A"])
if outcount==0 and headcount==0:
    print(sum(count)+multicount-1)
else:
    print(sum(count)+min(headcount,outcount)+multicount)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
b_x = 0
b_a = 0
x_a = 0
y = 0
for _ in range(N):
  s = input()
  ans += s.find('AB')
  if s[0] == 'B' and s[-1] == 'A':
    b_a += 1
  elif s[0] == 'B':
    b_x += 1
  elif s[-1] == 'A':
    x_a += 1
  else:
    y += 1
if x_a >= 1 and b_x >= 1:
  ans += b_a
elif x_a >= 1 or b_x >= 1:
  ans += b_a - 1
else:
  ans += b_a - 2
if x_a - 1 <= 0 or b_x -1 <= 0:
  ans += 0
else:
  ans += min(x_a - 1, b_x - 1)
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
letter_list=[input() for x in range(N)]
default_AB=sum([x.count("AB") for x in letter_list])
first_B=[]
last_A=[]
both_BA=[]
connect_AB=0
for x in letter_list:
    if x[-1]=="A" and x[0]!="B":
        last_A.append(x)
    if x[0]=="B" and x[-1]!="A":
        first_B.append(x)
    if x[0]=="B" and x[-1]=="A":
        both_BA.append(x)

if len(both_BA)==0:
    connect_AB=min(len(last_A),len(first_B))
if len(both_BA)!=0 and len(last_A)+len(first_B)>0:
    connect_AB=len(both_BA)+min(len(last_A),len(first_B))
if len(both_BA)!=0 and len(last_A)+len(first_B)==0:
    connect_AB=len(both_BA)//2
"""
print(default_AB)
print(first_B)
print(last_A)
print(both_BA)
"""
print(default_AB+connect_AB)

Submission status: Wrong Answer

Solution code:
N = int(input())

ans = 0
last_a_num = 0
first_b_num = 0
multi_ba_num = 0
for _ in range(N):
    s = input()
    ans += s.count('AB')
    if s[-1] == 'A' and s[0] == 'B':
        multi_ba_num += 1
    elif s[-1] == 'A':
        last_a_num += 1
    elif s[0] == 'B':
        first_b_num += 1

connect_num = multi_ba_num + min(last_a_num, first_b_num)
if last_a_num + first_b_num == 0:
    connect_num = multi_ba_num - 1
ans += connect_num

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

ans = 0
last_a_num = 0
first_b_num = 0
multi_ba_num = 0
for _ in range(N):
    s = input()
    ans += s.count('AB')
    if s[-1] == 'A':
        last_a_num += 1
    if s[0] == 'B':
        first_b_num += 1
    if s[-1] == 'A' and s[0] == 'B':
        multi_ba_num += 1

connect_num = min(last_a_num, first_b_num)
if last_a_num == first_b_num and last_a_num == multi_ba_num:
    connect_num -= 1
ans += connect_num

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

ans = 0
last_a_num = 0
first_b_num = 0
for _ in range(N):
    s = input()
    ans += s.count('AB')
    if s[-1] == 'A':
        last_a_num += 1
    if s[0] == 'B':
        first_b_num += 1

connect_num = min(last_a_num, first_b_num)
if last_a_num == first_b_num:
    connect_num -= 1
ans += connect_num

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

ans = 0
last_a_num = 0
first_b_num = 0
for _ in range(N):
    s = input()
    ans += s.count('AB')
    if s[-1] == 'A':
        last_a_num += 1
    if s[0] == 'B':
        first_b_num += 1

connect_num = min(last_a_num, first_b_num)
if connect_num == N:
    connect_num -= 1
ans += connect_num

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

Submission status: Wrong Answer

Solution code:
N=int(input())
ab=ba=a=b=0
for i in range(N):
    s=str(input())

    ab += s.count('AB')

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

if abs(a-b) >= ba:
    d = min(a,b)+ba
else:
    d = (a+b+ba)//2

print(ab+d)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

Submission status: Wrong Answer

Solution code:
n = int(input())
c,x,y,z = 0,0,0,0
for _ in range(n):
    s = input()
    c+=s.count("AB")
    if s[0]=="B" and s[-1]=="A": x+=1
    elif s[-1]=="A": y+=1
    elif s[0]=="B": z+=1
y,z = min(y,z),max(y,z)
if x+y<=z: print(c+x+y)
else: print(c+z+(x+y-z+1)//2)

Submission status: Wrong Answer

Solution code:
n = int(input())
c,x,y,z = 0,0,0,0
for _ in range(n):
    s = input()
    if "AB" in s: c+=1
    if s[0]=="B" and s[-1]=="A": x+=1
    elif s[-1]=="A": y+=1
    elif s[0]=="B": z+=1
y,z = min(y,z),max(y,z)
if x+y<=z: print(c+x+y)
else: print(c+z+(x+y-z+1)//2)

Submission status: Wrong Answer

Solution code:
count = 0
n = int(raw_input())
endA = 0
begB = 0
both = 0
for i in xrange(n):
    word = raw_input()
    for j in xrange(len(word)-1):
        if word[j] == "A" and word[j+1] == "B":
            count += 1
    if word[0] == "B" and word[len(word)-1] == "A":
        both += 1
    elif word[0] == "B":
        begB += 1
    elif word[len(word) - 1] == "A":
        endA += 1

if endA > 0 and both > 0:
    count += 1
    endA -= 1
if begB > 0 and both > 0:
    count += 1
    begB -= 1
count += both - 1
count += min(endA, begB)
print count

Submission status: Wrong Answer

Solution code:
count = 0
n = int(raw_input())
endA = 0
begB = 0
both = 0
for i in xrange(n):
    word = raw_input()
    for j in xrange(len(word)-1):
        if word[j] == "A" and word[j+1] == "B":
            count += 1
    if word[0] == "B" and word[len(word)-1] == "A":
        both += 1
    elif word[0] == "B":
        begB += 1
    elif word[len(word) - 1] == "A":
        endA += 1

if endA > 0:
    count += 1
    endA -= 1
if begB > 0:
    count += 1
    begB -= 1
count += both - 1
count += min(endA, begB)
print count

Submission status: Wrong Answer

Solution code:
#c
n = int(input())
stringsA = 0
stringsB = 0
both = 0
ans = 0
if n == 1:
    string = input()
    print(string.count("AB"))
else:
    for i in range(n):
        string = input()
        ans += string.count("AB")
        if string[-1] == 'A' and string[0] == 'B':
            both += 1
        elif string[-1] == 'A':
            stringsA += 1
        elif string[0] == 'B':
            stringsB += 1
    if both == 0:
      print(ans + min(stringsA,stringsB))
    elif stringsA + stringsB == 0 and both > 1:
      both -= 1
    elif stringsA == stringsB and both == 1:
      both = 0
    else:
      print(ans + min(stringsA,stringsB) + both)

Submission status: Wrong Answer

Solution code:
import sys

def parse(inp):
    n = int(next(inp))
    s = ['' for _ in range(n)]
    for i in range(n):
        s[i] = str(next(inp).replace('\n',''))
    return n,s
  
def main(inp):
    n,s = parse(inp)
    ans = 0
    
    inab = [x.count("AB") for x in s]
    ans += sum(inab)
    
    headB=0
    tailA=0
    BandA=0
    for i in range(n):
        if(s[i][:1] == 'B'): headB += 1
        if(s[i][-1:] == 'A'): tailA += 1
        if(s[i][:1] == 'B' and s[i][-1:] == 'A'): BandA += 1
    ans += min(headB,tailA)
    if(BandA >= 2 and headB == tailA):
        ans -=1
    
    return ans
  
print(main(sys.stdin))

Submission status: Wrong Answer

Solution code:
import sys

def parse(inp):
    n = int(next(inp))
    s = ['' for _ in range(n)]
    for i in range(n):
        s[i] = str(next(inp).replace('\n',''))
    return n,s
  
def main(inp):
    n,s = parse(inp)
    ans = 0
    
    inab = [x.count("AB") for x in s]
    ans += sum(inab)
    
    headB=0
    tailA=0
    BandA=0
    for i in range(n):
        if(s[i][:1] == 'B'): headB += 1
        if(s[i][-1:] == 'A'): tailA += 1
        if(s[i][:1] == 'B' and s[i][-1:] == 'A'): BandA += 1
    ans += min(headB,tailA)
    if(BandA >= 2):
        ans -=1
    
    return ans
  
print(main(sys.stdin))

Submission status: Wrong Answer

Solution code:
import sys

def parse(inp):
    n = int(next(inp))
    s = ['' for _ in range(n)]
    for i in range(n):
        s[i] = str(next(inp).replace('\n',''))
    return n,s
  
def main(inp):
    n,s = parse(inp)
    ans = 0
    
    inab = [x.count("AB") for x in s]
    ans += sum(inab)
    
    headB=0
    tailA=0
    for i in range(n):
        if(s[i][:1] == 'B'): headB += 1
        if(s[i][-1:] == 'A'): tailA += 1
    ans += min(headB,tailA)
    
    return ans
  
print(main(sys.stdin))

Submission status: Wrong Answer

Solution code:
N=int(input())
l=[]
for i in range(N):
    l.append(input())
cnt=cnt_a=cnt_b=count=0
for i in l:
    cnt=cnt+i.count('AB')
    if i[0]=='B' and i[-1]=='A':count+=1
    if i[0]=='B' and i[-1]!='A':cnt_b+=1
    if i[-1]=='A' and i[0]!='B':cnt_a+=1
if N==count:print (cnt+N-1)
else:print (cnt+count+min(cnt_a,cnt_b))

Submission status: Wrong Answer

Solution code:
read = input
n = int(read())
ans = 0
B = 0
A = 0
BA = 0
for i in range(n):
    s = read()
    ans += s.count("AB")
    if s[0] == 'B' and s[-1] == 'A': BA += 1
    elif s[0] == 'B':B += 1
    elif s[-1] == 'A':A += 1
# print(A,B,BA,ans)
# concate BA first, lefting one BA
if BA > 1:
    ans += BA - 1
    BA = 1
# satify BA
# print(A,B,BA,ans)
"""
BA
BA
PA
2
"""
if BA == 1:
    if A >= 1 and B >= 1:
        BA = 0
        A -= 1
        B -= 1
        ans += 2
    elif A >= 0:
        BA = 0
        ans += 1
        A -= 1
        B += 1
    elif B >= 0:
        BA = 0
        ans += 1
        B -= 1
        A += 1
ans += min(A,B)
print(ans)

Submission status: Wrong Answer

Solution code:
read = input
n = int(read())
ans = 0
B = 0
A = 0
BA = 0
for i in range(n):
    s = read()
    ans += s.count("AB")
    if s[0] == 'B' and s[-1] == 'A': BA += 1
    elif s[0] == 'B':B += 1
    elif s[-1] == 'A':A += 1
# print(A,B,BA,ans)
if BA >= max(A , B): # satify BA with A,B, BA>=0 ,then use BA as A or B, satify BA
    ans += 2*min(A,B)
    BA -= min(A,B)
    A -= min(A,B)
    B -= min(A,B)
    if A > B: A,B = B,A # B >=0 , BA >= 0
    # concate BA as BABA, that is count A only
    ans += BA - 1
    # if (BA)+ and B >= 1, (BA)+B
    if BA > 0:
        ans += 1
elif BA < max(A,B) and BA >= min(A,B): # satify BA, BA=0, left A>0,B==0 or A==0,B>0
    # use all min(A, B) to satify BA
    ans += 2*min(A,B)
    BA -= min(A,B)
    A -= min(A,B)
    B -= min(A,B)
    if A > B: A,B = B,A # B >=0 , BA >= 0
    # concate BA as BABA, that is count A only
    ans += BA - 1
    # if (BA)+ and B >= 1, (BA)+B
    if BA > 0:
        ans += 1
    
elif BA < max(A,B) and BA < min(A,B): # satify BA, and have A,B, math AB
    ans += BA * 2
    A -= BA
    B -= BA
    ans += min(A,B)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

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

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