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. MySQL slave状态之Seconds_Behind_Master

    在MySQL的主从环境中,我们能够通过在slave上运行show slave status来查看slave的一些状态信息,当中有一个比較重要的參数Seconds_Behind_Master.那么你是否 ...

  2. 基于XMPP实现的Openfire的配置安装+Android客户端的实现

    最近在整理一些这方面的资料,闲话少说,咱还是直奔主题吧 :) 一.基于xmpp实现的openfire的配置安装 1. 下载最新的openfire安装文件 官方下载站点: http://www.igni ...

  3. STL的基本使用之关联容器:set和multiSet的基本使用

    STL的基本使用之关联容器:set和multiSet的基本使用 简介 set 和 multiSet 内部都是使用红黑树来实现,会自动将元素进行排序.两者不同在于set 不允许重复,而multiSet ...

  4. 基于事件的异步模式——BackgroundWorker

    实现异步处理的方法很多,经常用的有基于委托的方式,今天记录的是基于事件的异步模式.利用BackgroundWorker组件可以很轻松的实现异步处理,并且该组件还支持事件的取消.进度报告等功能.本文以计 ...

  5. Two ways to create file using 'doc'.

    Here are two ways to create file using 'doc' command: Method i: copy con [file name][enter key] [con ...

  6. C语言求2的100次方怎么解,大整数运算

    #include "stdio.h"int ai[100]; void main(){ int a,b; ai[99]=1; for(b=0;b<100;b++)  for( ...

  7. jQuery实现的向下推送图文信息滚动效果

    HTML 我们以新浪微博信息滚动为背景,html中包含了多条微博图文信息,结构如下: <div id="con"> <ul> <li> < ...

  8. wpf 自定义RadioButton控件样式

    实现的效果为: 我感觉来自定义RadioButton样式和定义button空间的样式差不多,只是类型不同而已. 接下来分析一下样式代码: <!--自定义单选按钮样式-->        & ...

  9. 最全Media 响应式 设置方法

    大家对于css3中media属性并不陌生,但是随着一些高视网膜的设备面世,很多情况对于media的不标准的用法也越来越多,我通过查找一些知识结合实践给总结出一些标准的设置的方法. CSS3 中的 Me ...

  10. win7下.NET 2.0未在web服务器上注册的问题(转)

    转自:http://blog.sina.com.cn/s/blog_6d15b547010192hx.html 电脑装了win7操作系统,装上vs2008后运行dotnetnuke项目后出现" ...