1. 程式人生 > >NYOJ 2 括號配對問題

NYOJ 2 括號配對問題

第一次用棧來寫的程式碼,早上隊長講了一下,瞭解了下基礎,覺得C++的棧可以直接呼叫,C的棧卻要自己定義,還是該花時間好好看C++.這是用C寫的:

程式碼:

#include<stdio.h>
struct stack 
{
	char str[10005];
    int top;
};
void Initstack(stack& a)//**構造一個空棧**//       
{
	a.top=-1; 
}
void push(stack& a,char item)//**往棧裡面插入一個新元素**//              
{
	a.top++;         
    a.str[a.top]=item;
}
void pop(stack& a)//**刪除棧頂元素並返回其值**//                
{
	a.top--;         
}
int main()
{
	int s;
	char ch;
	scanf("%d",&s);
	getchar();
	while(s--)
	{
		stack a;
		Initstack(a);
		while(scanf("%c",&ch)&&ch!='\n')
		{
			if(ch=='['||ch=='(')
				push(a,ch);
			else
			{
				if(a.str[a.top]=='['&&ch==']')
				{
					pop(a);
				}
				else if(a.str[a.top]=='('&&ch==')')
				{
					pop(a);
				}
				else
				{
					push(a,ch);
				}
			}
		}
		if(a.top==-1)//**如果棧為空說明括號配對完成**//
		{
			printf("Yes\n");
		}
		else
		{
			printf("No\n");
		}
	}
	return 0;
}
        

C++的STL:

#include<cstdio>
#include<stdio.h>
#include<stack>
#include<algorithm>
using namespace std;
int main()
{
	int n;
	char ch;
	scanf("%d",&n);
	getchar();
	while(n--)
	{
		stack<char> s;
		while(scanf("%c",&ch)&&ch!='\n')
		{
			if(s.empty())
			{
				s.push(ch);
			}
		    else
			{
				if(ch=='('||ch=='[')
				{
					s.push(ch);
				}
			    else
				{
					if(ch==']'&&s.top()=='[')
					{
						s.pop();
					}
				    else if(ch==')'&&s.top()=='(')
					{
						s.pop();
					}
				    else
					{
						s.push(ch);
					}
				}
			}
		}
		if(s.empty())//**判斷棧是否為空**//
		{
			printf("Yes\n");
		}
		else
		{
			printf("No\n");
		}
	}
	return 0;
}

再貼一段:

#include<stdio.h>
struct stack
{
	char str[10002];
	int pos;
}s;
void push(char elem)//**往棧裡面插入一個新元素**// 
{
	s.pos++;
	s.str[s.pos]=elem;
}
void pop()//**刪除棧頂元素並返回其值**// 
{
	s.pos--;
}
int main()
{	
	int n;
	char ch;
	scanf("%d",&n);
	getchar();
	while(n--)
	{
		s.pos=-1;//**空棧**//
		while(scanf("%c",&ch)&&ch!='\n')
		{
			if(ch=='['||ch=='(')
				push(ch);
			else
			{
				if(ch==']'&&s.str[s.pos]=='['||s.str[s.pos]=='('&&ch==')')
				{
					pop();
				}
			    else
				{
					push(ch);
				}
			}
		}
		if(s.pos==-1)
		{
			printf("Yes\n");
		}
		else
		{
			printf("No\n");
		}
	}
	return 0;
}        

還有一段用陣列做的,其實思想都一樣。

#include <stdio.h>
int main()
{
	int s,top;
	scanf("%d",&s);
	getchar();
	while(s--)
	{
		top=0;
		char a,str[20000];
	    while((a=getchar())!='\n')
	    {
				if(a==')'&&top>0&&str[top-1]=='(')
				{
					top--;
				}
		        else if(a==']'&&top>0&&str[top-1]=='[')
				{
					top--;
				}
				else
				{
					str[top++]=a;
				}
	    }
		if(top==0)
		{
			printf("Yes\n");
		}
	    else
		{
			printf("No\n");
		}
	}
	return 0;
}        

最後一段是比較完美的了:

#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
int main()
{
	int n,len,i;
	char str[10001];
	scanf("%d",&n);
	getchar();
	while(n--)
	{    stack<char> s;
	     scanf("%s",str);
	     len=strlen(str);
		 for(i=0;i<len;i++)
		 {
			 if(s.empty())s.push(str[i]);
			 else
			 {
				 if(s.top()+1==str[i]||s.top()+2==str[i])//**'('跟')'ASCII碼差一,'['跟']'ASCII差二。**//
				 {
					 s.pop();
				 }
				 else
				 {
					s.push(str[i]);
				 }
			}
		}
		if(s.empty())
		{
			printf("Yes\n");
		}
		else
		{
			printf("No\n");
		}
	}
	return 0;
}