题目链接: 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. nginx--cookies转发

    nginx根据cookie分流   nginx根据cookie分流众所周知,nginx可以根据url path进行分流,殊不知对于cookie分流也很强大,同时这也是我上篇提到的小流量实验的基础. 二 ...

  2. hdoj 4828 卡特兰数取模

    Grids Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Sub ...

  3. 简化动态MERGE的SQL计算

    MSSQL.ORACLE等数据库支持MERGE语句更新表.但表结构未知时,因为缺乏集合类数据.用存储过程获得表结构再动态拼出SQL很麻烦,代码会有几十行之多:相同原因,用Java等高级语言实现也不简单 ...

  4. Cts框架解析(15)-任务运行完

    case运行完成后.会回到CtsTest的run方法中: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaXRmb290YmFsbA==/font/5a6L ...

  5. 负载均衡之F5设备

    http://xjsunjie.blog.51cto.com/999372/666672 目前全球范围内应用比较广泛的负载均衡设备为美国的F5.F5于2000年底进驻中国,在国内业界,F5负载均衡产品 ...

  6. C# Select SelectMany 区别

    string[] text = { "Today is 2018-06-06", "weather is sunny", "I am happy&qu ...

  7. 【转】Windows2008上传大文件的解决方法(iis7解决上传大容量文件)

    2008上传大文件的解决方法:http://wenku.it168.com/d_000091739.shtml 2003上传大文件的解决方法:http://tech.v01.cn/windowsxit ...

  8. C语言关键字—-sizeof 、typedef、const、static、register、extern、#define

    关键字:sizeof .#define.typedef.const.static.register.extern sizeof 1. 作用:求数据所占得内存空间大小 2. 本质:求数据得类型所占的内存 ...

  9. web前端面试系列 一 js闭包

    一.什么是闭包? JavaScript高级程序设计第三版: 闭包是指有权访问另一个函数作用域中的变量的函数. 在js中定义在函数内部的子函数能够访问外部函数定义的变量,因此js内部函数就是一个闭包. ...

  10. javaScript中innerHTML,innerText,outerHTML,outerText的区别

    开头说下innerText和outerText只在chrome浏览器中有效 定义和用法 innerHTML 属性设置或返回表格行的开始和结束标签之间的 HTML,包括标签. 来看代码 <!DOC ...