永发信息网

程序设计 括号匹配

答案:2  悬赏:60  手机版
解决时间 2021-05-17 03:16

括号匹配


描述


We give the following inductive definition of a “regular brackets” sequence:

the empty sequence is a regular brackets sequence,
if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and
if a and b are regular brackets sequences, then ab is a regular brackets sequence.
no other sequence is a regular brackets sequence
For instance, all of the following character sequences are regular brackets sequences:

(), [], (()), ()[], ()[()]

while the following character sequences are not:

(, ], )(, ([)], ([(]

Given a brackets sequence of characters a1a2 … an, your goal is to find the length of the longest regular brackets sequence that is a subsequence of s. That is, you wish to find the largest m such that for indices i1, i2, …, im where 1 ≤ i1 < i2 < … < im ≤ n, ai1ai2 … aim is a regular brackets sequence.

Given the initial sequence ([([]])], the longest regular brackets subsequence is [([])].


输入


The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters (, ), [, and ]; each input test will have length between 1 and 100, inclusive. The end-of-file is marked by a line containing the word “end” and should not be processed.


输出


For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.


样例输入

((()))
()()()
([]])
)[)(
([][][)
end


样例输出

6
6
4
0
6

最佳答案
这个我做过,发你邮箱了
全部回答

#include <stdio.h> #include<stdlib.h> #include<string.h> typedef char StackItem; typedef struct astack *Stack; typedef struct astack { int top,maxtop; StackItem *data; }Astack; void Error(char *s) { printf("%s",s); // exit(1); } Stack StackInit(int size) { Stack S=(Stack)malloc(sizeof astack); S->data=(StackItem*)malloc(size*sizeof(StackItem)); S->maxtop=size; S->top=-1; return S; } int StackEmpty(Stack S) { return S->top<0; } int StackFull(Stack S) { return S->top==S->maxtop; } StackItem StackTop(Stack S) { if(StackEmpty(S))Error("Stack is empty"); else return S->data[S->top]; } void Push(StackItem x,Stack S) { if(StackFull(S))Error("Stack is full"); else S->data[++S->top]=x; } StackItem Pop(Stack S) { if(StackEmpty(S)) Error("Stack is empty"); else return S->data[S->top--]; } void Parenthsis(char *expr) { int i,n; Stack ss=StackInit(100); n=strlen(expr); for(i=1;i<=n;i++) { if(expr[i-1]=='(')Push(i,ss); else if(expr[i-1]==')') { if(StackEmpty(ss)) { printf("the right square at position %d is not matched \n",i); } else printf("%d %d \n",Pop(ss),i); } } while(!StackEmpty(ss)) printf("the left square at position %d is not matched \n",Pop(ss));

} int main(int argc, char* argv[]) { char s[40]; printf("Please input a formula including parenthsis:\n"); scanf("%s",s); Parenthsis(s); scanf("%s",s); return 0; } ///用栈解决

我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
红色的靴子该怎么搭配
在网上买东西如果没蓄存卡支付、还有什么方法
梦之队的押韵口号,关于‘梦之队’的口号
什么是分组数据连接??
黄石港区黄石长江网吧这个地址怎么能查询到,
CF一个月的AC跟一个月的大炮多少钱?
除了原味之外,GF还有哪些著名的瞎子啊?要红
彩虹岛5区虎头鲍谁能给我一点钱啊
麻城市黄冈联塑管道地址在哪,我要去那里
打疫苗对狗狗有什么害处
我想加问问团队、手机有什么办法可以直接加入
长阳土家族自治县宜昌长阳乐园移动营业厅在什
祝贺国庆节语句,描写国庆节的句子表达快乐的
松下KX- FL318CN不能收发传真怎么回事?
地下城与勇士怎么点图标
推荐资讯
夜愿的几首纯音乐
现在学什么是有前途的
我的电脑玩游戏卡住,然后蓝屏,电脑高手进来
去日本劳务日本人过来面试后要等多久才知道是
慕容云海当服务员是哪一集
石门县常德欧神诺陶瓷怎么去啊,谁知道地址啊
魔兽疾风忍法帖 风之逆 1.1 会玩的高手进来
好鸽子一般要几个月才下蛋,下蛋时有怎样的征
上海、杭州哪个抵达时间段
老河口市襄樊小肥羊常来烧烤地址有谁知道?有
如何打印微信的文件,怎样把微信收藏文件怎么
求高手来给写篇英文自我介绍
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?