UVA673 水,模拟栈

来源:互联网 发布:查看linux版本 编辑:程序博客网 时间:2024/05/02 00:07

0)

注意,对于%c的输入,用的scanf。如果cin>> ,char 有坑。

1)

#include <iostream>#include <string.h>#include <stack>#include <stdio.h>using namespace std;int main(){    int kase;    cin>>kase;    char temp=getchar();    while(kase--){        stack <char> ss;        char ch;        while(1){            scanf("%c",&ch);//如果是cin,就不会退出            if(ch=='\n'){                break;            }            if(ss.empty()){                ss.push(ch);                continue;            }            char cur=ss.top();            if(ch==')'&&cur=='('){                ss.pop();            }            else if(ch==']'&&cur=='['){                ss.pop();            }            else{                ss.push(ch);            }        }        if(ss.empty()){            cout<<"Yes"<<endl;        }        else{            cout<<"No"<<endl;        }        while(!ss.empty()){            ss.pop();        }    }    return 0;}

2)

Description

 
 

 

You are given a string consisting of parentheses () and []. A string of this type is said to be correct:

(a)
if it is the empty string
(b)
if A and B are correct, AB is correct,
(c)
if A is correct, (A) and [A] is correct.

Write a program that takes a sequence of strings of this type and check their correctness. Your program can assume that the maximum string length is 128.

 

Input 

The file contains a positive integer n and a sequence of n strings of parentheses () and [], one string a line.

 

Output 

A sequence of Yes or No on the output file.

 

Sample Input 

 

3([])(([()])))([()[]()])()

 

Sample Output 

YesNoYes

0 0
原创粉丝点击