题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1361

题目意思: 根据输入的P-sequence , 输出对应的W-sequence.   P-sequence: 表示每个右括号前有多少个左括号;   W-sequence: 表示每个右括号要经过多少个左括号才能找到它能够匹配的左括号.

可以通过栈来做,边输入边处理.假设左括号用-1表示, 已经匹配好的括号(即 () ) 用1表示.那么,如果是左括号的话,就把-1压进去.直到找到匹配的括号.

以样例数据:  4 6 6 6 6 8 9 9 9    为例子

它的输入是这样的:    ( ( ( ( ) ( ( ) ) ) ) ( ( ) ( ) ) )

左边加粗部分表示当前这个右括号的对应左括号,右边加粗部分表示答案

(1)  4:  -1  -1  -1  -1                      --->              -1  -1  -1   1

(2)  6:  -1  -1  -1  1  -1  -1             --->              -1  -1  -1   1  -1  1

(3)  6:  -1  -1  -1   1  -1  1        --->              -1   -1  -1   1  

(4)  6:  -1   -1  -1  1   2        --->              -1   -1  

(5)  6:  -1   -1   4                           --->              -1   5

      (6)  8:  -1  5  -1  -1                        --->              -1   5  -1   1

(7)  9:  -1   5  -1  1  -1                   --->              -1   5  -1  1  1

      (8)  9:   -1   5  -1  1   1        --->              -1   5   3

      (9)  9:   -1   5   3                            --->              9

     

 #include <iostream>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <stack>
using namespace std; stack<int> s; int main()
{
int i, j, n, t, sum, past, cur;
while (scanf("%d", &t) != EOF)
{
while (t--)
{
scanf("%d", &n);
past = cur = ;
for (i = ; i < n; i++)
{
scanf("%d", &cur);
for (j = ; j < cur-past; j++)
s.push(-);
if (s.top() == -) // 找到匹配的那个括号
{
s.pop();
s.push();
}
else
{
sum = s.top();
s.pop();
while ()
{
if (s.top() == -)
break;
sum += s.top();
s.pop();
}
s.pop(); // 弹出匹配的那个括号
s.push(sum+);
}
if (!i)
cout << s.top();
else
cout << " " << s.top();
past = cur;
}
putchar('\n');
while (!s.empty())
s.pop();
}
}
return ;
}

hdu 1361.Parencodings 解题报告的更多相关文章

  1. Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5064 题目意思:给出n个数:a1, a2, ..., an,然后需要从中找出一个最长的序列 b1, b ...

  2. hdu 1896.Stones 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1896 题目意思:给出 n 块石头的初始位置和能到达的距离.对于第奇数次遇到的石头才抛掷,偶数次的就忽略 ...

  3. Valentine's Day Round 1001.Ferries Wheel(hdu 5174)解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5174 题目意思:给出 n 个人坐的缆车值,假设有 k 个缆车,缆车值 A[i] 需要满足:A[i−1] ...

  4. BestCoder27 1001.Jump and Jump... (hdu 5162) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5162 题目意思:有 n 个 kid,每个 kid 有三个成绩 a, b, c.选最大的一个成绩作为这个 ...

  5. BestCoder27 1002.Taking Bus(hdu 5163) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5163 题目意思:有 n 个车站,给出相邻两个车站的距离,即车站 i 和车站 i+1 的距离为 di ( ...

  6. BestCoder25 1001.Harry and Magical Computer(hdu 5154) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5154 题目意思:有 n 门 processes(编号依次为1,2,...,n),然后给出 m 种关系: ...

  7. BestCoder14 1002.Harry And Dig Machine(hdu 5067) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5067 题目意思:给出一个 n * m 的方格,每一个小方格(大小为1*1)的值要么为 0 要么为一个正 ...

  8. hdu 1425 sort 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1425 常规的方法是对输入的数从大到小进行排序(可以用sort或qsort),然后输出前m大的数. 不过 ...

  9. hdu 1361 Parencodings 简单模拟

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

随机推荐

  1. 奇酷手机显示Log

    1.在桌面点击拨号,在拨号盘输入“*20121220#”,进入工程模式;2.看到日志输出等级,点进去 Log print enable 选 enable Java log level 选 LOGV C ...

  2. C# 将long类型写入二进制文件用bw.Write(num);将其读出用long num= br.ReadInt64();

    理由: 因为long类型是 System.Int64 (长整型,占 8 字节,表示 64 位整数,范围大约 -(10 的 19) 次方 到 10 的 19 次方) 而long BinaryReader ...

  3. Html5学习笔记1 元素 标签 属性

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. jstl的函数

    <%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"%><%@ tag ...

  5. 合并SO为单独交货单

    本场景为单步交货     为客户建立专用的route.     增加一个pull rule         在做订单的时候,为订单行选择 上面建立好的route,     连续建立了 2个 订单 SO ...

  6. Java基础:抽象类和接口

    转载请注明出处:jiq•钦's technical Blog 一.引言 基于面向对象五大原则中的以下两个原则,我们应该多考虑使用接口和抽象类: 里氏替换原则:子类能够通过实现父类接口来替换父类,所以父 ...

  7. c语言知识点总结-------静态区、堆、栈、常量区等

    在C语言中地址占4个字节 1.编程语言发展 低级语言----->高级语言 机器语言 ---> 汇编---->高级语言(C语言.C++.JAVA等) 机器语言 :0101 0010 1 ...

  8. 阿里云OSS对象存储 简单上传文件

    不得不说阿里云的命名比较让人摸不着头脑,开始以为是文件存储NAS,弄了半天什么文件系统,挂载点的搞不明白.后来才搞清楚原来 对象存储OSS才是我需要的. 其中EndPoint就是画红框的部分,但是要加 ...

  9. C# 之 集合ArrayList

    .NET Framework提供了用于数据存储和检索的专用类,这些类统称集合. 这些类提供对堆栈.队列.列表和哈希表的支持.大多数集合类实现系统的接口.以下我们主要来讲一下ArrayList.     ...

  10. Android 短信验证码控件

    Android 短信验证码控件,便于项目中使用统一样式,统一提示改动.个人觉得挺好用的 <span style="font-size:18px;">public cla ...