括弧匹配检验——UPC

题目描述

假设表达式中允许包含两种括号:圆括号和方括号,其嵌套的顺序随意,如( ) 或[([ ][ ])]等为正确的匹配,[( ])或( 或 ( ( ) ) )均为错误的匹配。

现在的问题是,要求检验一个给定表达式中的括弧是否正确匹配?

输入一个只包含圆括号和方括号的字符串,判断字符串中的括号是否匹配,匹配就输出 “OK” ,不匹配就输出“Wrong”。输入一个字符串:[([][])],输出:OK

输入

仅一行字符,字符个数小于 255。

输出

匹配就输出OK ,不匹配就输出Wrong。

样例输入

[(])

样例输出

Wrong

栈的应用

#include <bits/stdc++.h>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define wuyt main
typedef long long ll;
template<class T> inline T min(T &x,const T &y){return x>y?y:x;}
template<class T> inline T max(T &x,const T &y){return x<y?y:x;}
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();
if(c == '-')Nig = -1,c = getchar();
while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();
return Nig*x;}
#define read read()
///const ll inf = 1e15;
///const int maxn = 2e5 + 7;
const ll mod=1e9+7;
const ll inf=0x3f3f3f3f;
const int maxn=1e6+9;
char ss[maxn];
int main()
{
	cin>>ss;
	int flag=0;
	int length=strlen(ss);
	stack<int> s;
	int ans=0;
	for(int i=0;i<length;i++)
	{
		if(ss[i]=='('||ss[i]=='['||ss[i]=='{')
            s.push(ss[i]);
		else
            if(!s.empty())
            {
                if(ss[i]==')'&&s.top()=='(')
                    s.pop();
                if(ss[i]==']'&&s.top()=='[')
                    s.pop();
                if(ss[i]=='}'&&s.top()=='{')
                    s.pop();
            }
            else
            {
                flag=1;
                break;
            }
	}
	if (flag==0&&s.empty())
        printf("OK
");
	else
        printf("Wrong
");
	return 0;
}