1 条题解

  • 0
    @ 2024-7-25 13:31:09

    参考答案:

    #include<iostream>
    
    using namespace std;
    
    const int N = 100010, M = 31 * N;
    int son[M][2], a[N], idx;
    
    void insert(int x)
    {
        int p = 0;
        for (int i = 30; i >= 0; --i)
        {
            int u = x >> i & 1;
            if (!son[p][u]) son[p][u] = ++idx;
            p = son[p][u];
        }
    }
    int query(int x)
    {
        int p = 0;
        int res = 0;
    
        for (int i = 30; i >= 0; --i)
        {
            int u = x >> i & 1;
            if (son[p][!u])
            {
                p = son[p][!u];
                res = res * 2 + 1;
            }
            else
            {
                p = son[p][u];
                res = res * 2 + 0;
            }
        }
    
        return res;
    }
    int main()
    {
        int n, res = 0;
    
        cin >> n;
        for (int i = 0; i < n; ++i)
        {
            cin >> a[i];
            insert(a[i]);
        }
    
        for (int i = 0; i < n; ++i) res = max(res, query(a[i]));
        cout << res << endl;
    
        return 0;
    }
    
    • 1

    信息

    ID
    82
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    1
    已通过
    1
    上传者