题目链接

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. OpenCV备忘

    都是转来的内容的,算是整理一下 OpenCV备忘 深度和通道的理解 CV_8UC1 是指一个8位无符号整型单通道矩阵, CV_32FC2是指一个32位浮点型双通道矩阵 CV_8UC1 CV_8SC1 ...

  2. jsp中使用java函数

    <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> ${fn ...

  3. tomcat服务配置及搭建

    一.在官网上下载tomcat 下载地址:http://tomcat.apache.org/download-60.cgi 下载完后解压 二.设置环境变量 1,JAVA_HOME 2.CATALINA_ ...

  4. hdu 3669 Cross the Wall(斜率优化DP)

    题目连接:hdu 3669 Cross the Wall 题意: 现在有一面无限大的墙,现在有n个人,每个人都能看成一个矩形,宽是w,高是h,现在这n个人要通过这面墙,现在只能让你挖k个洞,每个洞不能 ...

  5. C#中的引用传递、值传递

      先来说下C#中的数据类型.分值类型和引用类型两大类. 值类型:直接存储数据的值,保存在内存中 引用类型:存储对值的引用,实际上存储的就是一个内存的地址 C#预定义的简单类型,像int,float, ...

  6. C# 语言规范_版本5.0 (第13章 接口)

    1. 接口 一个接口定义一个协定.实现某接口的类或结构必须遵守该接口定义的协定.一个接口可以从多个基接口继承,而一个类或结构可以实现多个接口. 接口可以包含方法.属性.事件和索引器.接口本身不提供它所 ...

  7. sql第二天

    --基本格式 select * from tblclass --对于列进行限制 --格式一:取指定列 select cid,cname from TblClass select cname from ...

  8. ios发布笔录

    需要一张1024x1024的icon 发布尺寸4.7英寸  1334x7505.5英寸 2208-12424英寸  1136-6403.5英寸 960-640ipad  2048x1536 视频 ip ...

  9. uiscrollview 事件冲突

    self.scrollView.panGestureRecognizer.delaysTouchesBegan = YES;设置scrollView的延迟接收点击触摸事件,关闭触摸事件 self.sc ...

  10. Java 服务器端手机验证码sdk

    感谢巨人们,站在巨人的肩膀上. 参考: http://blog.sina.com.cn/s/blog_80a6423d0102wm74.html#commonComment 1 点击生成验证码,发送到 ...