Hihocoder1458-Parentheses Matching(stack,vector)
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
Given a string of balanced parentheses output all the matching pairs.
输入
A string consisting of only parentheses ‘(‘ and ‘)’. The parentheses are balanced and the length of the string is no more than 100000.
输出
For each pair of matched parentheses output their positions in the string.
样例输入
(())()()
样例输出
1 4
2 3
5 6
7 8
题意
输出可以匹配的每一对括号的位置,按照左括号位置升序输出
思路
用stack标记’(‘的位置,扫一遍,遇到’)’就匹配最近的’(‘,用vector来存数对。
代码
#include<bits/stdc++.h>
using namespace std; int main() {
stack<int> p;
char s[];
while(~scanf("%s", s)) {
vector<pair<int, int> > aa;
int len = strlen(s);
while(!p.empty()) p.pop();
for(int i = ; i < len; i++) {
if(s[i] == '(')
p.push(i + );
else if(s[i] == ')') {
int x = p.top(); p.pop();
aa.push_back(pair<int, int>(x, i + ));
}
}
sort(aa.begin(), aa.end());
for(int i = ; i < aa.size(); i++) {
printf("%d %d\n", aa[i].first, aa[i].second);
}
}
return ;
}
Hihocoder1458-Parentheses Matching(stack,vector)的更多相关文章
- ArrayList,LinkedList,Vector,Stack之间的区别
一,线程安全性 Vector.Stack:线程安全 ArrayList.LinkedList:非线程安全 二,实现方式 LinkedList:双向链表 ArrayList,Vector,Stack:数 ...
- 16、Collection接口及其子接口Set和List(常用类LinkedList,ArrayList,Vector和Stack)
16.Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同 ...
- 5.1 stack,queue以及priority_queue
*:stack 使用要包含头文件stack,栈是一种先进后出的元素序列,删除和访问只能对栈顶的元素(最后一个添加的元素)进行,并且添加元素只能添加到栈顶.栈内的元素不能访问,要想访问先要删除其上方的所 ...
- 【转】java 容器类使用 Collection,Map,HashMap,hashTable,TreeMap,List,Vector,ArrayList的区别
原文网址:http://www.360doc.com/content/15/0427/22/1709014_466468021.shtml java 容器类使用 Collection,Map,Hash ...
- Stack和Vector源码分析
Stack和Vector源码分析 Stack和Vector源码分析stack源码分析1.Stack是什么2.Stack的结构图3.Stack继承关系4.Stack的主要方法5.Stack源码Vecto ...
- Java容器:Stack,Queue,PriorityQueue和BlockingQueue
Stack Queue PriorityQueue BlockingQueue ArrayBlockingQueue LinkedBlockingQueue PriorityBlockingQueue ...
- 顺序容器----顺序容器操作,vector对象如何增长,额外的string操作,容器适配器
一.顺序容器操作 1.向顺序容器添加元素 向顺序容器(array除外)添加元素的操作: 操作 说明 c.push_back(t) 在c的尾部创建一个值为t的元素.返回void c.emplace_ba ...
- java:容器/集合Collection(List(ArrayList,LinkedList,Vector),Set(HashSet(LinkedHashSet),TreeSet))
/** * Collection接口 不唯一,无序 * 常用的方法: * add(Object e) 确保此 collection 包含指定的元素(可选操作). * size():获取集合中元素的个 ...
- Stack,ArrayDeque,LinkedList的区别
本文首发于cartoon的博客 转载请注明出处:https://cartoonyu.github.io/cartoon-blog 这段时间把疯狂JAVA再看了一遍,发现Stac ...
随机推荐
- 前端开发—CSS
CSS 基础概念 致命三问: 它是什么? 层叠样式表,主要作用是对html标签进行装饰. 它的作用:再 html 框架的基础上 ,对标签内容做美化工作. 注释方法:/*单行注释*/ 多行注释同理与h ...
- loadrunner笔记----好记性不如烂笔头
1.Loadrunner主要由Vugen,Controller和Analyais3部分组成 2.简述描述集合点和集合点函数 集合点可以同步虚拟用户,以便能在同一时刻执行任务,集合点函数lr_rende ...
- 前端异步编程之Promise和async的用法
传统的异步解决方案采用回调函数和事件监听的方式,而这里主要记录两种异步编程的新方案: ES6的新语法Promise ES2017引入的async函数 Generator函数(略) Promise的含义 ...
- UVA227 - Puzzle(紫书习题3.5)
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring&g ...
- [洛谷P4887]第十四分块(前体)
题目大意: 给定一个长度为\(n\)的序列\(a\),\(k\),和\(m\)次询问. 每次询问给定区间\([l,r]\),求满足\(l\leqslant i< j\leqslant r\)且\ ...
- django迁移数据库报错解决
迁移数据库时提示之前的项目中模型未引入 如图 我在创建新的工程时,迁移数据模型时发现出错,错误提示关联模型未被解决,提示的模型是之前项目中定义的,本项目并没有用到.于是在不知道错误原因下,我重装dja ...
- Python编程:从入门到实践 - matplotlib篇 - Random Flow
随机漫游 # random_flow.py 随机漫游 import random class RandomFlow(): """一个生成随机漫游数据的类"&qu ...
- springmvc上传操作
创建虚拟目录 配置tomcat的配置文件server.xml 在真实路径中放置一个图片 启动服务器: 直接可以通过配置的虚拟路径来访问真实路径中的图片 所以 我们在做图片上传的操作的时候 就可 ...
- C++ auto类型说明符
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50864612 编程时常常需要把表达式的 ...
- JS中的call()(转)
1.方法定义 call方法: 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 说明: call 方 ...