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 这个题理解了题意就不难了

输入2 6 不解释

4 5 6 6 6 6

可以这样理解

(((()()())))

第一个右括号左边有4个左括号

第二个右括号左边有5个左括号

以此类推;

所以根据所给出的数字串;很容易还原括号;

输出时:(((()()()))) 一个完整的()输出1;共3个

(()()())这个输出4一个()有3个(),3+1; 一句话就是
比如(((()()()))),
输入:每个右括号之前的左括号数序列为P=4 5 6 6 6 6,;

输出:每个右括号所在的括号内包含的括号数为W=1 1 1 4 5 6. 可以利用 栈和队列
 #include<iostream>
#include<queue>
#include<stack>
#include<cstdio>
using namespace std;
int main()
{
queue<char>p,q;
stack<char>Q;
int a,b,c,m,n;
cin>>n;
while(n--)
{
cin>>m;
b=;
while(m--)
{
cin>>a;
c=a-b;//制定压入的左括号的数目
b=a;
while(c--)
p.push('(');
p.push(')');//每次压入左括号之后压入一个右括号
}
while(!p.empty())//将队列中的括号一个个取出
{
if(p.front()=='(')
Q.push(p.front());//如果是左括号直接压入栈中 else if(p.front()==')')//如果是右括号,则进行判断
{
if(Q.top()=='(')//如果栈顶元素是左括号
{
Q.pop();//出栈
Q.push();//压入 1
q.push();//这是为了把数字储存下来,方便以后输出答案
}
else//如果栈顶元素不是左括号
{
int sum=;
while(Q.top()!='(')//持续出栈 直到左括号
{
sum+=Q.top();//统计一共包含多少左括号
Q.pop();
}
Q.pop();//左括号出栈
q.push(sum);//这是为了把数字储存下来,方便以后输出答案
Q.push(sum);//压入括号个数
}
}
p.pop();
}
printf("%d",q.front());
q.pop();
while(!q.empty())
{
printf(" %d",q.front());
q.pop();
}
printf("\n");
}
return ;
}

Parencodings的更多相关文章

  1. [POJ1068]Parencodings

    [POJ1068]Parencodings 试题描述 Let S = s1 s2...s2n be a well-formed string of parentheses. S can be enco ...

  2. Parencodings(imitate)

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20679   Accepted: 12436 De ...

  3. 模拟 POJ 1068 Parencodings

    题目地址:http://poj.org/problem?id=1068 /* 题意:给出每个右括号前的左括号总数(P序列),输出每对括号里的(包括自身)右括号总数(W序列) 模拟题:无算法,s数组把左 ...

  4. Parencodings 分类: POJ 2015-06-28 22:00 7人阅读 评论(0) 收藏

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22757   Accepted: 13337 De ...

  5. hdu 1361 Parencodings 简单模拟

    Parencodings 题意: 由括号序列S可经P规则和W规则变形为P序列和W序列. p规则是:pi是第i个右括号左边的左括号的数: w规则是:wi是第i右括号与它匹配的左括号之间右括号的数(其中包 ...

  6. POJ 1068 Parencodings

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24932   Accepted: 14695 De ...

  7. Poj OpenJudge 1068 Parencodings

    1.Link: http://poj.org/problem?id=1068 http://bailian.openjudge.cn/practice/1068 2.Content: Parencod ...

  8. POJ1068——Parencodings

    Parencodings Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encode ...

  9. PO1068 Parencodings 模拟题

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28860   Accepted: 16997 De ...

  10. Hdu1361&&Poj1068 Parencodings 2017-01-18 17:17 45人阅读 评论(0) 收藏

    Parencodings Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

随机推荐

  1. BFS-hdu-4101-Ali and Baba

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4101 题目大意: 给一个矩阵,0表示空的可走,-1宝藏的位置(只有一个),其余的正整数表示该位置石头 ...

  2. Struts 有哪些经常使用标签库

    Struts 有哪些经常使用标签库 1.html标签库 2.bean标签库 3.logic标签库

  3. [RxJS] Filtering operators: skipWhile and skipUntil

    After takeUntil() and takeWhile() function, let's have a look on skipWhile() and skilUntil() functio ...

  4. block没那么难(一):block的实现

    本系列博文总结自<Pro Multithreading and Memory Management for iOS and OS X with ARC> block 顾名思义就是代码块,将 ...

  5. 走进 Facebook POP 的世界

    POP: 一个流行的可扩展的动画引擎iOS,它支持spring和衰变动态动画,使其可用于构建现实,基于物理交互.Objective - C API允许快速集成, 对于所有的动画和过渡他是成熟的. 解释 ...

  6. Java的演变过程

    1. 1996.01.23 JDK1.0 代号Oak:212个类.8个包: 2. 1997.02.19 JDK1.1 504个类.23个包: Java Bean.远程方法调用(RMI).JAR文件格式 ...

  7. LiLei&HanMeiMei的隐式马尔可夫爱情

    一篇非常棒的隐马尔可夫入门文章...推荐! from: http://staffwww.dcs.shef.ac.uk/people/W.Liu/hmm.html

  8. 关于c++中的引用

    引用是个别名. 1.引用是否占用空间 引用是否占用空间,此处是指广义上的占用内存空间,即为该对象新开辟一块内存.这个需要分不同的情况. 首先看一下常引用(const 引用). 这里关于常引用在c++  ...

  9. hadoop之wordCount程序理解

    有篇文章http://www.cnblogs.com/xia520pi/archive/2012/05/16/2504205.html中介绍的

  10. HTML 5 Audio/Video DOM buffered 属性

    1.实例1获取视频第一段缓冲范围部分,以秒计: myVid=document.getElementById("video1"); alert("Start: " ...