题目链接: 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. Failed to check the status of the service报错解决

    报这个错误是因为我的application_context.service.xml 文件里的的dubbo声明暴露口时的ref属性写错了. <dubbo:service interface=&qu ...

  2. .net 哈希

    using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptograph ...

  3. iPhone 证书导出分享给多个开发人员操作

    1.应用程序->实用工具->keychain Access->操作如图1 2.将上图导出的证书.AppleWWDRCA.cer.mythlinkDeveloper.mobilepro ...

  4. android清除缓存为什么总是存在12k?

    转载请注明出处:http://blog.csdn.net/droyon/article/details/41116529 android手机在4.2之后.清除缓存总是会残留12k的大小.预计强迫症患者 ...

  5. iOS开发 准确计算Coretext高度

    - (int)getAttributedStringHeightWithString:(NSAttributedString *)  string  WidthValue:(int) width{   ...

  6. ccs 中的定位

    一.相对定位 position:relative; 作用: 相对定位 一般加给定位元素父级    特点: (1)不脱离文档流: (2)不改变元素类型: (3)参照物是元素本身: 二.绝对定位 posi ...

  7. 【翻译自mos文章】当并行事务恢复进程在执行时,禁用并行事务恢复的方法

    当并行事务恢复进程在执行时,禁用并行事务恢复的方法 How to Disable Parallel Transaction Recovery When Parallel Txn Recovery is ...

  8. 常见Struts、Hibernate、Spring、J2EE、ibatis、Oracle等开发框架架构图及其简介

    各种系统架构图及其简介 转载请保留出处,不胜人生一场醉汇总. 以下文字和架构图均在本人相关系统设计和架构方案中有所应用. 原文出处:http://space.itpub.net/6517/viewsp ...

  9. IDEA 适用技巧

    快速打印 sout - System.out.println(); serr - System.err.println(); //在控制台中是红色的 输入sout,按下enter键,生成System. ...

  10. Django+uwsgi+nginx+angular.js项目部署

    这次部署的前后端分离的项目: 前端采用angular.js,后端采用Django(restframework),他俩之间主要以json数据作为交互 Django+uwsgi的配置可以参考我之前的博客: ...