poj 1068 Parencodings

一个水题,全是小错误,整了一下午。

题目:http://poj.org/problem?id=1068

题意:给出一组括号:                   (((()()())))

P是”)“前面有几个“(”:                   4      5      6  6  6  6

S是“)”前面第几个“(”是对应的:   1    1      1   4  5   6

让你根据给出的P求对应的S

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 int main()
 9 {
10     int n,i,t,a[1500],b[1500],top,j,sum,ans,x;
11     cin>>t;
12     while(t--)
13     {
14         cin>>n; x=0;
15         memset(b,0,sizeof(b));
16         top=0;
17         for(i=1; i<=n; i++)
18         {
19             cin>>b[i];
20             for(j=1; j<=b[i]-b[i-1]; j++)
21             {
22                 a[++top]=-1;//左括号用-1表示
23             }
24             a[++top]=1;//右括号用1表示
25 26         }
27         for(i = 1; i <= top; i++)
28         {
29             if(a[i]==1)
30             {
31                 sum=1; ans=1;
32                 for(j=i-1; ; j--)
33                 {
34                     ans+=a[j];
35                     if(a[j]==1)//刚开始以为a[j]=-1,也是false了,唉
36                     sum++;
37 
38                     if(ans==0)
39                     {
40                         x++;
41                         printf("%d",sum);
42                         if(x!=n)//输出格式上也错了好几遍
43                             printf(" ");
44                         else
45                             printf("\n");
46                         break;
47                     }
48                 }
49             }
50         }
51     }
52     return 0;
53 }

 

posted @ 2013-07-30 19:25  水门  阅读(161)  评论(0编辑  收藏  举报