题解
查看原帖
题解
1761076
TYQ321HF楼主2025/6/29 10:13
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=500001;
int n,l=1,r=1,ans,LG[N],RG[N],LH[N],RH[N];
string s;
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> s;s=" "+s;
	for(int i=2;i<=n;i++)
		LG[i]=s[i-1]=='G'?LG[i-1]+1:0;
	for(int i=n-1;i>=1;i--)
		RG[i]=s[i+1]=='G'?RG[i+1]+1:0;
	for(int i=2;i<=n;i++)
		LH[i]=s[i-1]=='H'?LH[i-1]+1:0;
	for(int i=n-1;i>=1;i--)
		RH[i]=s[i+1]=='H'?RH[i+1]+1:0;
	for(int i=1;i<=n;i++)
	{
		if(s[i]=='H')
		{
			if(LG[i])ans+=LG[i-1];
			if(RG[i])ans+=RG[i+1];
			ans+=LG[i]*RG[i];
		}
		else
		{
			if(LH[i])ans+=LH[i-1];
			if(RH[i])ans+=RH[i+1];
			ans+=LH[i]*RH[i];
		}
//		cout << ans << endl;
	}
	cout << ans << endl;
	return 0;
}
2025/6/29 10:13
加载中...