(算法练习)——栈的使用练习

要求:
http://codeup.cn/problem.php?cid=100000602&pid=1
代码为啥不能AC= =真的郁闷。。。

#include <stdio.h>
#include <stack>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main(){
    
    
	int n;
	cin>>n;
	for(int i = 0;i <n;i++){
    
    
		string str;
		cin>>str;
		stack<char>st;
		int j = 0;
		while(j <str.size()){
    
    
			if(str[j] == ')'){
    
    
				if(st.top() == '('){
    
    
					st.pop();
					
				}
				else{
    
    
					st.push(str[j]);
					
				}
			}
			if(str[j] == ']'){
    
    
				if(st.top() == '['){
    
    
					st.pop();
					
				}
				else{
    
    
					st.push(str[j]);
					
				}
			}
			if(str[j] == '}'){
    
    
				if(st.top() == '{'){
    
    
					st.pop();
					
				}
				else{
    
    
					st.push(str[j]);
					
				}
			}
			else if(str[j] =='(' || str[j] == '['|| str[j] == '{'){
    
    
				st.push(str[j]);
				
			}
			j++;
		}
		if(st.size() == 0){
    
    
			printf("yes");
		}
		else{
    
    
			printf("no");
		}
		if(i <n-1){
    
    
			printf("\n");
		}
		while(!st.empty()){
    
    
			st.pop();
		}
		
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_42377217/article/details/104429428
今日推荐