永发信息网

程序设计 括号匹配问题

答案:3  悬赏:70  手机版
解决时间 2021-04-21 02:23
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 sequenceFor 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

样例输出
66406要代码
最佳答案

#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;
}

全部回答

用栈实现,不是很复杂的!有时间了再给你传吧!

编译原理的作业吧,代码很复杂,就不写了

我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
华南家居大世界南1门在什么地方啊,我要过去
威斯布鲁克是什么位置,海贼王中布鲁克厉害还
天津和平区有沃尔玛商场吗
为什么我的开心农场进不去了,进到96%就卡住
在rt三角形abc中,∠abc=90°,∠a=30°,bd平分
宝宝乐园宝宝锁定忘了密码如何解锁?
龙顺宾馆怎么去啊,有知道地址的么
请问大家…我最近肚子老是有点痛…还腰酸背痛
孕妇吃什么抑制胃酸,孕妇胃酸过多吃什么能缓
为什么现在空间上传不了多张照片啊?
刚开通黄钻,QQ空间信纸怎么会用完?!
抢车位现在怎末开不了?
写关于动物的歇后语
the park is a great place ( ) 括号中填to g
睡觉在桌子上睡有哪些害处
推荐资讯
为什么爱了还会有那么多的人会不甘心?会去寻
刚下载的QQ为什么不能安装?
怀孕六个多月的孕妇晚上身上老冒汗是怎么回事
用魔兽电脑智能补丁弄的图怎么不能建立游戏
一套的情侣QQ
晒黑勒怎么办?
我想查询服装进货的一些信息!有哪些地方呢?
广州市五号地铁线几时开?
铜与铁哪个导热性好(快)一些?
长沙哪里有沙爹鱼串买
想快速减肥不吃减肥药
东月村我想知道这个在什么地方
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?