Parencodings(模拟)

ZOJ Problem Set - 1016
Parencodings

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Let S = s1 s2 ... s2n be a well-formed string of parentheses. S can be encoded in two different ways:

  • By an integer sequence P = p1 p2 ... pn where pi is the number of left parentheses before the ith right parenthesis in S (P-sequence).
  • By an integer sequence W = w1 w2 ... wn where for each right parenthesis, say a in S, we associate an integer which is the number of right parentheses counting from the matched left parenthesis of a up to a. (W-sequence).

Following is an example of the above encodings:

S (((()()())))
P-sequence 4 5 6666
W-sequence 1 1 1456

Write a program to convert P-sequence of a well-formed string to the W-sequence of the same string.


Input

The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case is an integer n (1 <= n <= 20), and the second line is the P-sequence of a well-formed string. It contains n positive integers, separated with blanks, representing the P-sequence.


Output

The output consists of exactly t lines corresponding to test cases. For each test case, the output line should contain n integers describing the W-sequence of the string corresponding to its given P-sequence.


Sample Input

2
6
4 5 6 6 6 6
9
4 6 6 6 6 8 9 9 9


Sample Output

1 1 1 4 5 6
1 1 2 4 5 1 1 3 9

思路:可以利用给出的P-sequence推导出括号序列,然后再算出W-sequence

W-sequence的定义有点晦涩,在次解释一下。

W-sequence:序列W = w1 w2 ... wn,wi含义:括号序列中第i个右括号和与其匹配的左括号之间有wi个右括号(包括第i个右括号自身)。

AC Code:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string>
 4 #include <queue>
 5 #include <vector>
 6 #include <cmath>
 7 #include <cstdio>
 8 #include <cstring>
 9 using namespace std;
10 
11 int main()
12 {
13     vector<pair<char, bool> > par;
14     int t, n;
15     scanf("%d", &t);
16     while(t--)
17     {
18         par.clear();
19         scanf("%d", &n);
20         int x, y, cnt = 0;
21         while(n--)
22         {
23             scanf("%d", &x);
24             y = x - cnt;
25             cnt = x;
26             while(y--)
27                 par.push_back(make_pair('(', false));
28             par.push_back(make_pair(')', false));
29         }
30         vector<pair<char, bool> >::iterator i, j;
31         bool first = true;
32         for(i = par.begin(); i != par.end(); i++)
33         {
34             if(i->first == ')')
35             {
36                 x = 0;
37                 for(j = i; ; j--)
38                 {
39                     if(j->first == ')')
40                     {
41                         j->second = true;
42                         x++;
43                     }
44                     else if(j->first == '('  && !j->second)
45                     {
46                         j->second = true;
47                         break;
48                     }
49                     else if(j == par.end()) break;
50                 }
51                 if(!first)
52                     printf(" %d", x);
53                 else
54                 {
55                     printf("%d", x);
56                     first = false;
57                 }
58             }
59         }
60         putchar('\n');
61     }
62     return 0;
63 }

 

posted on 2013-07-25 20:56  铁树银花  阅读(352)  评论(0编辑  收藏  举报

导航