hdu 1361.Parencodings 解题报告
题目链接: 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 2
(4) 6: -1 -1 -1 1 2 ---> -1 -1 4
(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 解题报告的更多相关文章
- Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5064 题目意思:给出n个数:a1, a2, ..., an,然后需要从中找出一个最长的序列 b1, b ...
- hdu 1896.Stones 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1896 题目意思:给出 n 块石头的初始位置和能到达的距离.对于第奇数次遇到的石头才抛掷,偶数次的就忽略 ...
- 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] ...
- BestCoder27 1001.Jump and Jump... (hdu 5162) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5162 题目意思:有 n 个 kid,每个 kid 有三个成绩 a, b, c.选最大的一个成绩作为这个 ...
- BestCoder27 1002.Taking Bus(hdu 5163) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5163 题目意思:有 n 个车站,给出相邻两个车站的距离,即车站 i 和车站 i+1 的距离为 di ( ...
- BestCoder25 1001.Harry and Magical Computer(hdu 5154) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5154 题目意思:有 n 门 processes(编号依次为1,2,...,n),然后给出 m 种关系: ...
- BestCoder14 1002.Harry And Dig Machine(hdu 5067) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5067 题目意思:给出一个 n * m 的方格,每一个小方格(大小为1*1)的值要么为 0 要么为一个正 ...
- hdu 1425 sort 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1425 常规的方法是对输入的数从大到小进行排序(可以用sort或qsort),然后输出前m大的数. 不过 ...
- hdu 1361 Parencodings 简单模拟
Parencodings 题意: 由括号序列S可经P规则和W规则变形为P序列和W序列. p规则是:pi是第i个右括号左边的左括号的数: w规则是:wi是第i右括号与它匹配的左括号之间右括号的数(其中包 ...
随机推荐
- app后端开发系列文章文件夹
一点废话 每个程序猿心中,都有一个大牛梦.我们在晨曦之光中敲击着代码,在寒冬覆雪中思考着0与1. 夏练三伏 冬练三九这说的就是我们这群[江湖]中人.在这里我们门派林立,C语言派历史悠久,在程序界就是嵩 ...
- Maven plugin提示错误“Plugin execution not covered by lifecycle configuration”
myeclipse在其POM文件的一处提示出错如下: Plugin execution not covered by lifecycle configuration: org.apache.maven ...
- Android API Guides---Services
服务 在该文献 基础 声明在清单服务 创建一个启动的服务 扩展IntentService类 扩展服务类 启动服务 停止服务 创建绑定服务 将通知发送给用户 执行在前台服务 管理服务生命周期 实施生命周 ...
- 谈谈Runtime类中的freeMemory,totalMemory,maxMemory几个方法
最近在网上看到一些人讨论到java.lang.Runtime类中的freeMemory(),totalMemory(),maxMemory ()这几个方法的一些问题,很多人感到很疑惑,为什么,在jav ...
- react 自定义 TabBar 组件
1.创建 组件 src/components/TabBar/index.js /** * TabBar 组件 */ import React ,{ PureComponent } from 'reac ...
- Anacoda 介绍、安装、环境切换
官网下载 概述 很多学习python的初学者甚至学了有一段时间的人接触到anaconda或者其他虚拟环境工具时觉得无从下手, 其主要原因就是不明白这些工具究竟有什么用, 是用来做什么的, 为什么要这么 ...
- mysql + php 中文乱码 全是? 解决方法
在my.ini文件中找到[client]和[mysqld]字段,在下面均加上default-character-set=utf8,保存并关闭,重启服务器 在window下重启失败,这是因为你安装了高版 ...
- jvm基础(2)
7.类装载器 (1)class装载验证流程: A加载.这是装载类的第一个阶段,执行的动作包括:取得类的二进制流,转为方法区数据结构,在java堆中生成对应的java.lang.Class对象. B链接 ...
- git mirror的创建与使用
please donwload repo mirro as follow steps, thanks 1.mirror server,server IP:192.168.0.123 1.1 -- de ...
- python--函数程序分析
写函数,用户传入修改的文件名,与要修改的内容,执行函数,完成整个文件的批量修改操作 import os #加载模块 def xiu(a,b,c): #三个接受值的形参 f = open(a,encod ...