ZOJ Problem Set - 1016
Parencodings

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Let S = s1 s2 ... s2n be a well-formed string of parentheses. S can be encoded in two different ways:

  • 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).
  • 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 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

思路:可以利用给出的P-sequence推导出括号序列,然后再算出W-sequence

W-sequence的定义有点晦涩,在次解释一下。

W-sequence:序列W = w1 w2 ... wn,wi含义:括号序列中第i个右括号和与其匹配的左括号之间有wi个右括号(包括第i个右括号自身)。

AC Code:

 #include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std; int main()
{
vector<pair<char, bool> > par;
int t, n;
scanf("%d", &t);
while(t--)
{
par.clear();
scanf("%d", &n);
int x, y, cnt = ;
while(n--)
{
scanf("%d", &x);
y = x - cnt;
cnt = x;
while(y--)
par.push_back(make_pair('(', false));
par.push_back(make_pair(')', false));
}
vector<pair<char, bool> >::iterator i, j;
bool first = true;
for(i = par.begin(); i != par.end(); i++)
{
if(i->first == ')')
{
x = ;
for(j = i; ; j--)
{
if(j->first == ')')
{
j->second = true;
x++;
}
else if(j->first == '(' && !j->second)
{
j->second = true;
break;
}
else if(j == par.end()) break;
}
if(!first)
printf(" %d", x);
else
{
printf("%d", x);
first = false;
}
}
}
putchar('\n');
}
return ;
}

Parencodings(模拟)的更多相关文章

  1. PO1068 Parencodings 模拟题

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28860   Accepted: 16997 De ...

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

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

  3. POJ 1068 Parencodings 模拟 难度:0

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

  4. POJ1068 Parencodings(模拟)

    题目链接. 分析: 水题. #include <iostream> #include <cstdio> #include <cstring> using names ...

  5. poj 1068 Parencodings 模拟

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

  6. poj 1068 Parencodings 模拟题

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

  7. 模拟 POJ 1068 Parencodings

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

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

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

  9. [ACM_模拟] POJ1068 Parencodings (两种括号编码转化 规律 模拟)

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

  10. hdu 1361 Parencodings 简单模拟

    Parencodings 题意: 由括号序列S可经P规则和W规则变形为P序列和W序列. p规则是:pi是第i个右括号左边的左括号的数: w规则是:wi是第i右括号与它匹配的左括号之间右括号的数(其中包 ...

随机推荐

  1. Macbook Pro开机黑屏了。

    问题描述:点了appstore的更新,然后重启黑屏.(说明:黑屏是屏幕没亮:灰屏是屏幕亮了是灰黑色的.) 黑屏问题大,灰屏问题小. 开机按option没反应的跳到步骤四 一.数据 苹果电脑黑屏了,想搞 ...

  2. ssh: Could not resolve hostname问题终于解决了?

    1.如果系统为64位,无法启动启动hdfs: ./sbin/start-dfs.sh.并有以下错误: sed: -e expression #1, char 6: unknown option to  ...

  3. Vue于React特性对比(二)

    一,关于响应式数据更新方式的实现 1)只有在data里面定义的数据才会有响应式更新 vue依赖的defineProperty的数据劫持加上依赖数据,实现数据的响应式更新.可以称之为依赖式的响应.因为依 ...

  4. Excel poi API基础教程!

    原文转子: http://blog.csdn.net/yellowd1/article/details/44628701 登录|注册     yellowd1的专栏       目录视图 摘要视图 订 ...

  5. CentOS6.5 重启网络报错:Bringing up interface eth0: Error: Connection activation failed: Device not managed by NetworkManager or unavailable

    CentOS6.5 重启网络报错: Bringing up interface eth0: Error: Connection activation failed: Device not manage ...

  6. javascript之彻底理解valueOf, toString

    参与运算的都是简单类型(一般就字符串和数字), 复杂类型是不参与运算的. ***当对象(非简单类型)用作键时,会先调用toString()方法把对象转化成字符串 var a = {},     b = ...

  7. Sass & Scss & CSS3

    Sass & Scss & CSS3 Sass & Scss @mixin & @include & @import & variable https: ...

  8. 弱网络模拟测试工具---易测app

    易测功能介绍   易测是一款基于无线客户端研发场景的通用测试工具, 它通过在研发人员的自持机上提供各种辅助能力&标准化的专项测试服务来提升研发质量&效率.   易测app是阿里巴巴做的 ...

  9. jQuery 获取和设置radio 和 checkbox 值的操作

    jquery 中的val(),可以取值也可赋值,表单元素中的radio和checkbox是比较常用的控件,下面说说对它们的取值和赋值的使用 1.取值 表单如下: <div class=" ...

  10. 【uoj#175】新年的网警 结论题+Hash

    题目描述 给出一张 $n$ 个点 $m$ 条边的无向连通图,每条边的边权为1.对于每个点 $i$ ,问是否存在另一个点 $j$ ,使得对于任意一个不为 $i$ 或 $j$ 的点 $k$ ,$i$ 到 ...