Chủ Nhật, 23 tháng 10, 2022

ngoac797 c++

#include <bits/stdc++.h>
using namespace std;
 
string s;
int t;
char mo[3]={'{','[','('};
char dong[3]={'}',']',')'};
stack<char> st;
 
 
void giai()
{
    char c;
    while(!st.empty()) st.pop();
    cin>>s;
    for (int i=0;i<=s.length()-1;i++)
    {
        if (s[i]=='{'||s[i]=='['||s[i]=='(')
        {
            st.push(s[i]);
        }
        else
        {
            if(st.empty()) 
            {
                    cout<<0<<endl;
                    return;
                }
            c = st.top();st.pop();
            for(int j=0;j<=2;j++)
            {
                if(c==mo[j]&&s[i]!=dong[j])
                {
                    cout<<0<<endl;
                    return;
                }
            }
        }
    }
    if(st.empty()) cout<<1<<endl;
    else cout<<0<<endl;
}
 
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   // freopen("ngoac797.inp","r",stdin);
  //  freopen("ngoac797.out","w",stdout);
    cin>>t;
    for (int i=1;i<=t;i++)
    {
        giai();
    }
}

Không có nhận xét nào:

Đăng nhận xét

Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.