2015 湘潭大学程序设计比赛(Internet)H题-括号匹配

括号匹配

Accepted : 30   Submit : 234
Time 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,用空格隔开。每个样例之后输出一个空行

样例输入

(){}[]
3
1
3
5
[([{()[]}])][()]
5
1
2
5
13
14

样例输出

2 0 0 0
4 0 0 0
6 0 0 0

12 2 2 1
11 1 2 1
6 0 0 0
16 1 0 0
15 0 0 0

Source

CKBoss
代码如下:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stack>
#define N 100100
using namespace std;

struct node
{
    int index,k,pre,r[4];//k是看它是哪种括号,pre是对应的另一半下标,r[1]是小括号,其次是中括号,大括号;
};

char str[N];
node a[N];

int main()
{
    node q,t;
    int i,m,x,j;
    int b[200]={0};

    b['(']=1;b['[']=2;b['{']=3;
    b[')']=4;b[']']=5;b['}']=6;

    while(scanf("%s",str+1)!=EOF)
    {
       stack<node>sta;
     memset(a,0,sizeof(a));
for(i=1;str[i];i++) { a[i].index=i; a[i].k=b[str[i]]; if(a[i].k<=3) { sta.push(a[i]); } else { q=sta.top(); sta.pop(); a[i].pre=q.index; q.pre=i; if(sta.size()) { t=sta.top(); sta.pop(); t.r[1]+=q.r[1];t.r[2]+=q.r[2];t.r[3]+=q.r[3]; t.r[q.k]+=1; sta.push(t); } a[q.index]=q; } } scanf("%d",&m); for(i=0;i<m;i++) { scanf("%d",&x); j=min(x,a[x].pre); printf("%d %d %d %d\n",a[x].pre,a[j].r[1],a[j].r[2],a[j].r[3]); } } return 0; }

 http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1232

 
posted @ 2015-04-27 15:43  西瓜不懂柠檬的酸  Views(187)  Comments(0)    收藏  举报
levels of contents