AB Substrings

AtCoder
IDdiverta2019_c
Time2000ms
Memory256MB
Difficulty
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$ [samples]
Samples
Input #1
3
ABCA
XBAZ
BAD
Output #1
2

For example, if we concatenate `ABCA`, `BAD` and `XBAZ` in this order, the resulting string `ABCABADXBAZ` has two occurrences of `AB`.
Input #2
9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA
Output #2
4
Input #3
7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA
Output #3
4
API Response (JSON)
{
  "problem": {
    "name": "AB Substrings",
    "description": {
      "content": "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 ",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "diverta2019_c"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Snuke has $N$ strings. The $i$\\-th string is $s_i$.\nLet us concatenate these strings into one string after arranging them in some order. Find the maximum possible number of occurrences of `AB` in the ...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments