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. DCOM初步窥探二

    1.COM进程透明性表现在“组件对象和客户程序可以拥有各自的空间,也可以共享同一个进程空间”. COM负责把客户的调用正确传到组件对象中,并保证参数传递的正确性. 组件对象和客户代码不必考虑调用传递的 ...

  2. Windows下多线程编程(一)

    前言 熟练掌握Windows下的多线程编程,能够让我们编写出更规范多线程代码,避免不要的异常.Windows下的多线程编程非常复杂,但是了解一些常用的特性,已经能够满足我们普通多线程对性能及其他要求. ...

  3. script 执行的三种方式

    <script>: 脚本的获取和执行是同步的.此过程中页面被阻塞,停止解析. <script defer = "defer">:脚本的获取是异步的,执行是同 ...

  4. django-registration中的问题

    https://github.com/ubernostrum/django-registration.git https://django-registration.readthedocs.io/en ...

  5. DBGRID控件里可以实现SHIFT复选吗?怎么设置?

    //////////////////////////////////////////////////    功能概述:公用的列表框选择框,是用DBGrid网格////    注意事项:希望用Query ...

  6. DHCP:动态主机配置协议

    DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一个局域网的网络协议,使用UDP协议工作, 主要有两个用途:给内部网络或网络服务供应商自动分配IP ...

  7. SQL中的逻辑运算符

    逻辑运算符和比较运算符一样,都是返回 true 或 false 值得布尔数据类型.   运算符 行为 ALL 如果一个比较集中全部都是 true ,则值为 true AND 如果两个布尔值表达式均为 ...

  8. centos中apache自用常用额外配置记录(xwamp)

    xwamp套件中apache配置,记录下,以免忘记. 配置路径 ${wwwroot_dir}/conf/httpd.conf 配置内容 <ifmodule mod_deflate.c> D ...

  9. php简易配置函数

    nilcms中:php简易配置函数. 文件位置:nc-admin/common.php /* * --------------------------------------------------- ...

  10. day 03 字符串 for 循环

    1.有变量量name = "aleX leNb" 完成如下操作: 1)移除 name 变量量对应的值两边的空格,并输出处理理结果 name = "aleX leNb&qu ...