a


【问题描述】
你是能看到第一题的 friends 呢。
——hja
给你一个只有小括号和中括号和大括号的括号序列,问该序列是否合法。
【输入格式】
一行一个括号序列。
【输出格式】
如果合法,输出 OK,否则输出 Wrong。
【样例输入】
[(])
【样例输出】
Wrong
【数据范围与规定】
70%的数据,1 ≤ ? ≤ 100。
对于100%的数据,1 ≤ ? ≤ 10000,所有单词由大写字母组成。

#include<iostream>
#include<cstring>
#include<cstdio>
#define maxn 10010
using namespace std;
int top;
char ch[maxn],st[maxn];
int main(){
    //freopen("Cola.txt","r",stdin);
    freopen("a.in","r",stdin);freopen("a.out","w",stdout);
    scanf("%s",ch+1);
    int len=strlen(ch+1);
    for(int i=1;i<=len;i++){
        if(ch[i]=='('||ch[i]=='['||ch[i]=='{')st[++top]=ch[i];
        else if(ch[i]==')'){
            if(top==0||st[top]!='('){
                printf("Wrong");
                return 0;
            }
            else top--;
        }
        else if(ch[i]==']'){
            if(top==0||st[top]!='['){
                printf("Wrong");
                return 0;
            }
            else top--;
        }
        else if(ch[i]=='}'){
            if(top==0||st[top]!='{'){
                printf("Wrong");
                return 0;
            }
            else top--;
        }
    }
    if(top!=0)printf("Wrong");
    else printf("OK");
    fclose(stdin);fclose(stdout);
    return 0;
}
100分 栈模拟

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-26
  • 2021-05-31
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-21
  • 2021-07-16
相关资源
相似解决方案