题目链接

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. QGIS

    project(GisFreeMap) set(CMAKE_BUILD_TYPE Debug) find_package(Qt4 REQUIRED QtCore QtGui QtXml) includ ...

  2. java 执行redis的部分方法

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

  3. memcahced缓存特点

    1.key-value数据结构 2.所有数据保存在内存中 3.可以分布式集群 4.处理并发的机制是libevent事件机制 5.当内容容量达到指定值后,就基于LRU(Least Recently Us ...

  4. FTP上传下载工具(FlashFXP) v5.5.0 中文版

    软件名称: FTP上传下载工具(FlashFXP) 软件语言: 简体中文 授权方式: 免费试用 运行环境: Win 32位/64位 软件大小: 7.4MB 图片预览: 软件简介: FlashFXP 是 ...

  5. 自定义实现IEnumerable

    Demo: http://files.cnblogs.com/files/georgeHeaven/Demo.IEnumerable.rar 一.使用场景 在开发过程中,经常需要使用foreach来循 ...

  6. 前端知识点-CSS相关知识点

    1.解释一下CSS的盒子模型? 回答一: a.标准的css盒子模型:宽度=内容的宽度+边框的宽度+加上内边具的宽度 b.网页设计中常听的属性名:内容(content).填充(padding).边框(b ...

  7. Android对px和dip进行尺寸转换的方法

    px  转换为 dip /** * PX 转换为 DP * * @param context * @param px * @return */ public static int px2dp(Cont ...

  8. hdu 3006 The Number of set

    二进制的状态压缩.比如A集合里面有{1,5,7}那么就表示为1010001.B集合有{3,4},二进制表示1100.A|B=1011101. 按照这样的思路 可以用01背包 把所有的组合全部求出来. ...

  9. "The Application was unable to start correctly (0xc000007b). Click OK to close the application"

    我有时将MFC编译成64位并运行,就会报这个错误. 后来查找原因,就在于系统中使用了错误的dll.比如这个程序要使用64位的dll,而你拷贝进去的是同名的32位dll.解决方法就是放置正确的dll. ...

  10. mac 搭建node 开发环境记录

    安装homebrew: enter 键 后 输入电脑密码 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/i ...