Problem:


Score : 400 points

Problem Statement

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

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

Constraints

1 \leq N \leq 10^{4}

2 \leq |s_i| \leq 10

s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

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

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4



Sample input:
3
ABCA
XBAZ
BAD


Sample output:
2

Submission status: Wrong Answer

Solution code:
N = int(input())
s = [(input()) for _ in range(N)]
ans = 0
a_cnt = 0
b_cnt = 0
ab_cnt = 0
for i in range(N):
  if s[i][0] == 'B':
    b_cnt += 1
  if s[i][-1] == 'A':
    a_cnt += 1
  if s[i][0] == 'B' and s[i][-1] == 'A':
    ab_cnt += 1
  for j in range(len(s[i])-1):
    if s[i][j] + s[i][j+1] == 'AB':
      ans += 1

if ab_cnt == min(a_cnt, b_cnt):
  print(ans)
  exit()
print(ans + min(a_cnt, b_cnt))

Submission status: Wrong Answer

Solution code:
N = int(input())
s = [(input()) for _ in range(N)]
ans = 0
a_cnt = 0
b_cnt = 0
for i in range(N):
  if s[i][0] == 'B':
    b_cnt += 1
  if s[i][-1] == 'A':
    a_cnt += 1
  for j in range(len(s[i])-1):
    if s[i][j] + s[i][j+1] == 'AB':
      ans += 1

print(ans + min(a_cnt, b_cnt))

Submission status: Wrong Answer

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

in_str = 0
front_B = 0
last_A = 0
for str in s:
    in_str += str.count('AB')
    if str[0] == 'B':
        front_B += 1
    if str[-1] == 'A':
        last_A += 1
# print(in_str, front_B, last_A)
ans = in_str + min(N - 1, min(front_B, last_A))
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a_num=0
b_num=0
set_num=0
ans=0
for i in range(n):
    s=list(input())
    for j in range(len(s)-1):
        if s[j]+s[j+1]=="AB":
            ans+=1
    if s[0]=="B" and s[-1]=="A":
        set_num+=1
    elif s[0]=="B":
        b_num+=1
    elif s[-1]=="A":
        a_num+=1
if set_num==0:
    print(ans+min(a_num,b_num))
else:
    ans+=set_num-1
    if 0<a_num:
        a_num+=1
    if 0<b_num:
        b_num+=1
    print(ans+min(a_num,b_num))

Submission status: Wrong Answer

Solution code:
n=int(input())
a_num=0
b_num=0
set_num=0
ans=0
for i in range(n):
    s=list(input())
    for j in range(len(s)-1):
        if s[j]+s[j+1]=="AB":
            ans+=1
    if s[0]=="B" and s[-1]=="A":
        set_num+=1
    elif s[0]=="B":
        b_num+=1
    elif s[-1]=="A":
        a_num+=1
if set_num==0:
    print(ans+min(a_num,b_num))
else:
    ans+=set_num-1
    print(ans+min(a_num+1,b_num+1))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
A=0
B=0
C=0
ans=0
for _ in range(0,N):
    s=input()
    if s[0]=='B':
        B+=1
    if s[-1]=='A':
        A+=1
    if s[-1]=='A' and s[0]=='B':
        C+=1

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

if A!=C or B!=C:
    print(ans+min(A,B))
else:
    print(ans+C-1)

Submission status: Wrong Answer

Solution code:
import sys
N = int(sys.stdin.readline())
ss = []
for i in range(N):
    ss.append(sys.stdin.readline().rstrip())

incount = 0
has_ba = 0
has_b = 0
has_a = 0
for s in ss:
    incount += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        has_ba += 1
    elif s[0] == 'B':
        has_b += 1
    elif s[-1] == 'A':
        has_a += 1

res = incount
if has_ba:
    res += has_ba - 1
    has_ba = 1
if has_b or has_a:
    npair = min(has_b, has_a)
    res += npair
    has_b -= npair
    has_a -= npair
if has_ba and (has_b or has_a):
    res += 1

print(res)

Submission status: Wrong Answer

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

incount = 0
has_ba = 0
has_b = 0
has_a = 0
for s in ss:
    incount += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        has_ba += 1
    elif s[0] == 'B':
        has_b += 1
    elif s[-1] == 'A':
        has_a += 1

res = incount
if has_ba:
    res += has_ba - 1
    has_ba = 1
if has_b or has_a:
    npair = min(has_b, has_a)
    res += npair
    has_b -= npair
    has_a -= npair
if has_ba and (has_b or has_a):
    res += 1

print(res)

Submission status: Wrong Answer

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

incount = 0
has_ba = 0
has_b = 0
has_a = 0
for s in ss:
    incount += s.count('AB')
    if s.startswith('B') and s.endswith('A'):
        has_ba += 1
    elif s.startswith('B'):
        has_b += 1
    elif s.endswith('A'):
        has_a += 1

res = incount
if has_ba:
    res += has_ba - 1
    has_ba = 1
if has_b or has_a:
    npair = min(has_b, has_a)
    has_b -= npair
    has_a -= npair
    res += npair
if has_ba and(has_b or has_a):
    res += 1
print(res)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[ str(input()) for _ in range(n)]
ans=0
a=0
b=0
ab=0
for i in range(n):
    for j in range(len(s[i])-1):
        if s[i][j:j+2]=="AB":
            ans+=1
#print(ans)
for i in range(n):
    if s[i][0]=="B" and s[i][-1]=="A":
        ab+=1
    elif s[i][0]=="B":
        b+=1
    elif s[i][-1]=="A":
        a+=1

ma=max(a,b)
mi=min(a,b)
if 1<=a:
    ans+=ab
else:
    a+=1
    ab-=1
    ans+=ab
ans+=min(a,b)

#print(a,b,ab)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=[ str(input()) for _ in range(n)]
ans=0
a=0
b=0
ab=0
for i in range(n):
    for j in range(len(s[i])-1):
        if s[i][j:j+2]=="AB":
            ans+=1
#print(ans)
for i in range(n):
    if s[i][0]=="B" and s[i][-1]=="A":
        ab+=1
    elif s[i][0]=="B":
        b+=1
    elif s[i][-1]=="A":
        a+=1

ma=max(a,b)
mi=min(a,b)
if a<=1:
    ans+=ab
else:
    a+=1
    ab-=1
    ans+=ab
ans+=min(a,b)


#print(a,b,ab)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
for i in range(n):
    s.append(input())
out = 0
a=0
b=0
c=0
for i in range(len(s)):
    if s[i][0] == "B" and s[i][len(s[i])-1] == "A":
        a=a+1
    elif s[i][0] == "B" and s[i][len(s[i])-1] != "A":
        b=b+1
    elif s[i][0] != "B" and s[i][len(s[i])-1] == "A":
        c=c+1
    for j in range(len(s[i])-1):
        if s[i][j] == "A" and s[i][j+1] == "B":
            out = out+1
if b == 0 and c == 0:
    out = out + a -1
else :
    out = out +a+ min(b,c)
    
print(int(out))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
for i in range(n):
    s.append(input())
out = 0
a=0
b=0
c=0
for i in range(len(s)):
    if s[i][0] == "B" and s[i][len(s[i])-1] == "A":
        a=a+1
    elif s[i][0] == "B":
        b=b+1
    elif s[i][len(s[i])-1] == "A":
        c=c+1
    for j in range(len(s[i])-1):
        if s[i][j] == "A" and s[i][j+1] == "B":
            out = out+1
if b == 0 and c == 0:
    out = out + a -1
else :
    out = out +a+ min(b,c)
    
print(int(out))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
for i in range(n):
    s.append(input())
out = 0
la = []
lb = []
for i in range(len(s)):
    if s[i][0] == "B":
        la.append(i)
    if s[i][len(s[i])-1] == "A":
        lb.append(i)
    for j in range(len(s[i])-1):
        if s[i][j] == "A" and s[i][j+1] == "B":
            out = out+1
if la == lb:
    out = out + len(la)
else :
    out = out + min(len(la),len(lb))
    
print(int(out))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
a = 0
b = 0
ba = 0
for i in range(n):
    s = input()
    ans += s.count("AB")
    if s[-1] == "A":
        if s[0] == "B":
            ba += 1
        a += 1
    if s[0] == "B":
        b += 1
if a > b:
    ans += b
elif b > a:
    ans += a
elif a == ba:
    ans += a-1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
ans = 0
ba, nba, bna = 0, 0, 0
for i in range(N):
  ans += S[i].count("AB")
  if S[i][0] != "B" and S[i][-1] == "A":
    nba += 1
  elif S[i][0] == "B" and S[i][-1] == "A":
    ba += 1
  elif S[i][0] == "B" and S[i][-1] != "A":
    bna += 1
if ba <= min(nba, bna):
  ans += min(nba, bna) + ba
else:
  ans += max(nba, bna)
  ba -= max(nba, bna) - min(nba, bna)
  ans += ba
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [input() for _ in range(N)]
ans = 0
ba, nba, bna = 0, 0, 0
for i in range(N):
  ans += S[i].count("AB")
  if S[i][0] != "B" and S[i][-1] == "A":
    nba += 1
  elif S[i][0] == "B" and S[i][-1] == "A":
    ba += 1
  elif S[i][0] == "B" and S[i][-1] != "A":
    bna += 1
if ba <= min(nba, bna):
  ans += min(nba, bna) + ba
else:
  ans += max(nba, nba)
  ba -= max(nba, nba) - min(nba, nba)
  ans += ba - 1
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
count=0
LastA=0
FirstB=0
LA=0
for i in range(0,N):
    f=input()
    count=count+f.count('AB')
    if f[0]=='B' and f[-1]=='A':
        LA=LA+1
    elif f[0]=='B':
        FirstB=FirstB+1
    elif f[-1]=='A':
        LastA=LastA+1
count=count+LA
if LastA==0 and FirstB==0:
    count=count-1
elif FirstB>=LastA:
    count=count+LastA
else:
    count=count+FirstB
print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
count=0
LastA=0
FirstB=0
LA=0
for i in range(0,N):
    f=input()
    count=count+f.count('AB')
    if f[0]=='B' and f[-1]=='A':
        LA=LA+1
    elif f[0]=='B':
        FirstB=FirstB+1
    elif f[-1]=='A':
        LastA=LastA+1
if LA>0:
    count=count+LA
if LastA==0 and FirstB==0:
    count=count-1
elif FirstB>=LastA:
    count=count+LastA
else:
    count=count+FirstB
print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
count=0
LastA=0
FirstB=0
LA=0
for i in range(0,N):
    f=input()
    count=count+f.count('AB')
    if f[0]=='B' and f[-1]=='A':
        LA=LA+1
    elif f[0]=='B':
        FirstB=FirstB+1
    elif f[-1]=='A':
        LastA=LastA+1
if LA>0:
    count=count+LA-1
    FirstB=FirstB+1
    LastA=LastA+1
if FirstB>=LastA:
    count=count+LastA
else:
    count=count+FirstB
print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
count=0
LastA=0
FirstB=0
LA=0
for i in range(0,N):
    f=input()
    count=count+f.count('AB')
    f=f.replace('AB','')
    if f[0]=='B' and f[-1]=='A':
        LA=LA+1
    elif f[0]=='B':
        FirstB=FirstB+1
    elif f[-1]=='A':
        LastA=LastA+1
if LA>0:
    count=count+LA-1
    FirstB=FirstB+1
    LastA=LastA+1
if FirstB>=LastA:
    count=count+LastA
else:
    count=count+FirstB
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
s= [input() for i in range(n)]
ans1=0
firstB=0
lastA=0
m=n
for i in s:
    ans1+=i.count("AB")
    if i[0]=="B":
        firstB+=1
    if i[-1]=="A":
        lastA+=1
    if i[0]!="B" and i[-1]!="A":
        m-=1
if min(firstB,lastA)==m:
    print(ans1+m-1)
else:
    print(ans1+min(firstB,lastA))

Submission status: Wrong Answer

Solution code:
n = int(input())
s= [input() for i in range(n)]
ans1=0
firstB=0
lastA=0
same=0
for i in s:
    ans1+=i.count("AB")
    if i[0]=="B":
        firstB+=1
    if i[-1]=="A":
        lastA+=1
    if i[0]=="B" and i[-1]=="A":
        same+=1
if same==min(firstB,lastA):
    print(ans1+same-1)
else:
    print(ans1+min(firstB,lastA))

Submission status: Wrong Answer

Solution code:
n = int(input())
s= [input() for i in range(n)]
ans1=0
firstB=0
lastA=0
for i in s:
    ans1+=i.count("AB")
    if i[0]=="B":
        firstB+=1
    if i[-1]=="A":
        lastA+=1
if min(firstB,lastA)==n:
    print(ans1+n-1)
else:
    print(ans1+min(firstB,lastA))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = []
ans = 0
fin_with_a = 0
start_with_b = 0
for _ in range(n):
    tmp = input()
    s.append(tmp[0]+tmp[-1])
    for i in range(len(tmp)-1):
        if tmp[i:i+2] ==  'AB':
            ans += 1
    if tmp[0] == 'B' and tmp[-1] == 'A':
        flaga = True
        flagb = True
        if fin_with_a > 0:
            ans += 1
            fin_with_a -= 1
            flaga = False
        if start_with_b > 0:
            ans += 1
            start_with_b -= 1
            flagb = False
        if flaga:
            start_with_b += 1
        if flagb:
            fin_with_a += 1
    elif tmp[0] == 'B':
        if fin_with_a > 0:
            ans += 1
            fin_with_a -= 1
        else:
            start_with_b += 1
    elif tmp[-1] == 'A':
        if start_with_b > 0:
            ans += 1
            start_with_b -= 1
        else:
            fin_with_a += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = [0]* N
Middle = 0
Bcount = 0
Acount = 0
ABcount = 0
ans = 0
for i in range(N):
    s[i] = input()
    if s[i][0] == "B" and s[i][-1] == "A":
        ABcount += 1
    elif s[i][0] == "B":
        Bcount += 1
    elif s[i][-1] == "A":
        Acount += 1
    for j in range(len(s[i])-1):
        if s[i][j] == "A" and s[i][j+1] == "B":
            Middle += 1
if Acount == 0 and Bcount==0:
    ans = Middle + ABcount - 1
elif Acount == 0:
    ans = Middle + ABcount
elif Bcount == 0:
    ans = Middle + ABcount
else:
    ans = Middle + ABcount +1 + min(Acount-1,Bcount-1)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
xx = 0
xA = 0
Bx = 0
BA = 0
A = 0
B = 0
for i in range(N):
  S = input()
  for i in range(len(S) - 1):
    if S[i] == "A" and S[i + 1] == "B":
      ans += 1
  if S[0] == "B":
    if S[-1] == "A":BA += 1
    else:Bx += 1
  else:
    if S[-1] == "A":xA += 1
    else:xx += 1
if xx + xA == 0:
  A = BA + xA - 1
else:
  A = BA + xA
if xx + Bx == 0:
  B = BA + Bx - 1
else:
  B = BA + Bx
ans += min(A,B)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
xx = 0
xA = 0
Bx = 0
BA = 0
for i in range(N):
  S = input()
  for i in range(len(S) - 1):
    if S[i] == "A" and S[i + 1] == "B":
      ans += 1
  if S[0] == "B":
    if S[-1] == "A":BA += 1
    else:Bx += 1
  else:
    if S[-1] == "A":xA += 1
    else:xx += 1
if BA == N:
  ans += N -1
else:
  ans += min(BA + Bx,xA + BA)
print(ans)

Submission status: Wrong Answer

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

ans = 0
a = 0
b = 0
c = 0
for i in range(n):
    for j in range(len(S[i]) - 1):
        if S[i][j:j+2] == "AB":
            ans += 1
    if S[i][-1] == "A":
        a += 1
    if S[i][0] == "B":
        b += 1
    if S[i][-1] == "A" and S[i][0] == "B":
        c += 1
# print(ans)
ans += min(a, b)
if a == c and a == b:
    ans -= 1

print(ans)

Submission status: Wrong Answer

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

ans = 0
a = 0
b = 0
c = 0
for i in range(n):
    for j in range(len(S[i]) - 1):
        if S[i][j:j+2] == "AB":
            ans += 1
    if S[i][-1] == "A":
        a += 1
    if S[i][0] == "B":
        b += 1
    if S[i][-1] == "A" and S[i][0] == "B":
        c += 1
# print(ans)
ans += min(a, b)
if a == c:
    ans -= 1

print(ans)

Submission status: Wrong Answer

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

ans = 0
a = 0
b = 0
for i in range(n):
    for j in range(len(S[i]) - 1):
        if S[i][j:j+2] == "AB":
            ans += 1
    if S[i][-1] == "A":
        a += 1
    if S[i][0] == "B":
        b += 1
# print(ans)
ans += min(a, b, n-1)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/diverta2019/tasks/diverta2019_c

import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

# greedyっぽいなぁ
# ABとなる部分を最大化したい
# *B → A*はむだ、*A→B*となる文字列はそのように連結させたほうがよい
# つまり新たにAB文字列になるのはmin(*Aの数,B*の数)
# ただしB*Aを満たす文字列に関してはどちらにもなりうるので、好きにpaddingできる

N = int(input())
cnt = end_A = start_B = both = 0
for _ in ra(N):
    s = input()
    cnt += s.count('AB')

    if s.endswith('A') and s.startswith('B'):
        both += 1
    elif s.endswith('A'):
        end_A += 1
    elif s.startswith('B'):
        start_B += 1

# print(end_A, start_B, both)
# min(end_A,start_B)のセットは作れる
# abs(end_A-start_B)あまる
# bothでboth-1個のABが作れる。またA*Bができる
# あまってたらA*Bでもう一個作れるよね？というアイデア

add = min(start_B, end_A)
if both > 0:
    add += both - 1 + (abs(end_A - start_B) > 0)

# print(cnt, add)
print(cnt + add)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/diverta2019/tasks/diverta2019_c

import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

# greedyっぽいなぁ
# ABとなる部分を最大化したい
# *B → A*はむだ、*A→B*となる文字列はそのように連結させたほうがよい
# つまり新たにAB文字列になるのはmin(*Aの数,B*の数)
# ただしB*Aを満たす文字列に関してはどちらにもなりうるので、好きにpaddingできる

N = int(input())
cnt = end_A = start_B = both = 0
for _ in ra(N):
    s = input()
    cnt += s.count('AB')

    if s.endswith('A') and s.startswith('B'):
        both += 1
    elif s.endswith('A'):
        end_A += 1
    elif s.startswith('B'):
        start_B += 1

# print(end_A, start_B, both)
# min(end_A,start_B)のセットは作れる
# abs(end_A-start_B)あまる
# bothでboth-1個のABが作れる。またA*Bができる
# あまってたらA*Bでもう一個作れるよね？というアイデア

add = min(start_B, end_A) + max(both - 1, 0) + (abs(end_A - start_B) > 0)

# print(cnt, add)
print(cnt + add)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/diverta2019/tasks/diverta2019_c

import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

# greedyっぽいなぁ
# ABとなる部分を最大化したい
# *B → A*はむだ、*A→B*となる文字列はそのように連結させたほうがよい
# つまり新たにAB文字列になるのはmin(*Aの数,B*の数)
# ただしB*Aを満たす文字列に関してはどちらにもなりうるので、好きにpaddingできる

N = int(input())
cnt = end_A = start_B = both = 0
for _ in ra(N):
    s = input()
    cnt += s.count('AB')

    if s.endswith('A') and s.startswith('B'):
        both += 1
    elif s.endswith('A'):
        end_A += 1
    elif s.startswith('B'):
        start_B += 1

# print(end_A, start_B, both)
# min(end_A,start_B)のセットは作れる
# abs(end_A-start_B)あまる
# bothでboth//2個のABが作れる。またA*Bができる
# あまってたらA*Bでもう一個作れるよね？というアイデア

add = min(start_B, end_A) + both // 2 + (abs(end_A - start_B) > 0)

print(cnt + add)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/diverta2019/tasks/diverta2019_c

import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

# greedyっぽいなぁ
# ABとなる部分を最大化したい
# *B → A*はむだ、*A→B*となる文字列はそのように連結させたほうがよい
# つまり新たにAB文字列になるのはmin(*Aの数,B*の数)
# ただしB*Aを満たす文字列に関してはどちらにもなりうるので、好きにpaddingできる

N = int(input())
cnt = end_A = start_B = both = 0
for _ in ra(N):
    s = input()
    cnt += s.count('AB')

    if s.endswith('A') and s.startswith('B'):
        both += 1
    elif s.endswith('A'):
        end_A += 1
    elif s.startswith('B'):
        start_B += 1

# print(end_A, start_B, both)
# min(end_A,start_B)のセットは作れる
# abs(end_A-start_B)についてbothで埋められる分は埋める
# bothで埋められない分は//2

add = min(start_B, end_A) + min(abs(start_B - end_A), both)
if both > abs(start_B - end_A):  # 埋めて余る分
    add += (both - abs(start_B - end_A) + 1) // 2


# print(cnt, add)
print(cnt + add)

Submission status: Wrong Answer

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

la = 0
fb = 0
ab = 0
for s in S:
    if "AB" in s[1:-1]:
        ab += 1
    if s[0] == 'B':
        fb += 1
    if s[-1] == 'A':
        la += 1

print(min(fb, la) + ab)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/diverta2019/tasks/diverta2019_c

import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

# greedyっぽいなぁ
# ABとなる部分を最大化したい
# *B → A*はむだ、*A→B*となる文字列はそのように連結させたほうがよい
# つまり新たにAB文字列になるのはmin(*Aの数,B*の数)

N = int(input())
cnt = end_A = start_B = 0
for _ in ra(N):
    s = input()
    cnt += s.count('AB')
    end_A += s.endswith('A')
    start_B += s.startswith('B')
print(cnt + min(start_B, end_A))

Submission status: Wrong Answer

Solution code:
def count_AB(s):
    res = 0
    for j in range(len(s) - 1):
        if s[j] == 'A' and s[j + 1] == 'B':
            res += 1
    return res


N = int(input())
sA, BsA, Bs = (0, 0, 0)
ans = 0
for i in range(N):
    s = input()
    if s[0] == 'B' and s[-1] == 'A':
        BsA += 1
        if len(s[1:-1]) > 0:
            ans += count_AB(s[1:-1])
    elif s[0] == 'B':
        Bs += 1
        if len(s[1:]) > 0:
            ans += count_AB(s[1:])
    elif s[-1] == 'A':
        sA += 1
        if len(s[:-1]) > 0:
            ans += count_AB(s[:-1])
    else:
        ans += count_AB(s)

ans += BsA
ans += min(sA, Bs)
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
s=[input() for _ in range(N)]
abcount=0
righta=0
leftb=0
bothab=0
buf=""
for i in range(N):
    buf=s[i]
    l=len(buf)
    if (buf[0]=="B")and(buf[l-1]=="A"):
        bothab=bothab+1
    elif (buf[0]=="B"):
        leftb=leftb+1
    elif (buf[l-1]=="A"):
        righta=righta+1
    for j in range(l-1):
        if (buf[j]=="A")and(buf[j+1]=="B"):
            abcount=abcount+1

abcount=abcount+min(bothab,righta,leftb)*2
#print(bothab,righta,leftb,abcount)
if min(bothab,righta,leftb)==bothab:
    leftb=leftb-bothab
    righta=righta-bothab
    abcount=abcount+min(leftb,righta)
elif min(bothab,righta,leftb)==righta:
    bothab=bothab-righta
    leftb=leftb-righta
    abcount=abcount+max(0,bothab-1)
    if leftb>0:
        abcount=abcount+1
else:
    bothab=bothab-leftb
    righta=righta-leftb
    abcount=abcount+max(0,bothab-1)
    if righta>0:
        abcount=abcount+1
print(abcount)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in range(n)]
ab = 0
ba = 0
last_a = 0
first_b = 0
for i in range(n):
  if s[i][0] == "B" and s[i][-1] == "A":
    ba += 1
  elif s[i][-1] == "A":
    last_a += 1
  elif s[i][0] == "B":
    first_b += 1
  ab += s[i].count("AB")
if last_a == 0 and first_b == 0:
  print(ab+ba-1)
else:
  print(ab+ba+min(last_a,first_b))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in range(n)]
ab = 0
ba = 0
last_a = 0
first_b = 0
for i in range(n):
  if s[i][0] == "B" and s[i][-1] == "A":
    ba += 1
  elif s[i][-1] == "A":
    last_a += 1
  elif s[i][0] == "B":
    first_b += 1
  ab += s[i].count("AB")
if last_a == 0 and first_b == 0:
  print(ab+ba-1)
else:
  print(ab+min(ba+last_a,ba+first_b))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [input() for _ in range(n)]
ab = 0
last_a = 0
first_b = 0
for i in range(n):
  if s[i][0] == "B":
    first_b += 1
  if s[i][-1] == "A":
    last_a += 1
  ab += s[i].count("AB")
if last_a == n and first_b == n:
  print(ab+n-1)
else:
  print(ab+min(last_a,first_b))

Submission status: Wrong Answer

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

sm=0

for i in range(N):
    count=0
    tmp=-1
    for j in range(len(s[i])):
        if s[i][j]=="A":
            tmp=1
        elif s[i][j]=="B":
            if tmp==1:
                count+=1
            tmp=-1
        else:
            tmp=-1
    sm+=count
chk=[0 for _ in range(N)]
for i in range(N):
    if s[i][0]=="B" and s[i][-1]=="A":
        chk[i]=3
    elif s[i][-1]=="A":
        chk[i]=2
    elif s[i][0]=="B":
        chk[i]=1
x=0
y=0
z=0
#print(sm)
ans=sm
for i in range(N):
    if chk[i]==3:
        z+=1
    if chk[i]==2:
        x+=1
    if chk[i]==1:
        y+=1
if z>0:
    a=z//2
    ans+=a
    if x>0:
        ans+=1
        x-=1
    if y>0:
        ans+=1
        y-=1
ans+=min(x,y)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n = int(readline())
cnt_a = 0
cnt_b = 0
cnt = 0
check = 0
memo = 0
for i in range(n):
    check = 0
    s = input()
    for j in range(1, len(s)):
        if s[j - 1] == 'A' and s[j] == 'B':
            cnt += 1
    if s[0] == 'B':
        cnt_b += 1
        check += 1
    if s[-1] == 'A':
        cnt_a += 1
        check += 1
    if check == 2:
        memo += 1
        cnt_a -= 1
        cnt_b -= 1
if cnt_a > cnt_b:
    cnt_b += memo
else:
    cnt_a += memo
print(cnt + min(cnt_a, cnt_b))

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n = int(readline())
cnt_a = 0
cnt_b = 0
cnt = 0
for i in range(n):
    s = input()
    for j in range(1, len(s)):
        if s[j - 1] == 'A' and s[j] == 'B':
            cnt += 1
    if s[0] == 'B':
        cnt_b += 1
    if s[-1] == 'A':
        cnt_a += 1
print(cnt + min(cnt_a, cnt_b))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
import re
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    s = SR(n)
    a = 0
    b = 0
    ab = 0
    ans = 0
    for si in s:
        if si[0] == "B" and si[-1] == "A":
            ab += 1
        elif si[0] == "B":
            b += 1
        elif si[-1] == "A":
            a += 1
        ans += si.count("AB")
    ans += ab - 1
    if ab:
        ans += (a > 0) + (b > 0)
        a -= a > 0
        b -= b > 0
    ans += min(a, b)
    print(ans)

    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
import re
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    s = SR(n)
    a = 0
    b = 0
    ab = 0
    ans = 0
    for si in s:
        if si[0] == "B" and si[-1] == "A":
            ab += 1
        elif si[0] == "B":
            b += 1
        elif si[-1] == "A":
            a += 1
        ans += si.count("AB")
    ans += ab - 1
    if a and b and ab:
        ans += 2
        a -= 1
        b -= 1
    ans += min(a, b)
    print(ans)

    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


def main():
    ans = 0
    A = []
    B = []
    N = int(input())

    for _ in range(N):
        s = input()
        l = len(s)
        ans += s.count("AB")
        if s[0] == "B":
            B.append(1)
        else:
            B.append(0)
        if s[-1] == "A":
            A.append(1)
        else:
            A.append(0)
    if A == B:
        ans += sum(A) - 1
    else:
        ans += min(sum(A), sum(B))
    return ans


if __name__ == "__main__":
    ans = main()
    print(ans)