Skip to content

Commit 8207ece

Browse files
authored
Merge pull request #371 from ViMo018/patch-5
Add solution for parentheses scoring problem
2 parents 7ded312 + c4cb3bb commit 8207ece

1 file changed

Lines changed: 101 additions & 0 deletions

File tree

  • Problems/Data-structures/Day-04/sol/Vishva Modh
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include<bits/stdc++.h>
2+
#include<queue>
3+
using namespace std;
4+
typedef unordered_map<int, int> umii;
5+
typedef unordered_map<long long, long long> umll;
6+
typedef unordered_map<char, long long> umci;
7+
typedef vector<pair<int, int>> vpi;
8+
typedef vector<int> vi;
9+
typedef long long ll;
10+
typedef vector<long long> vll;
11+
typedef unordered_map<int , bool> umib;
12+
#define sum(v) accumulate(v.begin(), v.end(), 0)
13+
#define endl '\n'
14+
#define f0(i, n) for(long long i = 0; i < n; i++)
15+
#define f1(i, n) for(long long i = 1; i < n; i++)
16+
#define as(v) sort(v.begin(), v.end())
17+
#define all(x) (x).begin(), (x).end()
18+
#define pb push_back
19+
template<class T> umll frequency(vector<T> &v) {umll freq;for(auto &x:v) freq[x]++; return freq;}
20+
template<class T> umci S_frequency(vector<T> &v) {umci freq;for(auto &x:v) freq[x]++; return freq;}
21+
template <class T> void input(vector<T> &v){for(auto &x:v)cin>>x;}
22+
ll power(ll x, ll y){ ll res = 1; while (y > 0){ if (y & 1) res = (ll)(res*x); y = y>>1; x = (ll)(x*x); } return res; }
23+
void pvll(const vector<long long> &arr){for(auto it : arr){cout << it << " ";}cout << endl;}
24+
void pvi(const vector<int> &arr){for(auto it : arr){cout << it << " ";}cout << endl;}
25+
26+
//function to check score i will use stack data structure
27+
//time complexity = o(n);
28+
29+
//submission link :- https://codeforces.com/contest/1997/submission/355833501
30+
31+
32+
ll checkscore(string s)
33+
{
34+
ll ans=0;
35+
stack<ll>st;
36+
37+
for(ll i=0;i<s.size();i++)
38+
{
39+
if(s[i] == '(')
40+
{
41+
st.push(i);
42+
}
43+
else{
44+
ll a = st.top();
45+
ans+= i-a;
46+
st.pop();
47+
}
48+
}
49+
return ans;
50+
}
51+
52+
void solve(){
53+
54+
//-------------INPUT-------------
55+
ll n;
56+
cin >> n;
57+
// make an rbs first
58+
// approach whenever open>0 do it close to make minimum score
59+
60+
string s;
61+
cin >> s;
62+
63+
64+
//-------------CODE--------------
65+
66+
ll open=0;
67+
ll close=0;
68+
69+
for(ll i=0;i<n;i++)
70+
{
71+
if(s[i] == '(')
72+
{
73+
open++;
74+
}
75+
else if(s[i] == ')')
76+
{
77+
open--;
78+
}
79+
else if(s[i] == '_' && open==0)
80+
{
81+
s[i] = '(';
82+
open++;
83+
}
84+
else if(s[i] == '_' && open>0)
85+
{
86+
s[i] = ')';
87+
open--;
88+
}
89+
}
90+
91+
cout << checkscore(s) << endl;
92+
93+
94+
95+
}
96+
97+
98+
int main(){
99+
int tt; cin >> tt; while(tt--)
100+
{solve();};
101+
}

0 commit comments

Comments
 (0)