POJ1068 Parencodings

来源:互联网 发布:品质365巨大骗局 知乎 编辑:程序博客网 时间:2024/05/17 20:29

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

#include<stdio.h>#include<string.h>int main(){    int m,n,i,j,t,q,s,a[100],b[100],lu[200];    scanf("%d",&m);    while(m--)    {        memset(b,0,sizeof(b));        memset(lu,0,sizeof(lu));        scanf("%d",&n);        for(i=0;i<n;i++)            scanf("%d",&a[i]);        for(i=0;i<n;i++)        {            t=a[i]+i;            b[t]=1;        }        for(i=0,s=0;i<n*2;i++)        {            if(b[i]==1)            {                for(j=i-1,q=0;j>=0;j--)                {                    if(lu[j]==1)                    {                        q++;//找着1对                         continue;                    }                    if(b[j]==1)//这对已找过                     {                        continue;                    }                    if(b[j]==0&&lu[j]==0)                    {                        lu[j]=1;//左括号标记为1                         s++;                        printf(s==1?"%d":" %d",q+1);//控制格式输出                         break;                    }                }            }        }        printf("\n");    }    return 0;}

是这样子的:

(   (  (  () ( () )  )  )   (   ()   ()   )   )0   0  0  01 0 01 1  1  1   0   01   01   1   1//左右括号标记为0,10   1  2  34 5 67 8  9 10   11  1213 1415  16 17//总的括号数数列           0    1 2  3 4          5    6   7  8//右括号数数列           4    6 6  6 6          8    9   9  9//P数列           1    1 2  4 5          1    1   3  9//W数列

你会发现:

右括号数数列+P数列=左括号被标记的数列(就是总的括号数数列头上为1的那一串)

(神不神奇?惊不惊喜?意不意外?)

因为P数列代表有多少个左括号,再加上右括号数数列就是到这个位置的总括号数(mdzz我本来就知道)

然后从头开始遍历,遇到1就说明遇到了右括号,把该右括号前面的左括号标记记录到lu[]数组里,即说明找到了一对,那q++记录对数;
到下一次遍历的时候q重置为0,如果再碰到lu[]为1的时候重新计数。

己己研究一下代码叭~

原创粉丝点击