-
Notifications
You must be signed in to change notification settings - Fork 0
/
cd.cpp
62 lines (53 loc) · 1.56 KB
/
cd.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
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// Fast IO
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// Keep accepting cases
while(true)
{
// Get the number of CDs in each collection
int n, m;
cin >> n >> m;
// Terminate if the input is 0 0
if(n == m && m == 0)
{
break;
}
// Store all the CDs in Jack's collection
vector<int> jack;
for(int i = 0; i < n; i++)
{
int x;
cin >> x;
jack.push_back(x);
}
int both = 0; // Stores the number of CDs in both collections
int i = 0; // Stores the index in Jack's collection
for(int j = 0; j < m; j++) // For each CD in Jill's collection
{
/// Get the next CD in Jill's collection
int x;
cin >> x;
// If the index in Jack's collection points to a CD with a lower
// number, keep advancing in Jack's collection
while(jack[i] < x && i < (n-1))
{
i++;
}
// If Jack's index now points to the same number as Jill's CD
if(jack[i] == x)
{
// Then they both have the CD with this number
both++;
}
// If Jack's index isn't the same, then it means that Jack doesn't
// have that CD, so has skipped to a CD with a higher number
}
cout << both << endl;
}
return 0;
}