-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_P267.cpp
49 lines (47 loc) · 1.02 KB
/
2_P267.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<bits/stdc++.h>
using namespace std;
int n,m,k;
int a[1005];
int ans[1005];
int stk[1005];
void solve()
{
for(int i = 0; i < n; i++) cin >> ans[i];
int top = 1;
int idx = 0;
stk[top] = a[0];
//intialize the stack(push the first element of array a into stack)
//stack's index starts by 1
//when top equals to 0,the stack is empty
for(int i = 0; i < n; i++)
{
//do iteration until top element is target item
while(stk[top] != ans[i])
{
stk[++top] = a[++idx];
if(top > m || idx > n - 1)
{
cout << "NO" << endl;
return;
}
}
if(top > m || idx > n - 1)
{
cout << "NO" << endl;
return;
}
//now the top of stack is the current target,just pop it
top--;
}
cout << "YES" << endl;
}
int main()
{
cin >> m >> n >> k;
for(int i = 0; i < n; i++) cin >> a[i];
while(k--)
{
solve();
}
return 0;
}