# 【编程练习】poj1068

Parencodings

Time Limit: 1000MS

Memory Limit: 10000K

Total Submissions: 24202

Accepted: 14201

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

http://blog.csdn.net/xinghongduo/article/details/6174671

http://blog.chinaunix.net/uid-22609852-id-3506161.html

ac代码：

```#include <stdio.h>
#include <stdlib.h>
char c_kuohao[10000] = {0};

//生成空格匹配的字符串
void genkuohao(char* c_kuohao,int* array,int arraylength )
{
int cur_index = 0;
for (int i = 0; i< arraylength-1;i++)
{
int j ;
for ( j = 0;j <*(array+i+1)- *(array +i);j++)
{
c_kuohao[cur_index + j] = '(';

}
c_kuohao[cur_index + j ] = ')';
cur_index = cur_index + j +1;
}
}

//从括号字符串中，获取int 数组
//找到一个右括号，把匹配最近的左括号设置为字符1，并生成对应的rarray数组
void getWarray(char* c_kuohao,int* rarray,int arraylength)
{
int index = 0;
int i = 0;
while(c_kuohao[i]!=0)
{
if (c_kuohao[i] ==')')
{
int j = i-1;
while(c_kuohao[j]!='(')
{
j--;
if (c_kuohao[j] == '1')
{
*(rarray + index) += 1;
}

}
*(rarray + index) += 1;
c_kuohao[j] ='1' ;
index++;
i++;
}
else
{
i++;
}

}
}

void main()
{

//freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout);

/* 同控制台输入输出 */
int mainIndex = 0;
scanf("%d",&mainIndex);

for (int i = 0; i < mainIndex;i++)
{

int N = 0;
scanf("%d",&N);
// 下面申请内存时候要用sizeof不然free时候会算错导致堆出错
int *array = (int*)malloc(sizeof(int)*(N +1));
int *rarray = (int*)malloc(sizeof(int)*N);
//给数组第一个位置放个0
*(array+0) = 0;

for (int j = 1;j<=N;j++)
{
scanf("%d",array+j);
*(rarray + j-1) =0;

}

for (int k = 0;k<10000;k++)
{
c_kuohao[k] = 0;
}

genkuohao(c_kuohao,array,N+1);
getWarray(c_kuohao,rarray,N);

for (int z = 0;z<N;z++)
{
printf("%d ",*(rarray + z));
}

printf("\n");

free(array);
free(rarray);
}

}```

http://blog.csdn.net/qingniaofy/article/details/7701626

0 条评论

• ### leetcode 3 Longest Substring Without Repeating Characters最长无重复子串

Given a string, find the length of the longest substring without repeating chara...

• ### leetcode 5 Longest Palindromic Substring--最长回文字符串

Given a string S, find the longest palindromic substring in S. You may assume th...

• ### leetcode 11 Container with Most Water

Here is the proof. Proved by contradiction:

• ### Code Forces 650 C Table Compression(并查集)

C. Table Compression time limit per test4 seconds memory limit per test256 m...

• ### HDU 3032 Nim or not Nim?(Multi-Nim)

Problem Description Nim is a two-player mathematic game of strategy in which ...

• ### 【Codeforces】1213B - Bad Prices

版权声明：本文为博主原创文章，遵循 CC 4.0 BY-SA 版权协议，转载请附上原文出处链接和本声明。 ...

• ### POJ2488-A Knight's Journey（DFS+回溯）

题目链接：http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS ...

• ### Code Forces 652D Nested Segments（离散化+树状数组）

Nested Segments time limit per test 2 seconds memory limit per test 256 m...

• ### poj-3660-cows contest（不懂待定）

N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a program...

• ### Codeforces 626E Simple Skewness(暴力枚举+二分)

E. Simple Skewness time limit per test:3 seconds memory limit per test:256 megab...