C - Pluses and Minuses

题解:这个题目暴力模拟的话肯定会超时啊。
找一下规律,以免放置他重复的进行某些算过的数字,看代码应该可以懂。
就只有一个if语句。

/*Keep on going Never give up*/
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
const int maxn = 2e5+10;
const int MaxN = 0x3f3f3f3f;
const int MinN = 0xc0c0c00c;
typedef long long ll;
const int mod = 100000000;
using namespace std;
string s;
int main()
{
    int t;
    cin>>t;
    while(t--){
        cin>>s;
        ll ans=0,cnt=0,x=0,last=0;
        for(int i=0;i<s.size();i++){
            ans++;
            if(s[i]=='+') cnt++;
            else cnt--;
            if(cnt<0){
                cnt=0;
                ll t=ans;
                ans+=ans-last;
                last=t;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}