POJ1068(ACM括号模拟)

来源:互联网 发布:java空指针错误 编辑:程序博客网 时间:2024/05/21 10:45

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: 
S(((()()())))P-sequence    4 5 6666W-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

264 5 6 6 6 69 4 6 6 6 6 8 9 9 9

Sample Output

1 1 1 4 5 61 1 2 4 5 1 1 3 9

Source

Tehran 2001

先根据P数组,将括号数组进行还原。再遍历括号数组,给每个右括号确定自己对应的左括号,并统计M数组的值。

代码:

import java.util.Scanner;//poj1068:括号问题(ACM模拟)public class Main {static int[] data_P;// P数组static int[] data_W;// W数组static char[] data;// 括号数组static int[] flag;// 左括号标记数组public static void main(String[] args) {Scanner in = new Scanner(System.in);int casenum = in.nextInt();// 所有case的数量int[] len = new int[casenum];int[][] case_P = new int[casenum][];for (int num = 0; num < casenum; num++) {int ind = in.nextInt();// W数组的长度len[num] = ind;int[] per_P = new int[ind + 1];// P数组长度多1。for (int i = 1; i < per_P.length; i++) {per_P[i] = in.nextInt();}case_P[num] = per_P;}for (int i = 0; i < casenum; i++) {// 初始化各数组int inx = len[i];data_P = case_P[i];data_W = new int[inx];data = new char[2 * inx];// 括号数组长度为2倍flag = new int[2 * inx];// 对应括号数组// 调用还原括号数组的函数theBracket(0);System.out.println();}}// 还原括号数组--index:数组游标public static void theBracket(int index) {for (int i = 1; i < data_P.length; i++) {int temp = data_P[i] - data_P[i - 1];for (int j = 0; j < temp; j++) {// 该右括号与上一个右括号之间左括号的数量data[index] = '(';index++;}// 还原完左括号最后还原一个右括号data[index++] = ')';}printdata_W();}// 计算W数组public static void printdata_W() {int indd = 0;// W数组游标for (int i = 0; i < data.length; i++) {int count = 0;// W数组某位置的值// 如果找到一个右括号,就以此为起点,回溯到0;期间遇到一个右括号,count+1;// 遇到一个括号且标志位不为0,将该标志位置1;break将count值放到W数组中,继续向后找。if (data[i] == ')') {for (int j = i; j >= 0; j--) {if (data[j] == ')') {count++;}if (data[j] == '(' && flag[j] == 0) {flag[j] = 1;break;}}data_W[indd++] = count;}}// 输出W数组for (int i = 0; i < data_W.length; i++) {if (i == data_W.length - 1) {System.out.print(data_W[i]);} else {System.out.print(data_W[i] + " ");}}}}

0 0
原创粉丝点击