堆栈排列是给定输入队列中对象的排列,这是通过在堆栈和内置推入和弹出功能的帮助下将元素从输入队列传输到输出队列来完成的。
定义明确的规则是:
- 仅从输入队列出队。
- 在单个堆栈中使用内置的推入,弹出功能。
- 堆栈和输入队列的末尾必须为空。
- 仅排队进入输出队列。
对于单个输入队列,使用堆栈可以实现大量的排列。
给定两个数组,这两个元素都是唯一的。一个代表输入队列,另一个代表输出队列。我们的任务是检查是否可以通过堆栈置换来获得给定的输出。
例子:
Input : First array: 1, 2, 3
Second array: 2, 1, 3
Output : Yes
Procedure:
push 1 from input to stack
push 2 from input to stack
pop 2 from stack to output
pop 1 from stack to output
push 3 from input to stack
pop 3 from stack to output
Input : First array: 1, 2, 3
Second array: 3, 1, 2
Output : Not Possible
这样做的想法是,我们将尝试使用堆栈将输入队列转换为输出队列,如果能够的话,则该队列是可置换的,否则就不能置换。
以下是逐步执行此操作的算法:
- 从输入队列中连续弹出元素,并检查它是否等于输出队列的顶部,如果不等于输出队列的顶部,则我们将元素压入堆栈。
- 一旦在输入队列中找到一个元素,例如输入队列的顶部等于输出队列的顶部,我们将从输入和输出队列中弹出一个元素,然后立即比较堆栈的顶部和输出队列的顶部。如果堆栈和输出队列的顶部相等,则从堆栈和输出队列中弹出元素。如果不相等,请转到步骤1。
- 重复上述两个步骤,直到输入队列为空。最后,如果输入队列和堆栈都为空,则输入队列是可置换的,否则不是可置换的。
以下是上述想法的实现:
C++
// Given two arrays, check if one array is
// stack permutation of other.
#include
using namespace std;
// function to check if input queue is
// permutable to output queue
bool checkStackPermutation(int ip[], int op[], int n)
{
// Input queue
queue input;
for (int i=0;i output;
for (int i=0;i tempStack;
while (!input.empty())
{
int ele = input.front();
input.pop();
if (ele == output.front())
{
output.pop();
while (!tempStack.empty())
{
if (tempStack.top() == output.front())
{
tempStack.pop();
output.pop();
}
else
break;
}
}
else
tempStack.push(ele);
}
// If after processing, both input queue and
// stack are empty then the input queue is
// permutable otherwise not.
return (input.empty()&&tempStack.empty());
}
// Driver program to test above function
int main()
{
// Input Queue
int input[] = {1, 2, 3};
// Output Queue
int output[] = {2, 1, 3};
int n = 3;
if (checkStackPermutation(input, output, n))
cout << "Yes";
else
cout << "Not Possible";
return 0;
}
Java
// Given two arrays, check if one array is
// stack permutation of other.
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
class Gfg
{
// function to check if input queue is
// permutable to output queue
static boolean checkStackPermutation(int ip[],
int op[], int n)
{
Queue input = new LinkedList<>();
// Input queue
for (int i = 0; i < n; i++)
{
input.add(ip[i]);
}
// Output queue
Queue output = new LinkedList<>();
for (int i = 0; i < n; i++)
{
output.add(op[i]);
}
// stack to be used for permutation
Stack tempStack = new Stack<>();
while (!input.isEmpty())
{
int ele = input.poll();
if (ele == output.peek())
{
output.poll();
while (!tempStack.isEmpty())
{
if (tempStack.peek() == output.peek())
{
tempStack.pop();
output.poll();
}
else
break;
}
}
else
{
tempStack.push(ele);
}
}
// If after processing, both input queue and
// stack are empty then the input queue is
// permutable otherwise not.
return (input.isEmpty() && tempStack.isEmpty());
}
// Driver code
public static void main(String[] args)
{
// Input Queue
int input[] = { 1, 2, 3 };
// Output Queue
int output[] = { 2, 1, 3 };
int n = 3;
if (checkStackPermutation(input, output, n))
System.out.println("Yes");
else
System.out.println("Not Possible");
}
}
// This code is contributed by Vivekkumar Singh
Python3
# Given two arrays, check if one array is
# stack permutation of other.
from queue import Queue
# function to check if Input queue
# is permutable to output queue
def checkStackPermutation(ip, op, n):
# Input queue
Input = Queue()
for i in range(n):
Input.put(ip[i])
# output queue
output = Queue()
for i in range(n):
output.put(op[i])
# stack to be used for permutation
tempStack = []
while (not Input.empty()):
ele = Input.queue[0]
Input.get()
if (ele == output.queue[0]):
output.get()
while (len(tempStack) != 0):
if (tempStack[-1] == output.queue[0]):
tempStack.pop()
output.get()
else:
break
else:
tempStack.append(ele)
# If after processing, both Input
# queue and stack are empty then
# the Input queue is permutable
# otherwise not.
return (Input.empty() and
len(tempStack) == 0)
# Driver Code
if __name__ == '__main__':
# Input Queue
Input = [1, 2, 3]
# Output Queue
output = [2, 1, 3]
n = 3
if (checkStackPermutation(Input,
output, n)):
print("Yes")
else:
print("Not Possible")
# This code is contributed by PranchalK
C#
// Given two arrays, check if one array is
// stack permutation of other.
using System;
using System.Collections.Generic;
class GFG
{
// function to check if input queue is
// permutable to output queue
static bool checkStackPermutation(int []ip,
int []op, int n)
{
Queue input = new Queue();
// Input queue
for (int i = 0; i < n; i++)
{
input.Enqueue(ip[i]);
}
// Output queue
Queue output = new Queue();
for (int i = 0; i < n; i++)
{
output.Enqueue(op[i]);
}
// stack to be used for permutation
Stack tempStack = new Stack();
while (input.Count != 0)
{
int ele = input.Dequeue();
if (ele == output.Peek())
{
output.Dequeue();
while (tempStack.Count != 0)
{
if (tempStack.Peek() == output.Peek())
{
tempStack.Pop();
output.Dequeue();
}
else
break;
}
}
else
{
tempStack.Push(ele);
}
}
// If after processing, both input queue and
// stack are empty then the input queue is
// permutable otherwise not.
return (input.Count == 0 && tempStack.Count == 0);
}
// Driver code
public static void Main(String[] args)
{
// Input Queue
int []input = { 1, 2, 3 };
// Output Queue
int []output = { 2, 1, 3 };
int n = 3;
if (checkStackPermutation(input, output, n))
Console.WriteLine("Yes");
else
Console.WriteLine("Not Possible");
}
}
// This code is contributed by PrinciRaj1992
输出:
Yes