天天看點

[ACM_模拟] POJ1068 Parencodings (兩種括号編碼轉化 規律 模拟)

Description

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

q 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). 

q 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: 

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 file 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

Sample Output

Source

<a href="http://poj.org/searchproblem?field=source&amp;key=Tehran+2001">Tehran 2001</a>

題目大意:一組标準的括号(就是每一個左括号的都有且僅有一個右邊的對應,符合常理),其編碼方式有兩種:

                  P:p[i]表示第i個右括号左邊有的左括号數量

                  W:w[i]表示從與第i個右括号對應的左括号開始至第i個右括号共有右括号的數量

              現在給出P串輸出W串

解題思路:用b[]統計第i個右括号和第i-1個右括号之間有多少個左括号,然後模拟流程(對于每個右括号向前找

最近左括号,然後在找到的左括号對應區間的b[]減1,依次尋找....

[ACM_模拟] POJ1068 Parencodings (兩種括号編碼轉化 規律 模拟)
[ACM_模拟] POJ1068 Parencodings (兩種括号編碼轉化 規律 模拟)

本文轉自beautifulzzzz部落格園部落格,原文連結:http://www.cnblogs.com/zjutlitao/p/3246004.html,如需轉載請自行聯系原作者

繼續閱讀