题目链接

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. 将Cygwin Emacs设为Windows explorer默认打开程序

    由于我在平日的学习与工作中会经常用到Cygwin中的Emacs,很自然地想到应该将emacsclient作为指定文件类型在Windows explorer中的默认打开程序.这样,便可以直接双击文件后在 ...

  2. jQ内容的强大,后面继续跟进...

    <script type="text/javascript" src="jQ/jquery.js"></script> <scri ...

  3. 解决 maven项目问题 An error occurred while filtering resources

    解决方法: Maven -> Update Project.

  4. XML+AJAX

  5. Mammoth官方文档翻译

    用于.NET的.docx转HTML的Mammoth Mammoth可用于将.docx文档(比如由Microsoft Word创建的)转换为HTML.Mammoth致力于通过文档中的语义信息生成简洁的H ...

  6. java中的URL InetAddress类

    /* * InetAddress类: * 用于标识网络上的硬件资源,表示互联网协议(ip)地址,是java对ip地址的封装. * 其实例对象包含以数字形式保存的IP地址,主机名. * InetAddr ...

  7. bzoj3156防御准备

    3156: 防御准备 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1349  Solved: 605[Submit][Status][Discuss ...

  8. 解决 bootstrap 在IE8下的兼容问题

    <meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="v ...

  9. WordPress网站加速优化,一键免费使用七牛CDN插件

    利用wordpress搭建网站是个人建站的主流方案,我曾分享过wordpress网站加速优化必做的十件事,帮助了不少个人站长.今天介绍帮助wordpress网站提升速度至少10倍的免费CDN加速插件: ...

  10. Pelican主题配置:elegant

    简介 elegant是Mac风格的优秀主题,简单,专注文章本身. A responsive, minimal, and stylish theme for Pelican:https://github ...