AB Substrings AtCoder - 5039

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值