[PTA Ladder Tournament Stack Order

The stacking order of PTA Ladder Tournament
Multiple writing methods, containers, arrays

Given a stack with a maximum capacity of M, put N numbers on the stack in the order of 1, 2, 3, …, N, and allow them to be popped in any order. What sequence of numbers is impossible to get? For example, given M=5 and N=7, we may get {1, 2, 3, 4, 5, 6, 7 }, but it is impossible to get {3, 2, 1, 7, 5, 6, 4 }.

Input format:

Enter the first line to give 3 positive integers not exceeding 1000: M (maximum stack capacity), N (number of elements pushed onto the stack), and K (number of unstacked sequences to be checked). In the last K lines, each line gives a pop sequence of N numbers. All numbers in the same line are separated by spaces.

Output format:

For each row of the pop-up sequence, if it is indeed a possible legal sequence, output YES in one row, otherwise output NO.

Input sample:

5 7 5
1 2 3 4 5 6 7
3 2 1 7 5 6 4
7 6 5 4 3 2 1
5 6 4 3 7 2 1
1 7 6 5 4 3 2

Sample output:

YES
NO
NO
YES
NO

The ladder game simulation game, alas, the writing was very complicated at the time. After changing it many times, there were still two or three examples. Looking at the number of people who wrote it, it was terrible.

The next paragraph is nonsense.

My initial idea was very general and it was very troublesome to implement. There are many things to judge, that is, when you count out of the stack, first you need to judge whether the stack is full at the moment , and also judge which elements are already out of the stack . If you are not in the stack, you must also determine that the elements in the stack cannot be larger than the elements popped out of the stack . That is, the loop sets the loop and also adds the mark, how troublesome it is, how troublesome it is.
—————————————————————————

****** In fact, when thinking in general terms, it is often very troublesome and requires a new and new way of thinking.

****** In the question, we need to judge whether the stacking order is reasonable,
we follow the meaning of the question, put one element on the stack from small to large, when the element on the stack is equal to the element on the stack, our family removes the element Stack, top–; and then continue to put elements. If we can put all elements in, it means that the stack order is reasonable.
Problems encountered in the process:

1. How to judge the stack is full.
-------According to top, top not only represents the top of the stack, but also represents the elements in the stack. Although we push the element once into the stack and execute ++top, at the same time, when the element that needs to be popped is consistent with the element in the stack, we will also execute top– to pop the stack. Therefore, when top>m, it can indicate that the stack has overflowed.

2. How to judge that the element in the stack is larger than the popped element
This problem does not need to be considered, because the stack is selected from small to large.

3. How to know which elements have been popped out of the stack.
Actually, we don’t need to care. Top already indicates the number of elements in the stack, so we don’t need to pay attention to which elements have been popped out.

So
using this idea, it is much easier to operate

———————-----------——————————————–

Method 1: Use an array to write

#include<stdio.h>
#include<string.h>
int a[1100],sta[1100];
int main()
{
	int m,n,k;
	scanf("%d %d %d",&m,&n,&k);
	while(k--)
	{
		int i,j;
		sta[0]=-1;//给sta[0]赋一个永远不可能与出栈元素相等的值 
		for(i=1;i<=n;i++)
			scanf("%d",&a[i]);
		int w=1,top=0;
		sta[++top]=w;//首先让1入栈
		i=1;
		while(i<=n)//按照出栈顺序让栈内元素出栈
		{
			while(a[i]==sta[top])//当前栈顶满足出栈要求,降低top
			{
				top--;//栈顶出栈
				i++;//新的栈顶与下一个将要出栈的元素进行对比
	          //下面的可以不需要,因为sta[0]==-1
        	  //if(top==0)//栈内无元素了,不可能再出栈了
	          //	break; 
			}
            //栈顶元素小于要出栈的元素,说明此刻要出栈的元素还没有进栈呢
            //出栈元素要进栈,那比它小的肯定得比它先进栈
			if(a[i]!=sta[top])
			{
				sta[++top]=++w;//注意++w,用意在于进过栈的元素不可能二次进栈,无论是未出栈的,还是出过栈的,进栈机会只有一次 
			}
			if(top>m||w>n)//栈溢出,或者所有的元素都进栈了
				break; 
		}
		//出栈顺序合法,栈顶最终指向1
		if(i==n+1)//当然只有当所有出栈顺序和合法的时候才YES,这一条件是必须的
			printf("YES\n");//所有出栈顺序合法,没有问题 
		else
			printf("NO\n"); 
	} 
}

Method 2: Use a container to write

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
using namespace std;
int a[1010];
stack<int>st;//容器栈,先进后出 
int main()
{
	int m,n,k;
	scanf("%d %d %d",&m,&n,&k);
	while(k--)
	{
	
		int i,w=1;
		for(i=1;i<=n;i++)
			scanf("%d",&a[i]);
		
		while(!st.empty())
			st.pop(); 
	
		st.push(w);//从1开始进栈 
	
		i=1;
		while(i<=n)//一个一个判定合法顺序 
		{
			while(a[i]==st.top())
			{
				st.pop();//删除栈顶元素
				i++;
				if(st.empty()!=0)//栈为空了就可以跳出循环了 
					break; 
			}
			if(st.empty()!=0||a[i]!=st.top())
			{
				st.push(++w);
			}
			if(st.size()>m||w>n)
				break; 
		}
		if(i==n+1)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/Helinshan/article/details/110679794