您的位置:首页 > 其它

括号匹配问题

2016-03-25 21:33 302 查看
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=2

#include <stdio.h>
#include <stack>
#include <map>
#include <queue>
#include <math.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

string s;

int main()
{
int t;
scanf("%d",&t);
while(t--)
{
stack<char> ans;///模拟进出站
int i;
cin>>s;
for(i=0; i<s.size(); i++)
{
if(s[i]=='['||s[i]=='(')
ans.push(s[i]);
else if(ans.size())
{
if(s[i]==')'&&ans.top()=='('||s[i]==']'&&ans.top()=='[')
{
ans.pop();
}
else
{
break;
}
}
else break;
}
if(i==s.size()&&ans.empty())
printf("Yes\n");
else printf("No\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: