(括号匹配,栈,队列)湘大校赛,H.括号匹配
来源:互联网 发布:js载入html 编辑:程序博客网 时间:2024/05/22 12:30
括号匹配
Accepted : 26 Submit : 210Time Limit : 10000 MS Memory Limit : 65536 KB题目描述
有一串括号(只包含"(", ")", "[", "]", "{", "}"), 保证这一括号串是匹配的, 长度小于100000, 有m个询问, 每个询问为一个整数x;对于每一个询问, 要求输出一行四个整数y, a, b, c; 表示第x个括号是跟第y个配对的, x和y之间有a对小括号, b对中括号, c对大括号。
输入
约200个样例, 每个样例第一行为一个只包含括号的长度小于100000的字符串, 第二行一个小于100000的整数m, 接下来m行每行一个整数x,x不大于括号字符串的长度, x > 0;
输出
每个询问输出一行四个整数y, a, b, c,用空格隔开。每个样例之后输出一个空行
样例输入
(){}[]3135[([{()[]}])][()]51251314
样例输出
2 0 0 04 0 0 06 0 0 012 2 2 111 1 2 16 0 0 016 1 0 015 0 0 0
<pre name="code" class="cpp">/*H.括号匹配链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1232题意:如题。分析:用栈解决“左右括号的匹配问题”,用队列解决“包含多少对括号的问题”。(下面用了3个栈,不过应该一个栈就ok了)*/#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int maxn = 100005;int q[3][maxn], s[3][maxn];int qt[3], st[3];int mat[256];char str[maxn];int ans[maxn];void init(){ mat['('] = 0; mat['['] = 1; mat['{'] = 2; mat[')'] = 3; mat[']'] = 4; mat['}'] = 5; for (int i = 0; i<3; i++) qt[i] = st[i] = 0;}int main(){ //freopen("input.txt","r",stdin); while (~scanf("%s", str + 1)) { init(); int len = strlen(str + 1); for (int i = 1; i <= len; i++) { int n = mat[str[i]]; if (n < 3)//如果是左括号 { s[n][st[n]++] = i;//进栈 q[n][qt[n]++] = i;//进队 } else//如果是右括号 { int L = s[n - 3][--st[n - 3]];//取栈顶元素并出栈 ans[i] = L; ans[L] = i; } } /*------------------------------------------*/ int m, x, a[3]; scanf("%d", &m); for (int i = 0; i<m; i++) { scanf("%d", &x); int n = mat[str[x]]; int low = min(x, ans[x]); int up = max(x, ans[x]); /*查找low和up之间有多少对括号*/ for (int j = 0; j<3; j++) { int L = upper_bound(q[j], q[j] + qt[j], low) - q[j]; int R = lower_bound(q[j], q[j] + qt[j], up) - q[j]; a[j] = R - L; } printf("%d %d %d %d\n", ans[x], a[0], a[1], a[2]); } printf("\n"); } return 0;}
0 0
- (括号匹配,栈,队列)湘大校赛,H.括号匹配
- 栈和队列---括号匹配
- [栈和队列]括号匹配
- 括号匹配(栈和队列)
- 括号匹配(栈)
- 括号匹配(栈)
- 括号匹配(栈)
- 括号括号括号匹配
- 栈和队列应用之括号匹配
- YTU 2238: 括号匹配(栈和队列)
- 括号匹配(栈实现)
- 括号匹配(栈操作)
- 括号匹配(栈练习)
- 最长括号匹配(栈)
- YTU 3003: 括号匹配(栈和队列)
- Problem C: 括号匹配(栈和队列)
- Problem C: 括号匹配(栈和队列)
- 栈---括号匹配
- Hive中的排序语法
- 黑马程序员----GUI、网络编程
- 用Dijkstra算法输出最短路径以及对应的最小权值(C语言版)
- 位运算简介及实用技巧(一):基础篇
- LeetCode-Repeated DNA Sequences
- (括号匹配,栈,队列)湘大校赛,H.括号匹配
- 位运算简介及实用技巧(二):进阶篇(1)
- 第十四章 Iterations and Comprehensions
- 位运算简介及实用技巧(三):进阶篇(2)
- BZOJ1010 [HNOI2008]玩具装箱toy(斜率优化dp)
- 注解的作用目标 类(接口 枚举) 方法 构造器 参数 局部变量 包(但是不能直接放在上面)
- 位运算简介及实用技巧(四):实战篇
- POJ 2506 Tiling -递推大数
- 2015 湘潭大学程序设计比赛(Internet)Problem D:最小的数