加载中...

括号匹配

括号匹配

题目

题目链接

image

代码

#include<iostream>
#include<stack>
#include<map>
#include<string>

using namespace std;

string str;
stack<int> s;
map<int,int> mp;

int main()
{
	cin>>str;
	int len=str.length();
	for(int i=0;i<len;i++)
	{
		if(str[i]=='(')
			s.push(i+1);
		else
		{
			if(!s.empty())
			{
				mp[s.top()]=i+1;
				s.pop();
			}
			else
			{
				cout<<"No";
				return 0;
			}
		}
	}
	if(s.empty())
	{
		cout<<"Yes"<<endl;
		for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++)
		{
			cout<<it->first<<' '<<it->second<<endl;
		}
	}
	else cout<<"No";
	return 0;
}
posted @ 2022-04-06 17:37  我没有bug  阅读(17)  评论(0编辑  收藏  举报