Parencodings

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

开始怎么都弄不明白题意是什么!还是看的别人的翻译,在这里分享一下: 

Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: 
设序列 S 是一个完全匹配的括号序列。序列 S 能用下面两种不同方法加密:

 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). 
通过一个整数序列 P = p1 p2...pn  ,其中 pi 表示序列 S 中第 i 个右括号 ")" 之前的左括号 “(” 的数量。当然这些括号是从左到右数的。

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

通过一个整数序列 W = w1 w2...wn ,wi 表示从第 i 个右括号 ")" 相匹配的左括号 “(” 的位置开始数的右括号 “)” 的数目,包括第 i 个右括号 “)” 本身。 

Following is an example of the above encodings: 
下面是上面两种加密方法的例子:

	S		(((()()())))

	P-sequence	    4 5 6666
解释:
第一个右括号 ")" 出现在 S 序列的第5个位置上,他前面有 4个左括号 “(” ,因此 p1 = 4

W-sequence	    1 1 1456
解释:
第一个右括号 ")" 出现在 S 序列的第5个位置上,和他匹配的左括号 “(” 出现在S 序列的第4个位置上,从第4个位置开始数到第5个位置,有 1个右括号 ")" ,因此 w1 = 1 。

第二个右括号 ")" 出现在 S 序列的第7个位置上,和他匹配的左括号 “(” 出现在S 序列的第6个位置上,从第6个位置开始数到第7个位置,有 1个右括号 ")" ,因此 w2 = 1 。

第三个右括号 ")" 出现在 S 序列的第9个位置上,和他匹配的左括号 “(” 出现在S 序列的第8个位置上,从第8个位置开始数到第9个位置,有 1个右括号 ")" ,因此 w3 = 1 。

第四个右括号 ")" 出现在 S 序列的第10个位置上,和他匹配的左括号 “(” 出现在S 序列的第3个位置上,从第3个位置开始数到第10个位置,有 4个右括号 ")" ,因此 w4 = 4 。

第五个右括号 ")" 出现在 S 序列的第11个位置上,和他匹配的左括号 “(” 出现在S 序列的第2个位置上,从第2个位置开始数到第11个位置,有 5个右括号 ")" ,因此 w5 = 5 。

第六个右括号 ")" 出现在 S 序列的第12个位置上,和他匹配的左括号 “(” 出现在S 序列的第1个位置上,从第1个位置开始数到第12个位置,有 6个右括号 ")" ,因此 w6 = 6 。

题目理解了,也就不感觉多难了。

我的解题思路也算是用的贪心法,也可以说是基本方法。

思路:知道了右括号的个数就知道了数组的大小,先把右括号的位置记录下来,赋值为 1 ,其余为 0 ;然后从第 i 个右括号位置开始,向左找离它最近的左括号,已经配对的赋值为2;在这过程中,每遇到一个 1 “右括号”加 1 ;最后输出即可;

源代码:(纯天然,无函数,菜鸟必备者也 O(∩_∩)O哈哈~)

 

#include<iostream>
using namespace std;
int main()
{
    int n,i,j,k,m,x,y;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        int a[21],c[21],d[21];
        int b[41]={0};
        cin>>m;
        for(j=1;j<=m;j++)
        {
            cin>>a[j];
            b[(j+a[j])]=1;
            c[j]=j+a[j];
        }
        int *q=c;
        q++;
        for(k=1;k<=m;k++)
          {
              x=1;
              for(j=*q-1;j>=1;j--)
              {
                if(b[j]==0) {b[j]=2 ; break ;}
                else if(b[j]==1) x++;
              }
              q++;
          d[k]=x;
          }
        for(j=1;j<=m;j++)
            cout<<d[j]<<" ";
        cout<<endl;
    }
   return 0;
}

 

 

 

 

 

posted on 2013-08-15 17:16  天梦Interact  阅读(326)  评论(0编辑  收藏  举报