ZJANS

d478. 共同的數 - 簡易版

Medium Last Update: 2026/02/01
Hash Table

給兩組數字,判斷共同的數有幾個


解法一

✅ 完整代碼

評分結果(參考) : AC (0.1s, 700KB)

#include<bits/stdc++.h>
#define int long long
using namespace std;

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n, m, num;
    cin >> n >> m;
    for(int i=0; i<n; i++){
        int cnt = 0;
        unordered_set<int> st;
        
        for(int j=0; j<m; j++){
            cin >> num;
            st.insert(num);
        }
        for(int j=0; j<m; j++){
            cin >> num;
            if(st.count(num)) cnt++;
        }
        
        cout << cnt << "\n";
    }
    
    return 0;
}