题目1 : Parentheses Matching
时间限制:
10000ms
单点时限:
1000ms
内存限制:
256MB
-
(())()()
样例输出
-
1 4 2 3 5 6 7 8
-
这道题给了我栈的另外一种用法,原来配对括号的时候,栈还可以压入它的下标,最后排下序就好了。
-
#include<stdio.h> #include<algorithm> #include<math.h> #include<string.h> #include<stack> using namespace std; char s[100005]; struct A { int q,p; }a[50005]; bool cmp(A m,A n) { return m.q<n.q; } int main() { gets(s); int len=strlen(s); stack <int>t; int l=0; for(int i=0;i<len;i++) { if(s[i]=='(') t.push(i+1); else { a[l].q=t.top(); a[l].p=i+1; t.pop(); l++; } } sort(a,a+l,cmp); for(int i=0;i<l;i++) { printf("%d %d\n",a[i].q,a[i].p); } }
描述
Given a string of balanced parentheses output all the matching pairs.
输入
A string consisting of only parentheses '(' and ')'. The parentheses are balanced and the length of the string is no more than 100000.
输出
For each pair of matched parentheses output their positions in the string.