为什么Output Limit Exceeded

寻星已逝 2014-11-26 04:17:25
括号匹配
Description
假设表达式中允许包含4种括号:圆括号、方括号、花括号、尖括号,其嵌套的顺序随意,即([]())<>或[([{}]{[]})]等为正确的格式,但{[(])>或([(<))或(()])或([)]均为不正确的格式。

给出括号的序列,请判断该序列的括号是否匹配为正确的格式。

Input
有多个测试用例。

每个测试用例占一行。每一行仅包含上述的括号,不含其它字符,一行的括号总数不超过10000个。

Output
对每个括号序列输出一行结果,如果该括号序列是匹配正确的,则输出"YES",否则输出"NO"。

Sample Input
([]())
[([][])]
[>(])
([(<))
{(()])

Sample Output
YES
YES
NO
NO
NO

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define MAXN 10001
int push(char *stack,int maxn,int *toppt,char x)
{
if(*toppt>=maxn)
return 1;
stack[*toppt]=x;
++(*toppt);
return 0;
}
int pop(char *stack,int *toppt,char *cp)
{
if(*toppt==0)
return 1;
--(*toppt);
*cp=stack[*toppt];
return 0;
}
void OutputStack(int *stack,int toppt)
{
int i;
for(i=toppt-1;i>=0;i--)
printf("%d",stack[i]);
printf("\n");
}
int correct(char *exp,int max)
{
int flag=0;
char s[MAXN];
int top=0;
char c;
int i;
for(i=0;i<max&&flag==0;i++)
{
if(exp[i]=='('||exp[i]=='['||exp[i]=='{'||exp[i]=='<')
push(s,MAXN,&top,exp[i]);
if(exp[i]==')'||exp[i]==']'||exp[i]=='}'||exp[i]=='>'){
flag=pop(s,&top,&c);
if((exp[i]==')'&&c!='(')||(exp[i]==']'&&c!='[')||(exp[i]=='}'&&c!='{')||(exp[i]=='>'&&c!='<'))
flag=1;
}
}
if(top!=0)
flag=1;
return flag;
}
int main()
{
char s[MAXN],c;
int i,l;
char exp[10001];
int top=0;
while(1)
{
gets(exp);
exp[MAXN]='\0';
if(strcmp(exp,"0")==0)
return 0;
if(correct(exp,strlen(exp))!=0)
printf("NO\n");
else
printf("YES\n");

}
}
...全文
289 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧