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. ExpandoObject的使用

    IDictionary<string, object> obj = new System.Dynamic.ExpandoObject(); obj.Add(); dynamic obj2 ...

  2. canvas前端压缩图片

    参考网上的用法,下面是利用canvas进行的图片压缩 <!DOCTYPE html> <html> <head> <meta charset="ut ...

  3. springboot springmvc 抛出全局异常解决方法

    springboot中抛出异常,springboot自带的是springmvc框架,这个就不多说了. springmvc统一异常解决方法这里要说明的是.只是结合了springboot的使用而已.直接上 ...

  4. go实现set

    package main import ( "fmt" "sync" ) type object interface{} type Set struct { m ...

  5. 编译安装php容易出现的问题以及解决办法

    http://crybit.com/20-common-php-compilation-errors-and-fix-unix/

  6. log4j.properties配置详情

    log4j: log for java 是Apache的一个开源项目! 00.将我们的日志信息,输出到指定的位置(控制台   文件中) 01.我们可以控制每一条日志的输出格式 02.我们设置日志信息的 ...

  7. AngularJS(五):表单及输入验证

    本文也同步发表在我的公众号“我的天空” 表单基础 表单是HTML中很重要的一个部分,基本上我们的信息录入都依靠表单,接下来我们学习如何在AngularJS中使用表单,首先看以下示例代码: <bo ...

  8. 用Node+wechaty写一个爬虫脚本每天定时给女(男)朋友发微信暖心话

    wechatBot 微信每日说,每日自动发送微信消息给你心爱的人 项目介绍 灵感来源 在掘金看到了一篇<用Node + EJS写一个爬虫脚本每天定时女朋友发一封暖心邮件>后, 在评论区偶然 ...

  9. jsp-简单的猜数小游戏

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  10. CentOS6.4安装JDK,卸载自带的OpenJDK

    1.查看OpenJDK的安装包 $ rpm -qa |grep java java-1.6.0-openjdk-1.6.0.0-1.62.1.11.11.90.el6_4.x86_64 java-1. ...