C. Pluses and Minuses 【1300 / 思维 前缀和】

在这里插入图片描述
https://codeforces.com/problemset/problem/1373/C

#include<bits/stdc++.h> 
using namespace std;
typedef long long int LL;
const int N=1e6+10;
LL s[N];
void solve(string ss)
{
    
    
	LL res=0,n=ss.size();
	for(int i=0;i<n;i++) 
	{
    
    
		if(ss[i]=='+') s[i+1]=s[i]+1;
		else s[i+1]=s[i]-1;
	}
	int pos=0;//s[0]可以当成一个哨兵,因为必定会执行一次 
	for(int i=0;i<=n;i++)
	{
    
    
		while(s[pos]+i>=0&&pos<n) pos++;
		res+=pos;
		if(s[pos]+i>=0) break;
	}
	cout<<res<<endl;
}
int main(void)
{
    
    
	int t; cin>>t;
	while(t--)
	{
    
    
		string s; cin>>s;
		solve(s);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/bettle_king/article/details/121306629
今日推荐