时间限制: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. 洛谷P2827 蚯蚓 队列 + 观察

    我们不难发现先被切开的两半一定比后被切开的两半大,这样就天然的生成了队列的单调性,就可以省去一个log.所以,我们开三个队列,分别为origin,big,smallorigin, big, small ...

  2. There are no packages available for installation. Sublime3解决方法

    最近在学习Vue,在配置sublime3的时候,想要高亮vue的语法,下载点插件 Package Control的时候,总报  There are no packages available for ...

  3. [bzoj 2726] 任务安排 (斜率优化 线性dp)

    3月14日第三题!!!(虽然是15号发的qwq) Description 机器上有N个需要处理的任务,它们构成了一个序列.这些任务被标号为1到N,因此序列的排列为1,2,3-N.这N个任务被分成若干批 ...

  4. struct 模块简介

    用处 按照指定格式将Python数据转换为字符串,该字符串为字节流,如网络传输时,不能传输int,此时先将int转化为字节流,然后再发送; 按照指定格式将字节流转换为Python指定的数据类型; 处理 ...

  5. 【codeforces 731E】Funny Game

    [题目链接]:http://codeforces.com/contest/731/problem/E [题意] 两个人轮流玩游戏; 取序列中的前k(k>1)个数字,拿出来; 把这k个数字全部加起 ...

  6. 【codeforces 805B】3-palindrome

    [题目链接]:http://codeforces.com/contest/805/problem/B [题意] 让你生成一个只包含a,b,c的字符串; 要求c出现的次数最少,且任意一个 长度为3的子串 ...

  7. jQuery练习总结(二)

    --------------------------------------- <!DOCTYPE html> <!DOCTYPE HTML PUBLIC "-//W3C/ ...

  8. python 在爬虫中timeout设置超时有什么作用

    是为了防止url不可访问,或者响应速度太慢而造成的时间浪费. 比如,你要爬取1000个网站,如果有100个需要30s才能返回数据,你等待他们返回的话就需要3000s了,如果你设置10s超时,那么就能知 ...

  9. telnet允许root用户登录

    默认情况下,linux不允许root用户以telnet方式登录linux主机,若要允许root用户登录,可采取以下3种方法之一:    1.修改login文件 redhat中对于远程登录的限制体现在/ ...

  10. Scrapy系列教程(6)------怎样避免被禁

    避免被禁止(ban) 有些网站实现了特定的机制,以一定规则来避免被爬虫爬取. 与这些规则打交道并不easy,须要技巧,有时候也须要些特别的基础. 假设有疑问请考虑联系 商业支持 . 以下是些处理这些网 ...