#P4387. Valid Stack Sequences
Valid Stack Sequences
Valid Stack Sequences
Given two sequences, pushed
and poped
, each containing integers from 1 to \( n \) (where \( n\leq100000 \)). The sequence pushed
represents the order in which integers are pushed onto a stack. You are to determine whether the sequence poped
could be the result of pop operations on that stack.
Each test input contains multiple test cases for preventing hardcoding solutions (each input contains no more than 5 test cases).
Example:
Input: 3 5 1 2 3 4 5 4 5 3 2 1 5 1 2 3 4 5 4 3 5 1 2 3 1 2 3 2 1 3</p>Output: Yes No Yes
In the first and third cases, it is possible to achieve the given output sequence. In the second case, it is not possible.
inputFormat
The first line contains an integer T
(\(1\le T\le5\)), representing the number of test cases. For each test case, the input is as follows:
- An integer
n
(\(1\le n\le 100000\)) indicating the number of elements. - A line containing
n
integers representing thepushed
sequence. - A line containing
n
integers representing thepoped
sequence.
The integers in the sequences are separated by spaces.
outputFormat
For each test case, output a single line with either Yes
or No
indicating whether the poped
sequence is a valid pop order for the given pushed
sequence.
sample
3
5
1 2 3 4 5
4 5 3 2 1
5
1 2 3 4 5
4 3 5 1 2
3
1 2 3
2 1 3
Yes
No
Yes
</p>