UVA514——栈的经典应用

在vjduge上发现这道题RE了一次后没再做,想起来应该是之前上C++课上到模版类那一章时候做的,因为数组开小了(只开了10...)结果过不去,这次顺手ac了一下。

题目:

There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish only a surface track. Moreover, it turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track.

The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has N ≤ 1000 coaches numbered in increasing order 1, 2, . . . , N . The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be a1.a2, . . . , aN . Help him and write a program that decides whether it is possible to get the required order of coaches. You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return back to the station.

Input

The input le consists of blocks of lines. Each block except the last describes one train and possibly more requirements for its reorganization. In the rst line of the block there is the integer N described above. In each of the next lines of the block there is a permutation of 1, 2, . . . , N. The last line of the block contains just ‘0’.

The last block consists of just one line containing ‘0’. Output

The output le contains the lines corresponding to the lines with permutations in the input le. A line of the output le contains ‘Yes’ if it is possible to marshal the coaches in the order required on the corresponding line of the input le. Otherwise it contains ‘No’. In addition, there is one empty line after the lines corresponding to one block of the input le. There is no line in the output le corresponding to the last “null” block of the input le.

Sample Input

5 12345 54123 0
6 654321 0
0

Sample Output

Yes No

Yes 

(粘贴过来的输入输出格式youdianbudui,具体还是上oj看吧)

主要就是用栈模拟整个过程,注意条件判断就好。

先贴上 算法入门经典 书上的解法以及另一种解法:

#include <iostream>
#include <stack>
using namespace std;


int main(){
    int N;
    while(cin>>N && N!=0){
        stack<int> s;
        int target[N+7];
        while(cin>>target[1] && target[1]!=0){
        
            //这种解法比较简便
            while(!s.empty())   s.pop();
            for(int i = 2;i<=N;i++){
                cin>>target[i];
            }
            
            int index = 1;
            int indexT = 1;
            while(index<=N){
                s.push(index++);
                while(!s.empty() && s.top()==target[indexT]){
                    indexT++;
                    s.pop();
                }
            }
            if(s.empty())
                cout<<"Yes"<<endl;
            else
                cout<<"No"<<endl;
//           
//            算法入门经典书上的解法: 
//            int indexB = 1;
//            int indexA = 1;
//            int ok = 1;
//            while(indexB<=N){
//                if(target[indexB] == indexA)
//                {
//                    indexB++;
//                    indexA++;
//                }
//                else if(!s.empty() && s.top()==target[indexB]){
//                    s.pop();
//                    indexB++;
//                }
//                else if(indexA <= N){
//                    s.push(indexA++);
//                }
//                else{
//                    ok = 0;
//                    break;
//                }
//            }
//            if(ok)
//                cout<<"Yes"<<endl;
//            else
//                cout<<"No"<<endl;
        }
        cout<<endl;
    }
    return 0;
}

再来是模版类(也就是自己写一个栈类)

#include<iostream>
using namespace std;
const int StackSize=1000+7;  //10只是示例性的数据,可以根据实际问题具体定义
template <class DataType>       //定义模板类SeqStack
class SeqStack
{
public:
    SeqStack( ) ;            //构造函数,栈的初始化
	~SeqStack( );            //析构函数
    void Push(DataType x);          //将元素x入栈
    DataType Pop( );                //将栈顶元素弹出
    DataType GetTop( );	         //取栈顶元素(并不删除)
	int Empty( );           //判断栈是否为空
private:
    DataType data[StackSize];      //存放栈元素的数组
    int top;                //栈顶指针,指示栈顶元素在数组中的下标
};

template <class DataType>
SeqStack<DataType>::SeqStack( )
{
	top=-1;
}

template <class DataType>
SeqStack<DataType>::~SeqStack( )
{
	
} 

template <class DataType> 
void SeqStack<DataType>::Push(DataType x)
{
	if(top==StackSize-1)	throw"上溢";
	data[++top] = x;
}

template <class DataType>
DataType SeqStack<DataType>::Pop( )
{ 
	if(top==-1)	throw"下溢";
	DataType x = data[top--];
	return x;
}

template <class DataType> 
DataType SeqStack<DataType>::GetTop( )
{
	if(top==-1)	throw"下溢";
	DataType x = data[top];
	return x;
}

template <class DataType> 
int SeqStack<DataType>::Empty( )
{
	if(top==-1)	return 1;
	return 0;
} 

int judge(int b[],int n)
{
	//b[]为出栈序列,n为个数,i代表进栈车厢,j为下标.
	int i = 1,j = 0;
	SeqStack<int> A;
	int flag = 1;
	A.Push(i);	//先将1号车入栈。

	while(j<n)
	{
		if(A.Empty()||(!A.Empty()&&A.GetTop()<b[j]))	//栈空 或者 栈顶元素小于此时出栈序列的元素.
			A.Push(++i);	//继续往A压栈.

		else if(!A.Empty()&&A.GetTop()==b[j])	//栈顶元素等于此时出栈序列的元素.
		{
			A.Pop();	//出栈.
			j++;	//序列后移.
		}

		else	//栈顶元素大于此时出栈序列元素,则不可能.
		{
			flag = 0;
			break;
		}
	}

	if(A.Empty())	flag = 1;	//出栈成功.
	else flag = 0;
	return flag;
}

int main()
{
	int N;
	int b[1007];
	//输入
	while(1)
	{
		cin>>N;
		if(N==0)
			break;
		while(N!=0)
		{
			cin>>b[0];
			if(b[0]==0)	break;
			for(int i = 1;i<N;i++)	cin>>b[i];
			if(judge(b,N))
				cout<<"Yes"<<endl;
			else
				cout<<"No"<<endl;
		}
               cout<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41508508/article/details/81086094