POJ1068浅析------Parencodings

来源:互联网 发布:购买空间和域名 编辑:程序博客网 时间:2024/05/21 17:59
Parencodings
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 14329 Accepted: 8524

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方法和W方法。

                 P方法:分别记录各个“)”之前“(”的个数组成P数组。

                 W方法:分别记录各个“)”与其所对应的“(”之间“)”的个数,包含其本身。

                 现给出P表示法,输出W表示法。

 

题目类型:  模拟

 

浅析:    建立一个整型数组模拟表示“(”、“)”序列,用0表示“(”,用1表示“)”。

          一.根据输入初始化模拟数组;

          二.将其转换为W模式:对每个“)”找到与其对应的“(”,即对每个1找到其左边距离最近的0,将其值改为1并记下两者中间1的个数然后再除以2加1,这便是P模式对应的值。

 

备注:memset函数头文件为<string.h>,对字符串初始化比较方便,能勉强用它对 int 数组赋初值 0 ,但不能赋初值 1 ,即不能像memset( a , 1 ,sizeof(a) )这么用.

 

我的代码如下:

#include<stdio.h>#include<string.h>int main(){    int t,i,j,n,flag;    int a[30],b[50];    scanf("%d",&t);    while(t--)    {        flag=0;        scanf("%d",&n);        for(i=1;i<=n;i++)        {            scanf("%d",&a[i]);                        }         memset(b,0,sizeof(b));        for(i=1;i<=n;i++)        {            b[a[i]+i]=1;        }        for(i=1;i<=n;i++)        {            for(j=a[i]+i-1;j>=1;j--)            {                if(b[j]==0)                {                    b[j]=1;                    if(!flag)                    {                        flag=1;                        printf("%d",(a[i]+i-j)/2+1);                        break;                    }                             else {                             printf(" %d",(a[i]+i-j)/2+1);                              break;                             }                  }                             }                         }          printf("\n");    }    return 0;    }

 

 

 

 

原创粉丝点击