C - AB Substrings

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.

Constraints1≤N≤1042≤|si|≤10si consists of uppercase English letters.
Input
Input is given from Standard Input in the following format:

N s1。。。sN

Output
Print the answer.

Sample Input 1
Copy
3
ABCA
XBAZ
BAD
Sample Output 1
Copy
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
Copy
9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA
Sample Output 2
Copy
4
Sample Input 3
Copy
7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA
Sample Output 3
Copy
4

这个思路叭,可以说是最蠢的了,就是把字符串按规则连起来,暴力
容易超时
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<string>
#define ll long long
#define dd double
using namespace std;
 
int main(){
	string s[10005];
	string st="";
	ll a[10005][3]={0};
	memset(a,0,sizeof(a));
	ll n;cin>>n;
	for(ll i=0;i<n;i++){
		cin>>s[i];
		if(s[i][0]=='B'){
			a[i][0]++;
		}
		if(s[i][s[i].size()-1]=='A'){
			a[i][1]++;
		}
	}
	ll flag=0;
	for(ll i=0;i<n;i++){
		if(a[i][0]==0&&a[i][1]!=0){
			flag=1;
			st+=s[i];
			a[i][2]=-1;
			break;
		}
	}
	ll q=0;
	if(flag==1){
		for(ll i=0;i<n;i++){
			if(a[i][2]==-1){
				continue;
			}
			else{
				if(a[i][0]!=0&&a[i][1]!=0){
					st+=s[i];
					a[i][2]=-1;
				}
			}
		}
		for(ll i=0;i<n;i++){
			if(a[i][2]==-1){
				continue;
			}
			else{
				if(a[i][0]!=0){
					st+=s[i];
					a[i][2]=-1;
					break;
				}
			}
		}
		for(ll i=0;i<n;i++){
			if(a[i][2]==-1){
				continue;
			}
			else{
				if(q%2==0){
					if(a[i][1]!=0){
						st+=s[i];
						a[i][2]=-1;
						i=-1;
						q++;
					}
				}
				else{
					if(a[i][0]!=0){
						st+=s[i];
						a[i][2]=-1;
						i=-1;
						q++;
					}
				}
			}
		}
		for(ll i=0;i<n;i++){
			if(a[i][2]!=-1){
				st+=s[i];
				a[i][2]=-1;
			}
		}
	}
	if(flag==0){
		for(ll i=0;i<n;i++){
			if(a[i][1]!=0&&a[i][2]!=-1){
				st+=s[i];
				a[i][2]=-1;
				break;
			}
		}
		for(ll i=0;i<n;i++){
			if(a[i][2]==-1){
				continue;
			}
			else{
				if(a[i][0]!=0&&a[i][1]!=0){
					st+=s[i];
					a[i][2]=-1;
				}
			}
		}
		for(ll i=0;i<n;i++){
			if(a[i][2]==-1){
				continue;
			}
			else{
				if(a[i][0]!=0){
					st+=s[i];
					a[i][2]=-1;
				}
			}
		}
		for(ll i=0;i<n;i++){
			if(a[i][2]!=-1){
				st+=s[i];
				a[i][2]=-1;
			}
		}
	}
	ll count = 0;
	for(ll i=0;i<st.size()-1;i++){
		if(st[i]=='A'&&st[i+1]=='B'){
			count++;
		}
	}
	cout<<count<<endl;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值