版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Gods_magic/article/details/53771462
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.
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 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
Source
先根据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] + " ");
}
}
}
}