POJ 1068 Parencodings
2016-05-05 23:39
246 查看
POJ 1068 Parencodings
[★★☆☆☆] 模拟 构造
题目大意:
一个括号表达式可以按照如下的规则表示,就是每个右括号之前的左括号数。比如(((()()()))),每个右括号之前的左括号数序列为P=4 5 6 6 6 6,而每个右括号所在的括号内包含的括号数为W=1 1 1 4 5 6.
现在给定P,输出W。
样例
输入:2
6
4 5 6 6 6 6
9
4 6 6 6 6 8 9 9 9
输出:
1 1 1 4 5 6
1 1 2 4 5 1 1 3 9
解题思路:
水题,根据P序列画出括号序列,再根据括号序列写出W序列。代码
#include <iostream> using namespace std; char S[50]; int sz; int P[21]; int main() { int TT; cin >> TT; P[0] = 0; while (TT--) { sz = 0; int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> P[i]; for (int j = 1; j <= P[i]-P[i-1]; j++) { S[sz++] = '('; } S[sz++] = ')'; } for (int i = 0; i < sz-1; i++) { if (S[i] == ')') { int res = 1; int t = 1; for (int j = i - 1; j >= 0; j--) { if (S[j] == '(') { t--; if (t == 0) { cout << res << ' '; break; } } if (S[j] == ')') { t++; res++; } } } } int i = sz-1; if (S[i] == ')') { int res = 1; int t = 1; for (int j = i - 1; j >= 0; j--) { if (S[j] == '(') { t--; if (t == 0) { cout << res << endl; break; } } if (S[j] == ')') { t++; res++; } } } } }
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- POJ1050 最大子矩阵和
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)
- POJ 1200 Crazy Search(简单哈希)