括号配对问题
时间限制:3000 ms | 内存限制:65535 KB
难度:3
描述
现在,有一行括号序列,请你检查这行括号是否配对。
输入
第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有"[","]","(",")"四种字符
输出
每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No
样例输入
3
[(])
(])
([[]()])
样例输出
No
No
Yes
# include <stdio.h>
# include <stdlib.h>
# define TRUE 1
# define FALSE 0
char str[10000];
typedef char ElementType;
typedef struct node
{
ElementType data;
struct node *next;
}LinkStackNode, *LinkStack;
void InitStack(LinkStack top) //初始化
{
top->next = NULL;
}
int Push(LinkStack top,ElementType e) //进栈
{
LinkStack temp;
temp = (LinkStack)malloc(sizeof(LinkStackNode));
if (temp == NULL)
return FALSE;
temp->data = e;
temp->next = top->next;
top->next = temp;
return TRUE;
}
int IsEmpty(LinkStack top) //判断栈满
{
if (top->next == NULL)
return TRUE;
else
return FALSE;
}
void GetTop(LinkStack top,ElementType *e) //读栈顶
{
*e = top->next->data;
}
int Pop(LinkStack top, ElementType *e) //出栈
{
LinkStack temp;
temp = (LinkStack)malloc(sizeof(LinkStackNode));
temp = top->next;
if (temp == NULL)
return FALSE;
*e = temp->data;
top->next = temp->next;
free(temp);
return (TRUE);
}
int Match(char ch,ElementType e) //判断是否匹配
{
if ((ch == '(' && e == ')') || (ch == '{' && e == '}') || (ch == '[' && e == ']'))
return TRUE;
else
return FALSE;
}
void BracketMatch(LinkStack top, char *str) //括号匹配算法
{
char ch;
int i;
for (i = 0;str[i] != '\0'; i++)
{
switch(str[i])
{
case'(':
case'[':
// case'{':
Push(top,str[i]);
break;
case')':
case']':
// case'}':
if (IsEmpty(top))
{
printf("No\n");
return;
}
else
{
GetTop(top,&ch);
if (Match(ch,str[i]))
Pop(top,&ch);
else
{
printf("No\n");
return ;
}
}//else
} //switch
} //for
if (IsEmpty(top))
printf("Yes\n");
else
printf("No\n");
}
int main()
{
LinkStack top;
top = (LinkStack)malloc(sizeof(LinkStackNode));
InitStack(top);
int n;
scanf("%d", &n);
while (n--)
{
InitStack(top);
scanf("%s",str);
BracketMatch(top,str);
}
return 0;
}
时间限制:3000 ms | 内存限制:65535 KB
难度:3
描述
现在,有一行括号序列,请你检查这行括号是否配对。
输入
第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有"[","]","(",")"四种字符
输出
每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No
样例输入
3
[(])
(])
([[]()])
样例输出
No
No
Yes
# include <stdio.h>
# include <stdlib.h>
# define TRUE 1
# define FALSE 0
char str[10000];
typedef char ElementType;
typedef struct node
{
ElementType data;
struct node *next;
}LinkStackNode, *LinkStack;
void InitStack(LinkStack top) //初始化
{
top->next = NULL;
}
int Push(LinkStack top,ElementType e) //进栈
{
LinkStack temp;
temp = (LinkStack)malloc(sizeof(LinkStackNode));
if (temp == NULL)
return FALSE;
temp->data = e;
temp->next = top->next;
top->next = temp;
return TRUE;
}
int IsEmpty(LinkStack top) //判断栈满
{
if (top->next == NULL)
return TRUE;
else
return FALSE;
}
void GetTop(LinkStack top,ElementType *e) //读栈顶
{
*e = top->next->data;
}
int Pop(LinkStack top, ElementType *e) //出栈
{
LinkStack temp;
temp = (LinkStack)malloc(sizeof(LinkStackNode));
temp = top->next;
if (temp == NULL)
return FALSE;
*e = temp->data;
top->next = temp->next;
free(temp);
return (TRUE);
}
int Match(char ch,ElementType e) //判断是否匹配
{
if ((ch == '(' && e == ')') || (ch == '{' && e == '}') || (ch == '[' && e == ']'))
return TRUE;
else
return FALSE;
}
void BracketMatch(LinkStack top, char *str) //括号匹配算法
{
char ch;
int i;
for (i = 0;str[i] != '\0'; i++)
{
switch(str[i])
{
case'(':
case'[':
// case'{':
Push(top,str[i]);
break;
case')':
case']':
// case'}':
if (IsEmpty(top))
{
printf("No\n");
return;
}
else
{
GetTop(top,&ch);
if (Match(ch,str[i]))
Pop(top,&ch);
else
{
printf("No\n");
return ;
}
}//else
} //switch
} //for
if (IsEmpty(top))
printf("Yes\n");
else
printf("No\n");
}
int main()
{
LinkStack top;
top = (LinkStack)malloc(sizeof(LinkStackNode));
InitStack(top);
int n;
scanf("%d", &n);
while (n--)
{
InitStack(top);
scanf("%s",str);
BracketMatch(top,str);
}
return 0;
}