题目描述
给定一个只包含左右括号的合法括号序列,按右括号从左到右的顺序输出每一对配对的括号出现的位置(括号序列以0开始编号)。
输入
仅一行,表示一个合法的括号序列。
输出
设括号序列有n个右括号。则输出包括n行,每行两个整数l,r,表示配对的括号左括号出现在第l位,右括号出现在第r位。
样例输入
(())()
样例输出
1 2
0 3
4 5
数据范围限制
括号序列长度不超过100。
.
.
.
.
.
分析
见左括号就压栈,见右括号就弹栈。
.
.
.
.
.
程序:
#include<iostream>
#include<string>
using namespace std;
int main()
{
int top=0,a[1000],l;
string s;
cin>>s;
l=s.length();
for (int i=0;i<l;i++)
{
if (s[i]=='(') a[++top]=i;
else
{
cout<<a[top]<<' '<<i<<endl;
top--;
}
}
return 0;
}