题目链接

Problem Description
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 file 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
 
题解:序列的含义如下:

P-sequence 4 5 6666 //某个左括号左边有几个左括号
W-sequence 1 1 1456 //某个右括号与匹配的左括号间有几个左括号

除了公式解答,也可以模拟。

#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
//#define LOCAL
/*
(((()()())))
P-sequence 4 5 6666 //左边有几个左括号
W-sequence 1 1 1456 //匹配的左括号间有几个左括号
*/
stack<int> s;
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
int t,n,k,m;
int i,j;
cin>>t;
bool flag[]= {false};
for(i=; i<t; i++)
{
cin>>n;
memset(flag,,);
k=;
for(j=; j<n; j++)
{
cin>>m;
flag[k+m]=true;//k为右括号的数量(不含当前)
k++;
}
for(j=; j<k+m-; j++)
{
if(!flag[j])s.push(j);
else
{
cout<<(j-s.top()+)/<<" ";//(右括号与相应的左括号序号差+1)/2,即为中间的左括号数
s.pop();
}
}
cout<<(j-s.top()+)/<<endl;
s.pop();
}
return ;
}

HDU 1361 Parencodings(栈)的更多相关文章

  1. hdu 1361.Parencodings 解题报告

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1361 题目意思: 根据输入的P-sequence , 输出对应的W-sequence.   P-se ...

  2. hdu 1361 Parencodings 简单模拟

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

  3. hdu 4923 单调栈

    http://acm.hdu.edu.cn/showproblem.php?pid=4923 给定一个序列a,元素由0,1组成,求一个序列b,元素在0~1之间,并且保证递增.输出最小的∑(ai−bi) ...

  4. hdu 3410 单调栈

    http://acm.hdu.edu.cn/showproblem.php?pid=3410 Passing the Message Time Limit: 2000/1000 MS (Java/Ot ...

  5. hdu 1506 单调栈问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目的意思其实就是要找到一个尽可能大的矩形来完全覆盖这个矩形下的所有柱子,只能覆盖柱子,不能留空 ...

  6. HDU 3328 Flipper 栈 模拟

    首先想说,英语太烂这题读了很长时间才读懂......题意是说输入有几张牌,然后输入这些牌的初始状态(是面朝上还是面朝下),然后输入操作方式,R表示翻一下右边的牌堆,L表示翻一下左边的牌堆,直到最后摞成 ...

  7. hdu 5033 单调栈 ****

    看出来是单调栈维护斜率,但是不会写,2333,原来是和询问放在一起的 #include <iostream> #include <cstdio> #include <cs ...

  8. Train Problem I hdu 1022(栈)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1022 题意:给出火车的进站与出站顺序,判断是否可以按照给出的出站顺序出站. #include < ...

  9. HDU 5033 (单调栈维护凸包) Building

    题意: 一个人在x轴上,他的左右两侧都有高楼,给出楼的横坐标Xi和高度Hi还有人的位置pos,求人所能看到的天空的最大角度. 分析: 将建筑物和人的位置从左到右排序,对于每个位置利用栈求一次人左边建筑 ...

随机推荐

  1. java 执行redis的部分方法

    @Autowired private RedisTemplate<String, Object> redisTemplate; public void setRedisTemplate(R ...

  2. php dday1... web服务器的搭建 数据库的安装....

  3. centos设置svn开机自启动

    安装好 svn 服务后,默认是没有随系统启动自动启动的, CentOS 7 的 /etc/rc.d/rc.local 是没有执行权限的, 系统建议创建 systemd service 启动服务 于是查 ...

  4. 一点公益二码公益开发模式系统源码App

    写这篇文章,是因为最近看到的一个著名的开源项目在内部使用时的各种问题,不得不说,很多的开源的东西思想是不错的,但离真正工程化都有不小的距离,所以没什么商业公司采用的开源产品如果要引入的话一定要慎重,通 ...

  5. 从minist database(t10k-images-idx3-ubyte)中读取图片

    matlab代码(亲测,可运行出来): % Matlab_Read_t10k-images_idx3.m % 用于读取MNIST数据集中t10k-images.idx3-ubyte文件并将其转换成bm ...

  6. Submission

    EI: ICIC Express Letters: http://www.icicelb.org/elb/index.html IJICIC: http://www.ijicic.net/ijicic ...

  7. Linux下常用的压缩与解压命令

    .tar (注:tar是打包,不是压缩!) 解包: tar xvf FileName.tar 打包: tar cvf FileName.tar DirName .gz 解压1: gunzip File ...

  8. 手机端rem自适应布局实例

    首先要书写核心js代码,控制住页面的初始大小:我是以750px(即iPhone6)的标准,设置font-size:100px:<script>        (function (doc, ...

  9. fedora22 mysql安装

    fedora19以后好像取消了对mysql的支持,看其他人好像说是用的mariadb的.centos里用yum安装的方式,放到fedora中不能用,所以找了很多资料,尝试了一种可行的办法. 在Fedo ...

  10. angularJS懒加载依赖模块

    //设置 .config [ '$ocLazyLoadProvider' ($ocLazyLoadProvider) -> # We configure ocLazyLoad to use th ...