Parencodings

Time Limit: 1000MS Memory Limit: 10000K

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


  • 题意就是给你一串括号,输入是第i个右括号左边有多少个左括号,要你输出第i个右括号左边有多少对匹配好的括号(包括自身)。

  • 数据量这么小,肯定是模拟啊,直接用一个标记数组标记左括号的匹配情况,每次有一个右括号就从当前的右括号开始向左找,一直找到左边第一个没有被标记的左括号,并且记录到第一个没被标记的左括号为止有多少个标记的左括号就好。然后输出就行。


#include<stdio.h>
#include<cstring>
using namespace std;
const int maxn = 100;
int num[maxn];
bool l[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(l,0,sizeof(l));
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++)
scanf("%d",&num[i]);
for(int i=1; i<=n; i++)
{
int sum = 0;
int k = num[i];
while(k>0 && l[k])
{
k--;
sum++;
}
if(k != 0)//左边没有了左括号,右括号只能打光棍
{
l[k] = true;
printf("%d",sum+1);
}
else
printf("%d",sum);
if(i != n)
printf(" ");
}
printf("\n");
}
return 0;
}

POJ:1086-Parencodings的更多相关文章

  1. poj:4091:The Closest M Points

    poj:4091:The Closest M Points 题目 描写叙述 每到饭点,就又到了一日几度的小L纠结去哪吃饭的时候了.由于有太多太多好吃的地方能够去吃,而小L又比較懒不想走太远,所以小L会 ...

  2. Poj OpenJudge 1068 Parencodings

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

  3. POJ:1182 食物链(带权并查集)

    http://poj.org/problem?id=1182 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1 ...

  4. 51Nod:1086背包问题 V2

    1086 背包问题 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放在容量为W的背包里 ...

  5. POJ:2229-Sumsets(完全背包的优化)

    题目链接:http://poj.org/problem?id=2229 Sumsets Time Limit: 2000MS Memory Limit: 200000K Total Submissio ...

  6. POJ:3126-Prime Path

    题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...

  7. POJ:2429-GCD & LCM Inverse(素数判断神题)(Millar-Rabin素性判断和Pollard-rho因子分解)

    原题链接:http://poj.org/problem?id=2429 GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K To ...

  8. POJ :3614-Sunscreen

    传送门:http://poj.org/problem?id=3614 Sunscreen Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...

  9. POJ:2377-Bad Cowtractors

    传送门:http://poj.org/problem?id=2377 Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Sub ...

  10. POJ:2139-Six Degrees of Cowvin Bacon

    传送门:http://poj.org/problem?id=2139 Six Degrees of Cowvin Bacon Time Limit: 1000MS Memory Limit: 6553 ...

随机推荐

  1. HDU 1176 免费馅饼 矩阵取数, dp + 滚动数组

    http://acm.hdu.edu.cn/showproblem.php?pid=1176 首先可以处理出整张地图的状态. book[T][POS]表示第T秒,在第pos个地方有多少个馅饼. dp[ ...

  2. Corn 表达式

    CronTrigger CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔,复发的发射工作的时间表.CronTr ...

  3. 路径方案数(mod)

    路径方案数(mod) [题目描述] 给一张无向图,n 个点和 m 条边,cyb 在 1 号点,他要去 2 号点, cyb 可以从 a 走到 b,当且仅当a到2的最短路,比b 到2的最短路长. 求 cy ...

  4. c#的Lambda 表达式

    首先看官方的说法: Lambda 表达式是一种可用于创建委托或表达式目录树类型的匿名函数. 通过使用 lambda 表达式,可以写入可作为参数传递或作为函数调用值返回的本地函数. Lambda 表达式 ...

  5. IO(字节流、字符流)

      第1章 字节流 在前面的学习过程中,我们一直都是在操作文件或者文件夹,并没有给文件中写任何数据.现在我们就要开始给文件中写数据,或者读取文件中的数据. 1.1 字节输出流OutputStream ...

  6. python之元组,列表和字典的区别

    Python语言包含6种内建的序列,其中,有两种主要的类型:列表和元组. 列表是可以修改的,而元组不可以,如果要添加或者删除某些元素,就只能用列表,为了限制某些元素,就会用到元组.一般来说,列表可以替 ...

  7. UVA 11552 Fewest Flops(区间dp)

    一个区间一个区间的考虑,当前区间的决策只和上一次的末尾有关,考虑转移的时候先统计当前区间出现过的字母以及种数ct 枚举上一个区间的末尾标号j,规定小于INF为合法状态,确定j之后看j有没有在当前的区间 ...

  8. UVA 714 Copying Books 抄书 (二分)

    题意:把一个包含m个正整数的序列划分成k个非空的连续子序列.使得所有连续子序列的序列和Si的最大值尽量小. 二分,每次判断一下当前的值是否满足条件,然后修改区间.注意初始区间的范围,L应该为所有正整数 ...

  9. 3. Netbackup 7.6客户端的安装(windows/linux)

    1 客户端的安装 1.1 Windows客户端安装 1.1.1 客户端hosts修改 windows xp/2003/vista/2008/7/8用户HOSTS文件是在“c:\windows\syst ...

  10. 【转】瓜娃(guava)的API快速熟悉使用

    http://www.cnblogs.com/snidget/archive/2013/02/05/2893344.html 1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: I ...