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右括号与它匹配的左括号之间右括号的数(其中包 ...
随机推荐
- 为什么HierachyViewer无法连接真机调试
关于什么是Hierarchy Viewer,请查看官方文档:http://developer.android.com/tools/debugging/debugging-ui.html.个人理解:Hi ...
- 设计模式之状态模式(State)摘录
23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程,它们帮助一个系统独立于怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...
- 如何提高NodeJS程序的运行的稳定性
如何提高NodeJS程序运行的稳定性 我们常通过node app.js方式运行nodejs程序,但总有一些异常或错误导致程序运行停止退出.如何保证node程序的稳定运行? 下面是一些可以考虑的方案: ...
- 从SDCard获取的图片按分辨率处理的方法
前段时间公司开发的Launcher要做主题切换的功能,但切换主题时须要从sdcard中获取要切换的图片资源,拿到后图片的大小不正常. 后来查找原因是:系统对不同分辨率拿到的图片资源会自己主动的做转化, ...
- PHP compact() 函数
Compact ---- 创建一个包含变量名和它们的值的数组: <?php $firstname = "Bill"; $lastname = "Gates" ...
- AtomicInteger在实际项目中的应用
AtomicInteger.一个提供原子操作的Integer的类. 在Java语言中,++i和i++操作并非线程安全的.在使用的时候,不可避免的会用到synchronized关键字. 而AtomicI ...
- PC常用电源IC、MOS、三极管、二极管厂家
笔记本常用MOS.三极管.二极管厂家: 1.EMC 杰力电子(台湾)官方网站:http://www.excelliancemos.com/tw/solution.php 2.UBIQ(台湾电源厂家UP ...
- javascript 高级编程系列 - 继承
1. 原型链继承 (缺点:子类继承父类的引用类型的属性值会在各个实例中共享,创建子类实例时无法向父类构造函数传递参数) // 定义父类构造函数 function SuperClass(father, ...
- Mataplotlib绘图和可视化
Mataplotlib是一个强大的python绘图和数据可视化工具包 安装方法:pip install matplotlib 引用方法:import matplotlib.pyplot as plt ...
- linux c 网络编程:用域名获取IP地址或者用IP获取域名 网络地址转换成整型 主机字符顺序与网络字节顺序的转换
用域名获取IP地址或者用IP获取域名 #include<stdio.h> #include<sys/socket.h> #include<netdb.h> int ...