HDU - 5479 Scaena Felix
题目链接:HDU - 5479
题目大意:寻找匹配括号
eg. ()() 2组 )(() 1组 )))((( 0组
思路:不能单纯计数否则 )( 时出错,逐个配对考虑栈应用,( 压入栈,) 时弹出栈中 ( 配对并计数
数组简单模拟栈如下👇
#include<stdio.h>
#include<string.h>
main()
{
int n;
scanf("%d",&n);
getchar();
while(n--)
{
char a[1001],b[1001];
memset(b,'\0',sizeof(b));
int top=0,sum=0;
gets(a);
int len=strlen(a);
for(int i=0;i<len;i++)
{
if(a[i]=='(') b[top++]=a[i];
else
{
if(top==0) continue;
if(b[top-1]=='(')
{
sum++;
b[--top]='\0';
}
}
}
printf("%d\n",sum);
}
}
本文解析了HDU-5479题目的括号匹配算法,通过使用栈的数据结构来实现括号的正确配对,解决括号组合的问题,并提供了完整的C语言代码实现。
614

被折叠的 条评论
为什么被折叠?



