poj 1068 Parencodings
来源:互联网 发布:上海java培训周日班 编辑:程序博客网 时间:2024/05/21 09:29
题意:
一个合法的括号序列 S 可以用两种不同的序列进行编码:
( 1 ) 序列 P = p1 p2 … pn,其中 pi 表示第 i 个右括号左边有 pi 个左括号。
( 2 ) 序列 W = w1 w2 … wn , 其中 wi 表示和第 i 个右括号匹配的左括号到这个右括号间(包括这个右括号)有多少个右括号。
给出一个合法的 P 序列,求对应的 W 序列。
分析:
可以发现 第 (i -1 ) 个右括号和第 i 个右括号之间的左括号数等于 (P[i] - P[i - 1]),可以预先算出所有 num[i] = P[i] - p[i - 1],然后就可以仿照经典的括号匹配那样给右括号分配左括号了,对于第 i 个右括号,找到最近的 (num[j] > 0 && j <= i),然后 num[j]–,即分配一个左括号,过程中记录下经过了多少个右括号就行了。
#include<cstdio>using namespace std;int main() { int t, n, pre, now, num[20]; scanf("%d", &t); while(t--) { scanf("%d", &n); pre = 0; for(int i = 0; i < n; i++) { scanf("%d", &num[i]); now = num[i]; num[i] = now - pre; pre = now; } for(int i = 0; i < n; i++) { if(num[i]) { putchar('1'); num[i]--; } else { int cnt = 1, j = i; while(!num[j]) { j--; cnt++; } num[j]--; printf("%d", cnt); } putchar(' '); } puts(""); } 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
- 2、机器学习算法KMeans -- Java代码
- IOC之Autofac
- 1.1构建Android Studio开发环境
- JAVASE 学习day2
- oralce while 语句的使用
- poj 1068 Parencodings
- Gym 100512D Dynamic LCA ((LCA 在线算法DFS+ST) + 分类讨论!!)
- 【leetcode】4. Median of Two Sorted Arrays
- 如何从网站爬取图片?
- 【题】【数位动规】NKOJ1725 数字
- Android网址
- 中文分词的python实现-基于HMM算法
- 查找--顺序查找,二分法,斐波那契查找
- 浅看插入排序