题目:T4- 咕咕东学英语
题意:
输入:
输出:
样例:
数据范围:
解题思路:这个题其实是有规律的,只要在一个字串中,A和B的数量都超过了1,那么这个字串就是合法的,我们就找到那些A数量为1和B数量为1并且不合法的子串就行了。
代码:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
long long n;
cin>>n;
string cnt;
cin>>cnt;
long long anum=0,bnum=0;
long long tot=n*(n-1)/2;
char p=cnt[0];
if(cnt[0]=='A')anum++;//anum代表一个字串中A的数目,
//这个字串由连续的A和B构成,bnum代表B的数目。
//对于这种字串,不合法的字串数目为anum+bnum-1,扫面这样的字串就行了
else bnum++;
for(int i=1;i<cnt.size();i++)
{
if(p!=cnt[i])
{
if(bnum>0&&anum>0)
{
tot-=(anum+bnum-1);
if(cnt[i]=='A')anum=0;//新的字串,进行更新
else bnum=0;
}
p=cnt[i];
}
if(cnt[i]=='A')anum++;
else bnum++;
}
if(bnum>0&&anum>0)//扫到最后没更新,在这里更新一下
{
tot-=(anum+bnum-1);
}
cout<<tot;
}