POJ 1068 Parencodings

1.原题

Description

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

 

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

2.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:

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

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

2.题意

有两种方式

    1.找到一个右括号,然后输出他前面的左括号的数目
    2.找到一个右括号,然后找到与它匹配的左括号,然后输出他们之间的右括号的数目(包括自己)

现在就是给你第一种方式的输入,然后叫你求出第二种方式的输出

3.思路

这道题是在那个训练计划中是模拟题,其实我也不懂什么是模拟了,就说说自己的思路吧,其实很简单了,就是根据第一种方式,就可以很简单的求出所对应的括号序列,然后根据得出来的括号序列,记录下括号总个数,然后将整个扫一遍,然后找到右括号,就开始进入方法,如果是还是右括号,则个数加1,如果是左括号,看之前是否有右括号,没有的话,则这个左括号与该右括号是匹配的,则可以输出个数。

4.代码##(Accept)


#include<iostream>
#include<cstring>

using namespace std;

int num = 0;
char list[50];

void checkResult(int index){
    int op = 1;
    int kk = 0;
    for (int j=index-1;j>=0;j--) {
        if(list[j] == ')'){
            kk++;
            op ++;
        }
        else {
            if(kk == 0) {
                break;
            }
            else{
                kk--;
            }
        }
    }
    cout << op;
}

int main(){

    int testcase = 0;
    cin>>testcase;
    while(testcase--) {
        cin>>num;
        int before = 0;
        int p = 0;
        int index = 0;
        memset(list,0,sizeof(list));
        for (int i = 0; i < num;i++){
            cin>>p;
            if(i == 0){
                for (;index < p-before;index++) list[index] = '(';
            }else{
                index++;
                for (int j = 0;j < p-before;j++) {
                    list[index+j] = '(';
                    index++;
                }
            }
            list[index] = ')';
            before = p;
        }
        bool pp = false;
        for (int i = 0;i <= index;i++) { 
            if(list[i] == ')') {
                if(pp) cout << " ";
                else pp = true;
                checkResult(i);
            }
        }
        cout << endl;
    }
    return 0;
}