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. 《深入理解java虚拟机》笔记(6)内存分配与回收策略

    一.垃圾回收日志说明 [GC[DefNew: 7307K->494K(9216K), 0.0043710 secs] 7307K->6638K(19456K), 0.0044894 sec ...

  2. 关于 SQL Server Reporting Services 匿名登录的解决方案

    每次访问报表都需要windows验证,这样的报表给客户确实很说不过去. SSRS 可以匿名登录的设定步骤: 环境: 开发工具:SQL Server Business Intelligence Deve ...

  3. 5.类型、值和变量-JavaScript权威指南笔记

    开始变得有意思起来了,然而第三章还是以基础知识了解的角度阐释相关的概念,并没有深入到结合代码以及要实现的功能讲用法和原理的程度. 1.概论. value:程序的运行是对值的操作. type:能够表示并 ...

  4. 时间日期相关:Date类、DateFormat类、Calendar类

    1 Date类 类 Date 表示特定的瞬间,精确到毫秒. 1秒=1000毫秒 毫秒的0点:公元1970年 一月一日,午夜0:00:00 对应的毫秒值就是0 时间和日期的计算,必须依赖毫秒值. Sys ...

  5. cpu 满载测试软件

    for i in `seq 1 $(cat /proc/cpuinfo |grep "physical id" |wc -l)`; do dd if=/dev/zero of=/d ...

  6. Bot Framework:Activity类简明指南

    Bot Framework相关文档:https://docs.botframework.com/en-us/csharp/builder/sdkreference/attachments.html B ...

  7. 爬去酷狗top500的数据

    import requests from bs4 import BeautifulSoup import time headers={ #'User-Agent':'Nokia6600/1.0 (3. ...

  8. 爬去豆瓣图书top250数据存储到csv中

    from lxml import etree import requests import csv fp=open('C://Users/Administrator/Desktop/lianxi/do ...

  9. CDOJ 490 UESTC 490 Swap Game(思路,逆序对)

    题意:有两种颜色的小球形成环,求最小交互次数使球相连. 题解:先解决另一个简单的问题,如果是一个链,把红球标记为1,蓝球标记为0,要排成升序需要多少次交换呢?答案是逆序对总数,原因是一次交互最多消除一 ...

  10. ubuntu 18.04下 配置qt opencv的坑

    问题和过程描述: 我按照网上的教程装了qt5.8版本,然后去配置opencv,感觉一切顺利,然后随便写了个 Mat src = imread("xxx") 然后imshow发现编译 ...