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

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

 
 
 
解析:模拟。
 
 
 
#include <cstdio>

char s[10000000];
int a[25];
int n;
int res[25];
int rid[25]; void solve()
{
int cnt = 0, rcnt = 0;
for(int i = 1; i <= a[0]; ++i)
s[cnt++] = '(';
s[cnt] = ')';
rid[rcnt++] = cnt;
++cnt;
for(int i = 1; i < n; ++i){
int num = a[i]-a[i-1];
for(int j = 1; j <= num; ++j)
s[cnt++] = '(';
s[cnt] = ')';
rid[rcnt++] = cnt;
++cnt;
}
int res_cnt = 0;
for(int i = 0; i < rcnt; ++i){
int l = 0, r = 1;
for(int j = rid[i]-1; ; --j){
if(s[j] == '('){
++l;
if(l == r){
res[res_cnt++] = l;
break;
}
}
else
++r;
}
}
for(int i = 0; i < n-1; ++i)
printf("%d ", res[i]);
printf("%d\n", res[n-1]);
} int main()
{
int t;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
for(int i = 0; i < n; ++i)
scanf("%d", &a[i]);
solve();
}
return 0;
}

  

POJ 1068 Parencodings的更多相关文章

  1. 模拟 POJ 1068 Parencodings

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

  2. POJ 1068 Parencodings【水模拟--数括号】

    链接: http://poj.org/problem?id=1068 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27454#probl ...

  3. POJ 1068 Parencodings 模拟 难度:0

    http://poj.org/problem?id=1068 #include<cstdio> #include <cstring> using namespace std; ...

  4. poj 1068 Parencodings(栈)

    题目链接:http://poj.org/problem?id=1068 思路分析:对栈的模拟,将栈中元素视为广义表,如 (((()()()))),可以看做 LS =< a1, a2..., a1 ...

  5. POJ 1068 Parencodings (类似括号的处理问题)

                                                                                                    Pare ...

  6. poj 1068 Parencodings(模拟)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj ...

  7. [ACM] POJ 1068 Parencodings(模拟)

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19352   Accepted: 11675 De ...

  8. poj 1068 Parencodings 模拟

    进入每个' )  '多少前' (  ', 我们力求在每' ) '多少前' )  ', 我的方法是最原始的图还原出来,去寻找')'. 用. . #include<stdio.h> #incl ...

  9. poj 1068 Parencodings 模拟题

    Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two diff ...

随机推荐

  1. zoj 3745 Salary Increasing(坑爹的细节题!)

    题目 注意题目中的,引用绝望的乐园中的进一步解释如下: 这是一道浙大月赛的题,一如既往的坑爹,好好一道水题,被搞成一道坑题!!! //注意:r(i) < l(i+1) !细节啊细节! #incl ...

  2. hdu 1242 Rescue(BFS,优先队列,基础)

    题目 /******************以下思路来自百度菜鸟的程序人生*********************/ bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r ...

  3. POJ2513Colored Sticks

    http://poj.org/problem?id=2513 题意 : 一些木棒,两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 思路 : 这个题的话就 ...

  4. POJ1426Find The Multiple

    http://poj.org/problem?id=1426 题意 : 输入一个数n,找n的倍数m,这个m所满足的条件是,每一位数只能由0或1组成,在题目的旁边用红色的注明了Special Judge ...

  5. C# 中的命名规则

    需要注意: C# 区分大小写 ,若有int a 和 int A ,则a, 和 A是不同的 普通字段,属相,方法,类的命名规则: C#中推荐使用  camelCasing ,和 PascalCasing ...

  6. Eclipse安装SVN插件的方法( 手动安装)

    Eclipse Svn 插件CSDN下载地址:http://download.csdn.net/source/3143260  手动安装: 1.在Eclipse根目录下建一个任意文件夹(如plugin ...

  7. linux入门教程(四) 初步进入linux世界

    [Linux 系统启动过程] Linux的启动其实和windows的启动过程很类似,不过windows我们是无法看到启动信息的,而linux启动时我们会看到许多启动信息,例如某个服务是否启动. Lin ...

  8. http://www.cnblogs.com/huangcong/archive/2010/06/14/1757957.html

    http://www.cnblogs.com/huangcong/archive/2010/06/14/1757957.html http://www.cnblogs.com/langtianya/a ...

  9. Project Euler 96:Su Doku 数独

    Su Doku Su Doku (Japanese meaning number place) is the name given to a popular puzzle concept. Its o ...

  10. 毕向东JAVA视频讲解(四五课)

    内存的划分: 1,寄存器. 2,本地方法区. 3,方法区. 4,栈内存. 存储的都是局部变量. 而且变量所属的作用域一旦结束,该变量就自动释放. 5,堆内存. 存储是数组和对象(其实数组就是对象) 凡 ...