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. 软工1816 · Alpha冲刺(8/10)

    团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员1(组长):王彬 过去两天完成了哪些任务 推进前后端各个接口的整合 学习jQuery基本语法,为beta阶段的商铺页面做准备 接下 ...

  2. UVA 11270 轮廓线

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33787 题意: 用1*2或2*1的长条把n*m方格铺满的方案数. ...

  3. HDU 5179 beautiful number 数位dp

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5179 bc(中文): http://bestcoder.hdu.edu.cn/contes ...

  4. IDEA + SSH OA 第一天(Hibernate : Mapping (RESOURCE) not found)

    切入主题,看看今天的错误是如何发生的: 首先这是我的项目路径,java 是 Sources Root , resources 是 Resources Root ,放了所需要的配置文件,其中 Hiber ...

  5. CentOS7实现RabbitMQ高可用集群

    CentOS安装RabbitMQ集群 1.有3台已安装RabbitMQ的机器 192.168.38.133 rabbitmq1 192.168.38.137 rabbitmq2 192.168.38. ...

  6. 获取emacs安装的elpa包名称

    | grep "./" | sed 's/\.\///g' | sed 's/-[0-9].*$//' | sort -u

  7. NFS 它的目的就是想让不同的机器、不同的作业系统可以彼此分享个别的档案啦

    NFS即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源.在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件, ...

  8. 1029 C语言文法翻译(2)

    program à external_declaration | program external_declaration 翻译:<源程序>→ <外部声明> | <源程序 ...

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

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

  10. hdu-题目1421:搬寝室

    http://acm.hdu.edu.cn/showproblem.php?pid=1421 搬寝室 Time Limit: 2000/1000 MS (Java/Others)    Memory ...