UVA 673 栈的应用

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 stringa line.OutputA sequence of ‘Yes’ or ‘No’ on the output file.

Sample Input

3

([])

(([()])))

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

Sample Output

Yes

No

Yes


题意 很好理解 

1 如果  是空 则合法。

2 如果 A与B合法 ,则AB合法。

3 如果A合法 (A)[A]合法。


用栈来模拟这个过程。

每次取一个元素

如果与栈顶匹配,就pop栈顶,如果不匹配,压入栈中

到最后,如果为空,则合法


错误记录
1.RE
 把 stack < >q定义在了主函数内。运行内存爆了
2.WA
 未考虑情况(1) 即为空行的情况
 错误代码
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <sstream>
#include <string>
#define maxn 100000000
using namespace std;
char s[maxn];
stack <char> q;
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        while(!q.empty()) q.pop();
        cin>>s;
        int flag=0;
        for(int i = 0 ; s[i] ; i++){
            if(s[i]=='('||s[i]=='[')
                q.push(s[i]);
            else{
                if(!q.empty()&&((s[i]==')'&&q.top()=='(')||(s[i]==']'&&q.top()=='[')))
                    q.pop();
                else{
                    flag=1;
                    break;
                }
            }
        }
        if(!q.empty()) flag = 1;
        if(flag) printf("No\n");
        else printf("Yes\n");
    }
}
经过修改后
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <sstream>
#include <string>
#define maxn 100000000
using namespace std;
string s;
stack <char> q;
int main(){
    int t;
    //freopen("c:\\MyDrivers\\in.txt","r",stdin);
    //freopen("c:\\out.txt","w",stdout);
    scanf("%d",&t);
    getchar();
    while(t--){
        while(!q.empty()) q.pop();
        getline(cin,s);
        int flag=0;
        for(int i = 0 ; s[i] ; i++){
            if(s[i]=='('||s[i]=='[')
                q.push(s[i]);
            else{
                if(!q.empty()&&((s[i]==')'&&q.top()=='(')||(s[i]==']'&&q.top()=='[')))
                    q.pop();
                else{
                    flag=1;
                    break;
                }
            }
        }
        if(!q.empty()) flag = 1;
        if(flag) printf("No\n");
        else printf("Yes\n");
    }
}
通过 getline 函数来实现 100ms

简化
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <sstream>
#include <string>
#define maxn 100000000
using namespace std;
stack <char> q;
int main(){
    int t;
    //freopen("f:\\in.txt","r",stdin);
    //freopen("f:\\out.txt","w",stdout);
    scanf("%d%*c",&t);
    while(t--){
        while(!q.empty()) q.pop();
        char s;
        int flag=0;
        while((s=getchar()) != '\n'){
            if(s=='('||s=='[')
                q.push(s);
            else{
                if(q.empty())
                q.push(s);
                else if(s==']'&&q.top()=='[')
                q.pop();
                else if(s==')'&&q.top()=='(')
                q.pop();
                else q.push(s);
            }
        }
        if(!q.empty()) flag = 1;
        if(flag) printf("No\n");
        else printf("Yes\n");
    }
}
通过 getchar()来读取 高效而便于编程, 时间 20ms

事实上大量输入的情况下 cin 还是挺慢的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值