1373C - Pluses and Minuses - CodeForces Solution


math *1300

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    s = input()
    A = []
    cnt = 0
    for c in s:
        A.append(cnt)
        if c=='+':
            cnt-=1
        else:
            cnt+=1
    A.append(cnt)

    m1 = max(A)
    ans = 0
    cnt = 0
    for i in range(len(s)):
        cnt = max(cnt, A[i])
        ans += m1-cnt+1

    print(ans)

C++ Code:

#include <bits/stdc++.h>
using namespace std;

// Aliases
using ll = long long;
using ull = unsigned long long;
using ld = long double;

 
// Constants
constexpr ll INF = 4e18;
constexpr ld EPS = 1e-9;
constexpr ll MOD = 1e9 + 7;
 
// Macros
#define F first
#define S second
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)
typedef vector<int> vi;
typedef pair<int,int> pi;
#define pb push_back
#define MP make_pair
#define REP(i,a,b) for (int i = a; i < b; i++)

const ll mod = 1e9 + 7;
 
ll inv(ll i) {if (i == 1) return 1; return (mod - ((mod / i) * inv(mod % i)) % mod) % mod;}
 
ll mod_mul(ll a, ll b) {a = a % mod; b = b % mod; return (((a * b) % mod) + mod) % mod;}
 
ll mod_add(ll a, ll b) {a = a % mod; b = b % mod; return (((a + b) % mod) + mod) % mod;}

ll mod_sub(ll a, ll b) {a = a % mod; b = b % mod; return (((a - b + mod) % mod) + mod) % mod;}
  
ll ceil_div(ll a, ll b) {return a % b == 0 ? a / b : a / b + 1;}
 
ll pwr(ll a, ll b) {a %= mod; ll res = 1; while (b > 0) {if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1;} return res;}

template <typename T> // cin >> vector<T>
istream &operator>>(istream &istream, vector<T> &v)
{
    for (auto &it : v)
        cin >> it;
    return istream;
}
template <typename T> // cout << vector<T>
ostream &operator<<(ostream &ostream, const vector<T> &c)
{
    for (auto &it : c)
        cout << it << " ";
    return ostream;
}

// Mathematical functions
int GCD(int a, int b)
{
    while (b)
    {
        a %= b;
        swap(a, b);
    }
    return a;
}
 
int GCD_extended(int a, int b, int &x, int &y)
{
    x = 1, y = 0;
    int x1 = 0, y1 = 1, a1 = a, b1 = b;
    while (b1)
    {
        int q = a1 / b1;
        tie(x, x1) = make_tuple(x1, x - q * x1);
        tie(y, y1) = make_tuple(y1, y - q * y1);
        tie(a1, b1) = make_tuple(b1, a1 - q * b1);
    }
    return a1;
}
int LCM(int a, int b)
{
    return ((ll)a * b) / GCD(a, b);
}
 
ll modpow(ll x, ll n, int m = MOD)
{
    if (x == 0 && n == 0)
        return 0; // undefined case
    ll res = 1;
    while (n > 0)
    {
        if (n % 2)
            res = (res * x) % m;
        x = (x * x) % m;
        n /= 2;
    }
    return res;
}
 
int modinv(int x, int m = MOD)
{
    return modpow(x, m - 2, m);
}
 
mt19937 rng;
int getRandomNumber(int l, int r)
{
    uniform_int_distribution<int> dist(l, r);
    return dist(rng);
}
bool cmp(pair<ll, ll>& a,
         pair<ll, ll>& b)
{
    return a.first < b.first;
}
double factorial(int n) {
      if(n == 0)
      return 1;
    double factorial = 1;
    for (double i = 2; i <= n; i++)
        factorial = factorial * i;
    return factorial;
}
 
double nCr(double n, double r) {
    return factorial(n) / (factorial(r) * factorial(n - r));
}
bool isPerfectSquare(long double x)
{
    // Find floating point value of
    // square root of x.
    if (x >= 0) {
 
        long long sr = sqrt(x);
         
        // if product of square root
        //is equal, then
        // return T/F
        return (sr * sr == x);
    }
    // else return false if n<0
    return false;
}
int czypie(int v)
{
    if (v<=1)
        return 0;
    for (int i=2; i*i<=v; i++)
        if (!(v%i))
            return 0;
    return 1;
}
ll sum(ll n){
    ll ans=n*(n+1)/2;
    return ans;
}
bool isPrime(int n)
{
    if (n <= 1)
        return true;
    if (n <= 3)
        return true;
    if (n % 2 == 0 || n % 3 == 0)
        return false;
    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
 
    return true;
}
ll binToDec(string s) { return bitset<64>(s).to_ullong(); }
string decToBin(ll a) { return bitset<64>(a).to_string(); }

ll andOperator(ll a, ll b)
{
    ll shiftcount = 0;
 
    while (a != b and a > 0)
    {
        shiftcount++;
        a = a >> 1;
        b = b >> 1;
    }
    return int64_t(a << shiftcount);
}
ll factorial(ll n){
    ll ans=1;
    for (ll i=1;i<=n;i++){
         ans=mod_mul(ans,i);
    }
    return ans;
}
int nCr(ll n, ll r) {
    ll ans=factorial(n);
    ans=mod_mul(ans,modinv(factorial(r)));
    ans=mod_mul(ans,modinv(factorial(n-r)));
    return ans;
}

ll covert(ll n){
    string s=decToBin(n);
    int ch=-1;
    for (int i=0;i<64;i++){
        if (s[i]=='1'){
            ch=i;
            break;
        }
    }
    if (ch==-1){
        return 0;
    }
    else{
        int cnt=0;
        for (int i=ch;i<64;i++){
            if (s[i]=='0'){
                cnt++;
            }
        }
        return cnt;
    }
}
ll covert_cnt(ll n){
    string s=decToBin(n);
    int ch=-1;
    for (int i=0;i<64;i++){
        if (s[i]=='1'){
            ch=i;
            break;
        }
    }
    if (ch==-1){
        return 0;
    }
    else{
        int cnt=0;
        for (int i=ch;i<64;i++){
            
                cnt++;
            
        }
        return cnt;
    }
}
ll g(ll x){
    if (x==1) return 1;
    if (x%2==0) return 2*g(x/2)+1;
    else return 2*g(x/2);
}



void solve(){
    string s;
    cin>>s;
    int n=s.length();
    int curr=0;
    int mn=INT_MAX;
    vector <pair<int,int>> v;
    for (int i=0;i<n;i++){
        if (s[i]=='+'){
            curr++;
        }
        else{
            curr--;
        }
        if (curr<0){
            v.pb({curr,i});

        }
        mn=min(mn,curr);
    }
    if (mn>=0){
        cout << n << endl;
        return; 
    }
    curr=0;
    int ch=-1;
    for (int i=0;i<n;i++){
        if (s[i]=='+'){
            curr++;
        }
        else{
            curr--;
        }
        if (curr==mn){
            ch=i;
            break;
        }

    }
    int mx=-1;
    ll ans=0;
    for (int i=0;i<v.size();i++){
        if (v[i].S>ch){
            break;
        }
        if (abs(v[i].F)>mx){
            ans+=abs(v[i].S)+1;
            mx=max(mx,abs(v[i].F));

        }


    }
    cout << ans+n << endl;

}


     







    


    




   
   

    
    

 
 
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    t=1;
    cin>>t;
    while(t--){
        solve();
    
    }
 
 
 
    return 0;
}


Comments

Submit
0 Comments
More Questions

767A - Snacktower
1365A - Matrix Game
714B - Filya and Homework
31A - Worms Evolution
1691A - Beat The Odds
433B - Kuriyama Mirai's Stones
892A - Greed
32A - Reconnaissance
1236D - Alice and the Doll
1207B - Square Filling
1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String
1244C - The Football Season
1696B - NIT Destroys the Universe
1674A - Number Transformation
1244E - Minimizing Difference
1688A - Cirno's Perfect Bitmasks Classroom
219A - k-String
952A - Quirky Quantifiers
451B - Sort the Array
1505H - L BREAK into program
171E - MYSTERIOUS LANGUAGE
630D - Hexagons
1690D - Black and White Stripe
1688D - The Enchanted Forest
1674C - Infinite Replacement