文章

60

粉丝

361

获赞

41

访问

498.2k

头像
简单思路
P1067 中山大学2019年机试题
发布于2021年1月17日 22:15
阅读数 10.4k

使用map输入优先级

其他和括号题判断一样

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	string s;
	map<char ,int>prior;
	prior['{']=4;prior['[']=3;prior['(']=2;prior['<']=1;
	while(cin>>n)
	{
		for(int i=0;i<n;i++)
		{
			cin>>s;
			stack<char>st;
			int flag=0;
			for(int i=0;i<s.size();i++)
			{
				if(!st.empty())
				{
					if(st.top()=='('&&s[i]==')'||st.top()=='['&&s[i]==']'||st.top()=='{'&&s[i]=='}'
						||st.top()=='<'&&s[i]=='>')
						st.pop();
					else
					{
						if(prior[st.top()]<prior[s[i]])
							break;
						st.push(s[i]);
					}
				}
				else
					st.push(s[i]);
			}
			if(!st.empty()) 
				cout<<"NO"<<endl;
			else
				cout<<"YES"<<endl;
		}
	}
	return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发