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题目的意思很明确就是括号字符串按照第一种方式编码得到一个序列P,按照第二种方式编码得到数列W。现在要求给出一个序列P,输出另一个序列W。该题是简单的模拟题,不需要太复杂的算法。我为了重新双向链表的知识,熟悉链表的构建,所以用链表的方法写了一个程序。另外,用数组的方法也写了一个程序,两个程序运行内存和代码长度不同,但运行时间都是0S。下面是两个程序的代码。
链表:
#include<stdio.h>
#include<malloc.h>
typedef struct kh
{
char ch;
int num;
struct kh *before;
struct kh *next;
}node;
node *head,*p,*pre;
int b;
int creat(int i, int tmp)
{
int temp = tmp;
tmp = tmp - b;
for(int j = 0; j <= tmp; j++)
{
p = (node *)malloc(sizeof(node));
if(NULL == p)
return -1;
if( j == tmp)
p->ch = ')';
else
p->ch = '(';
p->num = 0;
if(0 == i)
{
i++;
head = p;
pre = p;
}
pre->next = p;
p->before = pre;
pre = p;
}
p->next = NULL;
b = temp;
return 1;
}
int srch(int n)
{
int i;
node *t;
for(i = 0; i < n; i++)
{
p = head;
while( p->ch != ')')
p = p->next;
t = p;
while(p->before->ch != '(')
{
if(p->before->ch == 'n' )
{
t->num += p->before->num;
p->before->ch = 'y';
}
p = p->before;
}
t->num += 1;
p->before->ch = '0';
t->ch = 'n';
printf("%d ",t->num);
}
return 1;
}
int main()
{
int N,n,tmp,i;
scanf("%d",&N);
while(N--)
{
scanf("%d",&n);
for(i = 0;i <n; i++)
{
scanf("%d",&tmp);
creat(i,tmp);
}
head->before = NULL;
srch(n);
printf("\n");
while(head!=NULL)
{
p = head;
head = head->next ;
free(p);
}
b = 0;
}
return 1;
}
数组(比较简单)
#include<stdio.h>
int main()
{
int N,n,num,i,tmp,m,out;
scanf("%d",&N);
while(N--)
{
char ch[41] = {0};
m = tmp = 0;
scanf("%d",&n);
while(n--)
{
scanf("%d",&num);
for(i = 0; i < num - tmp; i++)
ch[m++] = '(';
ch[m++] = ')';
tmp = num;
}
for(i = 0; i < m; i++)
{
out = 0;
if(ch[i] == ')')
{
tmp = i;
out++;
while(--tmp)
{
if(ch[tmp] == '(')
{
ch[tmp] = '0';
break;
}
if(ch[tmp] == ')')
out++;
}
printf("%d ",out);
}
}
printf("\n");
}
return 1;
}