💡資訊
趙長鵬警示 #加密行業 安全風險,強調社交帳號為薄弱環節。駭客透過偽造 Ledger Discord 管理員帳號發布虛假漏洞資訊,誘 ব্যবহারকারী জমা দিন:
```cpp
#include
#include
#include
using namespace std;
int main() {
int n;
cin >> n;
vector
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int m;
cin >> m;
vector
for (int i = 0; i < m; ++i) {
cin >> b[i];
}
// Sort both arrays to make the search more efficient
sort(a.begin(), a.end());
sort(b.begin(), b.end());
vector
int i = 0, j = 0;
while (i < n && j < m) {
if (a[i] < b[j]) {
++i;
} else if (b[j] < a[i]) {
++j;
} else {
// Found a common element
intersection.push_back(a[i]);
++i;
++j;
}
}
// Print the intersection
for (int k = 0; k < intersection.size(); ++k) {
cout << intersection[k] << (k == intersection.size() - 1 ? "" : " ");
}
cout << endl;
return 0;
}
```
**Explanation:**
1. **Include Headers:**
- `iostream`: For input and output operations (like `cin` and `cout`).
- `vector`: To use dynamic arrays (vectors).
- `algorithm`: To use the `sort` function.
2. **Input:**
- Read the size of the first array (`n`) and the elements of the first array (`a`).
- Read the size of the second array (`m`) and the elements of the second array (`b`).
3. **Sorting:**
- `sort(a.begin(), a.end());` and `sort(b.begin(), b.end());` sort both arrays in ascending order. Sorting is crucial for the efficiency of the intersection finding algorithm.
4. **Finding the Intersection:**
- `vector
- `int i = 0, j = 0;`: Initializes two index variables, `i` for array `a` and `j` for array `b`.
- `while (i < n && j < m)`: The loop continues as long as both indices are within the bounds of their respective arrays.
- `if (a[i] < b[j])`: If the current element in `a` is smaller than the current element in `b`, increment `i` to move to the next element in `a`.
- `else if (b[j] < a[i])`: If the current element in `b` is smaller than the current element in `a`, increment `j` to move to the next element in `b`.
- `else`: If `a[i]` and `b[j]` are equal, it means we've found a common element.
- `intersection.push_back(a[i]);`: Add the common element to the `intersection` vector.
- `++i; ++j;`: Increment both `i` and `j` to move to the next elements in both arrays.
5. **Output:**
- The code iterates through the `intersection` vector and prints each element, separated by a space. The last element is printed without a trailing space.
**How it Works (Intersection Algorithm):**
The algorithm efficiently finds the intersection of two sorted arrays. It uses two pointers, one for each array. It compares the elements pointed to by the pointers:
- If the element in the first array is smaller, the pointer for the first array is incremented.
- If the element in the second array is smaller, the pointer for the second array is incremented.
- If the elements are equal, it means we've found a common element, so it's added to the intersection, and both pointers are incremented.
This approach avoids unnecessary comparisons and has a time complexity of O(n log n + m log m) due to the sorting step, and O(n + m) for the intersection finding, where n and m are the sizes of the input arrays. If the arrays are already sorted, the time complexity is O(n + m).
**Example Usage:**
```
Input:
5
1 2 3 4 5
3
3 5 6
Output:
3 5
```
```
Input:
4
2 4 6 8
5
1 3 5 7 9
Output:
```
(Empty output because there are no common elements)