Problem Statement
Snuke has N strings. The i-th string is si.
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≤N≤104
2≤|si|≤10
si consists of uppercase English letters.
Input
Input is given from Standard Input in the following format:
N
s1
\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
题意:找出给出的序列随意组合可以得到的最多的AB的序列的个数。
统计出来以A结尾的字符串个数,以B开头的字符串个数;以及B和A分别在字符串两端的情况个数;处理一下三个数既可以得出结果;
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const int mod=998244353;
int a[1000010];
int main()
{
int n;
cin >>n;
int sum=0;
int a=0,b=0;
int cot=0;
while(n--)
{
string s;
cin >>s;
int len=s.size();
if(s[0]=='B'&&s[len-1]=='A') cot++;
else
{
if(s[0]=='B') b++;
else if(s[len-1]=='A') a++;
}
for(int i=1;i<len;i++)
{
if(s[i]=='B'&&s[i-1]=='A') sum++;
}
}
if(cot>0)
{
if(b==0) sum+=cot-1,b++;
else sum+=cot;
}
sum+=min (a,b);
cout <<sum<<endl;
return 0;
}