60分求助!(超时)
查看原帖
60分求助!(超时)
996300
CMY2013楼主2024/4/5 20:29
#include<bits/stdc++.h>
using namespace std;
string s;
int n,ans=0;
bool f[2000000];
stack<char> c;

int main()
{
	cin>>n;
	cin>>s;
	memset(f, 0, sizeof(f));
	for(int i=0;i<n;i++)
	{
		int cnt=0;
		while(!c.empty())
		{
			c.pop();
		}
		if(f[i]) continue;
		for(int j=i;j<n;j++)
		{
			if(c.empty())
			{
				ans+=cnt;
				cnt++;
				c.push(s[j]);
				f[j]=1;
				//cout<<ans<<' '<<cnt<<' '<<i<<' '<<j<<endl;
			}
			else 
			{
				if(s[j]==c.top()) c.pop();
				else c.push(s[j]);
			}
		}
		if(c.empty())
		{
			ans+=cnt;
			cnt++;
		}
	}
	cout<<ans;
	return 0;
}
2024/4/5 20:29
加载中...