Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***
- Solved by: Mohammad Sabbir Ahmed
- Problem name: 673 - Parentheses Balance
- Problem link: https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&category=0&problem=614&mosmsg=Submission+received+with+ID+26160948
- ***/
- #include <bits/stdc++.h>
- //#include <stdio.h>
- //#include <string.h>
- //#include <math.h>
- //#include <stdlib.h>
- //#include <ctype.h>
- //#include <string.h>
- //
- //#include <algorithm>
- //#include <iostream>
- //#include <vector>
- //#include <map>
- //#include <set>
- //#include <string>
- //#include <sstream>
- //#include <queue>
- //#include <list>
- //#include <string>
- //#include <stack>
- #define ll long long
- #define smallestnum 1e18
- using namespace std;
- void solve()
- {
- //ll n;
- //char str[130];
- string str;
- //scanf("%s", str);
- //cin >> str;
- getline(cin,str);
- //cout << "String: " << str << endl;
- ll len = 0;
- len = str.size();
- //cout << "Len: " << len << endl;
- if(len==0)
- {
- printf("Yes\n");
- return;
- }
- stack<char> st;
- for(int i=0; i<len; i++)
- {
- //printf("Asci of %c: %d\n", str[i]);
- if(str[i] == '(' || str[i] == '[')
- {
- st.push(str[i]);
- //cout << "Empty: " << st.empty() << endl;;
- }
- else
- {
- //cout << "Emp: " << st.empty() << endl;
- if( !st.empty() && str[i] - st.top() == 2 && str[i] == ']'){
- st.pop();
- }
- else if( !st.empty() && str[i] - st.top() == 1 && str[i] == ')'){
- st.pop();
- }
- else if(i<len){
- printf("No\n");
- return;
- }
- }
- }
- if(st.empty())
- printf("Yes\n");
- else printf("No\n");
- return;
- }
- void tscan()
- {
- int t;
- //scanf("d", &t);
- cin >> t;
- getchar();
- while(t--)
- {
- //cout << "Case: " << t << endl;
- solve();
- }
- }
- int main()
- {
- // freopen("in.txt", "r", stdin);
- // freopen("out.txt", "w", stdout);
- //ios_base::sync_with_stdio(0);
- //cin.tie(0);
- tscan();
- //cout << '[' - ']';
- return 0;
- }
Add Comment
Please, Sign In to add comment