zoukankan      html  css  js  c++  java
  • Uva


    用栈就搞定了,忘了刚开始会出现 )) ,]] 这种情况,没有加栈非空的条件,结果RE了好几次,智商太拙计。

    AC代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cctype>
    #include <cstring>
    #include <string>
    #include <sstream>
    #include <vector>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <bitset> 
    #include <cassert> 
    #include <cmath>
    
    using namespace std;
    
    const int maxn = 256;
    
    int main()
    {
    	int n;
    	scanf("%d", &n);
    	getchar();
    	while (n--) {
    		char str[maxn];
    		fgets(str, sizeof(str), stdin);
    		bool flag = true;
    		stack<char> stc;
    		for (int i = 0; str[i] != ''; i++) {
    			switch (str[i])
    			{
    			case '(':
    				stc.push(str[i]);
    				break;
    			case '[':
    				stc.push(str[i]);
    				break;
    			case ')':
    				// 刚开始忘了加!stc.empty(),RE了好几次,太伤心了
    				if (!stc.empty() && stc.top() == '(') {
    					stc.pop();
    				}
    				else {
    					flag = false;
    				}
    				break;
    			case ']':
    				if (!stc.empty() && stc.top() == '[') {
    					stc.pop();
    				}
    				else {
    					flag = false;
    				}
    				break;
    			}
    			if (!flag) {
    				break;
    			}
    		}
    		if (flag && stc.empty()) {
    			printf("Yes
    ");
    		}
    		else {
    			printf("No
    ");
    		}
    	}
    
    	return 0;
    }




  • 相关阅读:
    冒泡排序
    二分查找
    数3退1的java实现
    列出目录下对应的子目录及文件
    errno相关
    Fibonacci
    windows 下查看Python 的安装路径
    20条编程经验
    [转]一位程序员工作10年总结的13个忠告
    Sql查询语句过滤重复的数据
  • 原文地址:https://www.cnblogs.com/zhangyaoqi/p/4591544.html
Copyright © 2011-2022 走看看