-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckArrayFormation.cpp
More file actions
54 lines (53 loc) · 1.32 KB
/
Copy pathcheckArrayFormation.cpp
File metadata and controls
54 lines (53 loc) · 1.32 KB
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
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;
// Check Array Formation Through Concatenation
class Solution
{
public:
bool canFormArray(vector<int> &arr, vector<vector<int>> &pieces)
{
unordered_map<int, int> mp;
for (int i = 0; i < arr.size(); i++)
mp[arr[i]] = i + 1;
for (int i = 0; i < pieces.size(); ++i)
{
if (pieces[i].size() == 1 && mp[pieces[i][0]])
continue;
if (pieces[i].size() > 1 && mp[pieces[i][0]])
{
int idx = mp[pieces[i][0]];
for (int j = 1; j < pieces[i].size(); j++)
{
if (idx >= arr.size())
return false;
if (arr[idx] == pieces[i][j])
idx++;
else
return false;
}
}
else
return false;
}
return true;
}
};
int main()
{
int n, m;
cin >> n;
vector<int> arr(n);
for (int &i : arr)
cin >> i;
cin >> m;
vector<vector<int>> pieces(m);
for (auto &v : pieces)
{
int size;
cin >> size;
v.resize(size, 0);
for (int &i : v)
cin >> i;
}
cout << Solution().canFormArray(arr, pieces);
}