时间限制: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)的更多相关文章

  1. ArrayList,LinkedList,Vector,Stack之间的区别

    一,线程安全性 Vector.Stack:线程安全 ArrayList.LinkedList:非线程安全 二,实现方式 LinkedList:双向链表 ArrayList,Vector,Stack:数 ...

  2. 16、Collection接口及其子接口Set和List(常用类LinkedList,ArrayList,Vector和Stack)

    16.Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同 ...

  3. 5.1 stack,queue以及priority_queue

    *:stack 使用要包含头文件stack,栈是一种先进后出的元素序列,删除和访问只能对栈顶的元素(最后一个添加的元素)进行,并且添加元素只能添加到栈顶.栈内的元素不能访问,要想访问先要删除其上方的所 ...

  4. 【转】java 容器类使用 Collection,Map,HashMap,hashTable,TreeMap,List,Vector,ArrayList的区别

    原文网址:http://www.360doc.com/content/15/0427/22/1709014_466468021.shtml java 容器类使用 Collection,Map,Hash ...

  5. Stack和Vector源码分析

    Stack和Vector源码分析 Stack和Vector源码分析stack源码分析1.Stack是什么2.Stack的结构图3.Stack继承关系4.Stack的主要方法5.Stack源码Vecto ...

  6. Java容器:Stack,Queue,PriorityQueue和BlockingQueue

    Stack Queue PriorityQueue BlockingQueue ArrayBlockingQueue LinkedBlockingQueue PriorityBlockingQueue ...

  7. 顺序容器----顺序容器操作,vector对象如何增长,额外的string操作,容器适配器

    一.顺序容器操作 1.向顺序容器添加元素 向顺序容器(array除外)添加元素的操作: 操作 说明 c.push_back(t) 在c的尾部创建一个值为t的元素.返回void c.emplace_ba ...

  8. java:容器/集合Collection(List(ArrayList,LinkedList,Vector),Set(HashSet(LinkedHashSet),TreeSet))

    /** * Collection接口  不唯一,无序 * 常用的方法: * add(Object e) 确保此 collection 包含指定的元素(可选操作). * size():获取集合中元素的个 ...

  9. Stack,ArrayDeque,LinkedList的区别

        本文首发于cartoon的博客     转载请注明出处:https://cartoonyu.github.io/cartoon-blog     这段时间把疯狂JAVA再看了一遍,发现Stac ...

随机推荐

  1. IT级别

    IT领袖:年入过亿(例如任正非.马化腾.李彦宏.丁磊.马云等,包括期权股票以及投资理财等收入.) IT大哥:年入千万(级别次于以上几位大佬的公司老板,不缺钱,普遍对上一条里的人物羡慕嫉妒恨.) IT精 ...

  2. sass揭秘之@mixin,%,@function(转载)

    因为文章内含有很多sass代码,如需自己动手查看编译结果,推荐使用sassmeister这款在线编译工具,方便你阅读学习. 在阅读本文章之前,请先确认你已经阅读了上篇文章sass揭秘之变量,不然会给你 ...

  3. Flex简易教程

      常见的前端布局模型涵盖浮动.定位和弹性盒等 CSS 技术,其中浮动和定位技术往往在制作自适应布局页面时显得不够优雅--对于浮动布局,前后端分离时代很多时候我们并不知道每行会遍历显示多少个元素,每个 ...

  4. nyoj28-大数阶乘

    大数阶乘 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它? 输入 输入一个整数m(0& ...

  5. DNS解析流程原理(图例)

    13台根服务器的dns: 1.root-servers.net198.41.0.4美国2.root-servers.net192.228.79.201美国(另支持IPv6)3.root-servers ...

  6. Python - Datacamp - Introduction to Matplotlib

    Python - Datacamp - Introduction to Matplotlib Datacamp: https://www.datacamp.com/ # 1.py 基本matplotl ...

  7. 【hiho一下 第九周】 状态压缩·二

    [题目链接]:http://hihocoder.com/problemset/problem/1048 [题意] [题解] 按从左到右然后从上到下的顺序; 依次枚举每个格子是竖条还是横条; 然后在搜索 ...

  8. 如何打开WCF测试客户端

  9. Codeforces Round #464 (Div. 2)

    A. Love Triangle time limit per test: 1 second memory limit per test: 256 megabytes input: standard ...

  10. mysql开发之---每日一得01

    2015年7月7日------------------------- 1.truncate表会清空建表语句auto_increment的值:某个表的id即是主键也是自增,你能够选择插入随意id值,假设 ...