POJ 1068--Parencodings
来源:互联网 发布:ubuntu更改系统语言 编辑:程序博客网 时间:2024/06/05 16:03
2017-10-05
原题链接
题目大意:
给出一串数字(P-sequence),分别表示一个仅由两两配对的左右括号队列(S)中,每个右括号之前的左括号数目。求位于每个右括号以及与它配对的左括号之间的右括号的数目(W-sequence)。
示例:
S (((()()()))) P-sequence 4 5 6666 W-sequence 1 1 1456
样例输入:
264 5 6 6 6 69 4 6 6 6 6 8 9 9 9
样例输出:
1 1 1 4 5 61 1 2 4 5 1 1 3 9
本题的关键在于记录括号的配对情况,因为一个左括号必然对应到一个右括号,所以把未配对的左括号位置压入堆栈,每出现一个右括号,即弹出栈顶(也就是离它最近的左括号)位置,便能方便地求出符合条件的右括号数目。
参考题解
代码:
#include <iostream>#include <stack>using namespace std;bool Pqueue[1000]; //构造括号队列stack<int> paren;int main() { //kaseNum:输入样例组数; everyNum:每组输入样例的数据个数; int kaseNum,everyNum; cin >> kaseNum; while(kaseNum > 0) { //这里对Pqueue进行初始化是必须的,因为Pqueue是全局变量 memset(Pqueue,false,sizeof(Pqueue)); cin >> everyNum; int tp,i; int Rnum = 0; //记录右括号的个数 for(i = 0; i < everyNum; i++) { cin >> tp; //记录右括号的位置 Pqueue[tp+Rnum] = true; Rnum++; } for(i = 0; i < Rnum+tp-1; i++) { if(!Pqueue[i]) { paren.push(i); } else { cout << (i - paren.top() + 1) / 2 << " "; paren.pop(); } } cout << (i - paren.top() + 1) / 2 << endl; paren.pop(); --kaseNum; } return 0;}
阅读全文
0 0
- poj 1068 Parencodings
- POJ 1068 Parencodings
- POJ 1068 Parencodings
- poj 1068 Parencodings
- POJ 1068 Parencodings
- POJ 1068 Parencodings
- POJ 1068 Parencodings 模拟
- POJ 1068 Parencodings
- poj 1068 Parencodings
- poj 1068 Parencodings
- poj 1068 Parencodings
- poj 1068 Parencodings
- poj-1068-Parencodings
- POJ 1068 Parencodings
- POJ 1068 Parencodings
- POJ 1068 Parencodings
- POJ-1068-Parencodings
- poj 1068 Parencodings
- rasa_core:基于机器学习的对话引擎
- VMWare运行时报错“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态”的解决方案
- Selenium在添加cookie时“Can only set Cookies for the current domain”错误处理
- HDU
- 八皇后
- POJ 1068--Parencodings
- LightOj1285
- HDU
- [转]PHP Redis基本命令查找
- unity中 字符串与类中GameObject变量的相互引用
- 虽然微不足道但是很有用的JAVA基础—流程控制
- php入门简介
- 卷积神经网络CNN经典模型整理Lenet,Alexnet,Googlenet,VGG,Deep Residual Learning
- 链表的删除和插入