1113. 括号匹配

题目描述

给定一个只包含左右括号的合法括号序列,按右括号从左到右的顺序输出每一对配对的括号出现的位置(括号序列以0开始编号)。

输入

仅一行,表示一个合法的括号序列。

输出

设括号序列有n个右括号。则输出包括n行,每行两个整数l,r,表示配对的括号左括号出现在第l位,右括号出现在第r位。

样例输入

(())()
这题也真是醉了
一模一样的输出居然一个0一个100
正确
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 char c[101];
 6 int js[100];
 7 void f()
 8 {
 9     int i=0,top=0,j=0;
10     while(i<strlen(c))
11     {
12         if(c[i]=='('){
13             js[j++]=i;
14         }
15         if(c[i]==')'){
16             cout<<js[--j]<<" "<<i<<endl;
17         }
18         i++;
19     }
20 }
21 int main()
22 {
23     cin>>c;
24     f();
25     return 0;
26 }
View Code

错误

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 char a[10001];
 6 struct stack
 7 {
 8     int p;
 9     int num;
10 }s[1001];
11 int main()
12 {
13     gets(a);
14     int l=strlen(a);
15     int begin;
16     int now=0;
17     for(int i=0;i<l;i++)
18     {
19         if(a[i]=='(')
20         {
21             s[now].p=1;
22             s[now].num=i;
23             now++;
24         }
25         else
26         {
27             //s[now].p=2;
28             //s[now].num=i;
29             cout<<s[now-1].num<<" "<<i<<endl;
30             now--;
31         }
32     }
33     return 0;
34 }
View Code

 

posted @ 2017-04-06 17:46  自为风月马前卒  阅读(445)  评论(0编辑  收藏  举报

Contact with me