18 solutions

  • 5
    @ 2022-1-1 21:38:14

    交第三遍了,感谢叶哥教我怎么交两段代码 这题两种思路,第一种不用栈

    #include<stdio.h>
    #include<cstring>
    using namespace std;
    const int N=300;
    int l;//用一个l来记录前括号 
    char a[N];//可以像钟哥那样用字符 
    int flag=0;
    int main(){
    	int t=0;
    	while(scanf("%c",&a[t])){
    		if(a[t]=='@')break;
    		t++;
    	} 
    	int n=strlen(a);
    	for(int i=0;i<n;i++){
    		if(a[i]=='(')l++;// 如果是前括号就++ 
    		if(a[i]==')')l--;// 如果是后括号就-- 
    		if(l<0){         // 如果l为负了说明这个后括号前面没有前括号匹配 ,这题直接计数显然是不对的,但我看到有人这样水过了,谢老师也讲了')('的问题,大概是数据有点问题(doge)
    			printf("NO");
    			flag=1;
    			break;
    		}
    	}
    	if(flag==0&&l==0)printf("YES");
    	if(flag==0&&l>0)printf("NO");//注意考虑完全部情况 
    	return 0;
    }
    

    第二种用栈,思路差不多就不贴注释了

    #include<stdio.h>
    #include<stack>
    using namespace std;
    const int N=300;
    char a; 
    int flag=0;
    int main(){
    	stack<char>st;
    	while(scanf("%c",&a)&&a!='@'){
    		if(a=='('){
    			st.push(a);
    		}
    		if(a==')'){
    			if(st.empty()){
    				printf("NO");
    				flag=1;
    				break;
    			}
    			else st.pop();
    		}
    	}
    	if(flag==0&&st.empty())printf("YES");
    	if(flag==0&&!st.empty())printf("NO");
    	return 0;
    }
    
    • 2
      @ 2022-1-1 21:07:27

      仅供参考

      #include<bits/stdc++.h>
      using namespace std;
      char num[1005];
      int main()
      {
      	std::ios::sync_with_stdio(false);
      	cin>>num;
      	stack<char> st;
      	for(int i=0;num[i]!='@';i++)
      	{
      		if(num[i]=='(')
      			st.push(num[i]);
      		if(num[i]==')')
      		{
      			if(!st.empty())//如果栈非空 
      				st.pop();
      			else{
      				st.push('(');//如果是空的,就加一个然后直接break 
      				break;	
      			}
      		}
      	}
      	if(!st.empty())//判断栈是否是空的 
      		cout<<"NO";
      	else
      		cout<<"YES";//是空的才符合 
      	return 0;
      }
      
      • 2
        @ 2022-1-1 20:49:56

        仅供参考,写的一般

        #include <bits/stdc++.h>
        using namespace std;
        stack<char>s;
        int main(){
        	char k;
        	while(cin>>k&&k!='@'){//输入
        		if(k=='('){
        			s.push(k);
        		}
        		else if(k==')'){
        			if((!s.empty())&&s.top()=='('){//如果配对就删除头部
        				s.pop();
        			}//否则不配对,则直接结束
        			else{
        				cout<<"NO";
        				return 0;
        			}
        		}
        	}//判断是否有剩下没配对的
        	if(s.empty()){
        		cout<<"YES";
        	}
        	else cout<<"NO";
        	return 0;
        }
        • 1
          @ 2025-3-29 19:07:53

          数组模拟栈 or stack

          #include <bits/stdc++.h>
          #define endl '\n'
          using namespace std;
          typedef pair<int, int> PII;
          using ll = long long;
          using ULL = unsigned long long;
          const int N = 1e7 + 5;
          
          char c;
          stack<char> s;
          char stk[N];
          int tt;
          inline void solve() {
          /* 
             while (cin >> c && c!='@') {
                  if (c == '(') {
                      s.push(c);
                  }
                  if (c == ')') {
                      if (!s.empty()) {
                          s.pop();
                      }else {
                          cout << "NO" << endl;
                          return;
                      }
                  }
              }
              if (s.empty()) cout << "YES" << endl;
              else cout << "NO" << endl;  
          */
              while (cin >> c && c!='@') {
                  if (c == '(') {
                      stk[++tt] = c;
                  }
                  if (c == ')') {
                      if (tt) {
                          --tt;
                      }else {
                          cout << "NO" << endl;
                          return;
                      }
                  }
              }
              if (tt) cout << "NO" << endl;
              else cout << "YES" << endl; 
          }
           
          int main() {
              ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
              int _ = 1;
              //int _; cin >> _;
              while (_--) solve();
              return 0;
          }
          
          • 1
            @ 2022-1-2 22:07:47

            简单写一下

            #include<bits/stdc++.h>
            using namespace std;
            stack<char> s;
            int main() {
            	char ch;
            	int flag=0;
            	while(cin>>ch){
            		if(ch=='@')break;
            		else if(ch=='(')s.push(ch);
            		else if(ch==')'&&!s.empty())s.pop();
            		else if(ch==')'&&s.empty()){
            			flag=1;
            			break;
            		}
            	}
            	if(!flag&&s.empty())cout<<"YES"<<endl;
            	else cout<<"NO"<<endl;
            	return 0;
            }
            
            • 1
              @ 2022-1-2 13:40:57
              #include<bits/stdc++.h>
              using namespace std;
              int main()
              {
              	stack<char> pp;
              	char a;
              	while(cin>>a&&a!='@'){
              		if(a=='('){
              			pp.push(a);
              		}else if(a==')'){
              			if(pp.empty()){//若此时栈中为空则输出NO
              				cout<<"NO";
              				return 0;
              			}
              			else pp.pop();
              		}
              	}
              	if(pp.empty()) cout<<"YES";
              	else cout<<"NO";
              	return 0;
              }
              
              • 1
                @ 2022-1-1 22:14:26

                随便写写

                #include<iostream>
                #include<string.h>
                using namespace std;
                char arr[1005];
                int main()
                {
                	int num=0;
                	cin>>arr;
                	for(int i=0;i<strlen(arr);i++)
                	{
                		if(arr[i]==')') num++;
                		if(arr[i]=='(') num--;
                	}
                	if(num==0) cout<<"YES";
                	else cout<<"NO";
                	return 0;
                }
                
                • 1
                  @ 2022-1-1 21:25:14

                  模板题,仅供参考

                  #include <bits/stdc++.h>
                  using namespace std;
                  int main()
                  {
                    stack <char> s;
                    char c;
                    while(cin>>c&&c!='@')
                    {
                    	if(c=='(')
                    	{
                    		s.push(c);
                  	}
                  	else if(c==')')
                  	{
                  		if(s.empty())
                  		{
                  			cout<<"NO";
                  			return 0;
                  		}
                  		else
                  		{
                  			s.pop();
                  		}
                  	}
                    }
                    if(s.empty())
                    {
                    	cout<<"YES";
                    }
                    else cout<<"NO";
                   return 0;
                  }
                  
                  • 1
                    @ 2022-1-1 21:03:08
                    #include <bits/stdc++.h>
                    using namespace std;
                    int main()
                    {
                    	int cnt1=0,cnt2=0;
                    	string s;
                    	getline(cin,s);
                    	for(int i=0; i<s.size(); i++)
                    	{
                    		if(s[i] == '(') cnt1++;
                    		else if(s[i] == ')') cnt2++;
                    		if(cnt2 > cnt1)//注意第一个括号是 ) 如果是就直接NO
                    		{
                    			cout << "NO";
                    			return 0;
                    		}
                    	}
                    	if(cnt1 == cnt2)cout << "YES";
                    	else cout << "NO";
                    	return 0;
                    }
                    
                    • 0
                      @ 2022-1-3 11:09:27
                      #include<bits/stdc++.h>
                      using namespace std;
                      char a[305];
                      stack<int> S;
                      int main(){
                      	for(int i=1;i<=305;i++){
                      		cin>>a[i];
                      		if(a[i]=='@'){
                      			break;
                      		}
                      		if(a[i]=='('){
                      			S.push(a[i]);
                      		}
                      		if(a[i]==')'&&S.empty()==0){
                      			S.pop();
                      			continue;
                      		}
                      		if(a[i]==')'&&S.empty()!=0){
                      			cout<<"NO";
                      			return 0;
                      		}
                      	}
                      	if(S.empty()){
                      		cout<<"YES";
                      	}
                      	else{
                      		cout<<"NO";
                      	}
                      	return 0;
                      }
                      
                      • 0
                        @ 2022-1-1 22:50:24
                        #include<bits/stdc++.h>
                        using namespace std; 
                        stack <int> st;
                        string t;
                        int main()
                        {
                            cin>>t;
                            int len = t.size();
                            int flag = 0;
                            for(int i=0;i<len;i++){
                                if(t[i] == '('){
                                   
                                    st.push(t[i]);
                                }
                                else if(t[i] == ')'){
                                    if(st.empty()){
                                        flag = 1;break;
                                    }
                                    else{
                                        if(t[i] == ')' && st.top() == '(') st.pop();
                                    }
                                }
                            }
                        
                            if(st.size() || flag){
                            	cout<<"NO";
                        	}
                            else{
                            	cout<<"YES";
                        	}
                            return 0;
                        }
                        
                        
                        
                        • 0
                          @ 2022-1-1 22:10:59

                          #include<bits/stdc++.h> using namespace std; char a[300]; int n1=0,n2=0;//判断左括号和右括号数量是否相等 int main(){ scanf("%s",a); stackstk; for(int i=0;a[i]!='@';i++){ if(a[i]'('){ stk.push(i); n1++; } if(a[i]')')n2++; if(!stk.empty()){ if(a[i]')')stk.pop(); } } if(stk.empty()&&(n1n2))printf("YES"); else printf("NO"); return 0; }

                          • 0
                            @ 2022-1-1 22:05:56
                            #include <iostream>
                            #include <cstring>
                            using namespace std;
                            int main()
                            {
                            	int count=0,su=0;
                            	char a[1001];
                            	fgets(a,1001,stdin);
                            	for(int i=0;i<strlen(a);i++)
                            	{
                            		if(a[i]=='(')  count++;
                            		if(a[i]==')')  su++;
                            		if(count<su)
                            		{
                            			cout<<"NO"<<endl;
                            			return 0;
                            		}
                            	}
                            	    if(count==su) cout<<"YES"<<endl;
                            		else cout<<"NO"<<endl;
                            	return 0;
                            }
                            
                            • 0
                              @ 2022-1-1 21:56:24
                              #include<bits/stdc++.h>
                              using namespace std;
                              stack<char> S; 
                              int main(){
                              	char x;
                              	while(cin>>x){
                              		if(x=='@'){
                              			break;
                              		}
                              		else if(x=='('){
                              			S.push(1);
                              		}
                              		else if(x==')'){
                              			if(!S.empty()&&S.top()==1){
                              				S.pop();
                              			}
                              			else{
                              				S.push(-1);
                              			}
                              		}
                              	}
                              	if(!S.empty()){
                              		printf("NO");
                              	}
                              	else{
                              		printf("YES");
                              	}
                              	
                              	return 0;
                              }
                              
                              • 0
                                @ 2022-1-1 21:53:42

                                日常蒟蒻确认

                                #include<bits/stdc++.h>
                                using namespace std;
                                int main()
                                {
                                	char wuhu[2000];
                                	stack<char> s;
                                	cin>>wuhu;
                                	for(int i=0;wuhu[i]!='@';i++)
                                	{
                                			if(wuhu[i]=='(')
                                			{
                                			s.push(wuhu[i]);//放栈里 
                                			}
                                			else if(wuhu[i]==')')
                                			{
                                				if(!s.empty())//栈不空说明有'(' 
                                				{
                                				s.pop();//配对啦出来吧你 
                                				}
                                				else//空了 好吧重开 
                                				{
                                					cout<<"NO";
                                					return 0;//这里还是看了大佬
                                				}
                                			}
                                		}
                                		if(!s.empty())//再看看栈空不空 
                                		cout<<"NO";
                                		else
                                		cout<<"YES";
                                	return 0;
                                }
                                
                                • 0
                                  @ 2022-1-1 21:49:06
                                  #include<stdio.h>
                                  #include<string.h>
                                  int main()
                                  {
                                  	char arr[225];
                                  	int left = 0;
                                  	scanf("%s",arr);
                                  	for(int i = 0;i<strlen(arr);i++){
                                  		if(arr[i]=='('){
                                  			left++;
                                  		}
                                  		if(arr[i]==')'){
                                  			if(left==0){
                                  				printf("NO");
                                  				left-=1;
                                  				break;
                                  			}
                                  			else{
                                  				left--;
                                  			}
                                  		}
                                  	}
                                  	if(left==0)printf("YES");
                                  	else if(left>0)printf("NO");
                                  	return 0;
                                  }
                                  
                                  • 0
                                    @ 2022-1-1 21:35:00

                                    第一眼的做法,因为没卡左右括号,看看就好

                                    #include<bits/stdc++.h>
                                    using namespace std;
                                    int main(){
                                    	int n=0,k=0;
                                    char s;
                                    while(1){
                                    	cin>>s;
                                    	if(s=='@') break;
                                    	if(s=='(') n++;
                                    	if(s==')') k++; 
                                    }
                                    if(n==k) cout<<"YES";
                                    else cout<<"NO";
                                    	return 0;
                                    }
                                    
                                    • 0
                                      @ 2022-1-1 21:26:46
                                      
                                      #include<bits/stdc++.h>
                                      typedef long long ll;
                                      using namespace std;
                                      char s[100010];
                                      int main()
                                      {
                                      	int flage=0;
                                      	stack<char>q;
                                        	cin>>s;
                                        	for(int i=0;i<strlen(s);i++)
                                        	{
                                      
                                        		if(s[i]=='('||s[i]==')')
                                        		{
                                        			if(flage==0)//判断栈是否为空;
                                        			{
                                        				q.push(s[i]);
                                        				flage=1;
                                      			  }
                                        			else if((s[i]=='('&&q.top()==')')||(s[i]==')'&&q.top()=='('))
                                        			{
                                        				q.pop();
                                        				if(q.empty()) flage=0;
                                      			}
                                      			else q.push(s[i]);
                                      		}
                                      	}
                                      	if(q.empty()) cout<<"YES"<<endl;
                                      	else cout<<"NO"<<endl;
                                      	return 0;
                                      }
                                      
                                      • 1

                                      Information

                                      ID
                                      289
                                      Time
                                      1000ms
                                      Memory
                                      128MiB
                                      Difficulty
                                      5
                                      Tags
                                      # Submissions
                                      248
                                      Accepted
                                      91
                                      Uploaded By