1. 程式人生 > >CIDR去重-滴滴筆試題

CIDR去重-滴滴筆試題

題目連結

思路和考點

  • 這道題主要是對字串進行分割,解析,然後將ip逆序儲存,判斷位數較少的是否被位數較多的數包圍。思路比較簡單,就是實現比較麻煩。

程式碼

#include <cstdlib>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <stdio.h>
#include <numeric>
#include <algorithm>
#include <functional>
#include <stack>
#include <queue>
#include <cmath>
#include <assert.h>
#include <vector>
#include <memory>
#include <memory.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
typedef unsigned char uchar;

//#define G_DEBUG

uchar reverseBits(uchar n) {
    n = ((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1);
    n = ((n & 0xCCCCCCCC) >> 2) | ((n & 0x33333333) << 2);
    n = ((n & 0xF0F0F0F0) >> 4) | ((n & 0x0F0F0F0F) << 4);
    return n;
}

int main()
{

#ifdef G_DEBUG
    // 除錯使用
    int N = 0;
    ifstream file("data.txt");

    string s;
    getline( file, s );
    N = atoi( s.c_str() );
    vector<string> strs(N);
    int i = 0;
    while( getline( file, s ))
        strs[i++] = s;
#else
    int N = 0;
    cin >> N;
    vector<string> strs(N);

    for (int i = 0; i < N; i++)
        cin >> strs[i];

#endif

    vector<int> bit_nums(N, 0);
    vector<ll> val_nums(N, 0);
    int idx = 0;
    int curr_bit = 0;
    ll curr_val = 0;
    ll tmp = 0;
    ll tmp_val = 0;
    uchar c;
    for (int i = 0; i < N; i++)
    {
        idx = strs[i].find_first_of('/');
        bit_nums[i] = atoll(strs[i].substr(idx + 1).c_str());

        idx = strs[i].find_first_of('.');
        c = atoi(strs[i].substr(0, idx).c_str());
        curr_val = (ll)reverseBits(c);
        tmp = idx;

        idx = strs[i].find_first_of('.', idx + 1);
        c = atoi(strs[i].substr(tmp + 1, idx - tmp).c_str());
        curr_val = ((ll)reverseBits(c) << 8) | curr_val;
        tmp = idx;

        idx = strs[i].find_first_of('.', idx + 1);
        c = atoi(strs[i].substr(tmp + 1, idx - tmp).c_str());
        curr_val = ((ll)reverseBits(c) << 16) | curr_val;
        tmp = idx;

        idx = strs[i].find_first_of('.', idx + 1);
        c = atoi(strs[i].substr(tmp + 1, idx - tmp).c_str());
        curr_val = ((ll)reverseBits(c) << 24) | curr_val;

        curr_val = (((ll)1 << bit_nums[i]) - 1) & curr_val;

        val_nums[i] = curr_val;
    }

    vector<int> removed_idxs;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (i == j)
                continue;
            if ((bit_nums[i] >= bit_nums[j] )
                && (val_nums[j] == (val_nums[i] & (((ll)1 << bit_nums[j]) - 1)))
                )
            {
                removed_idxs.push_back( i );
                break;
            }
        }
    }

    int re_idx = 0;
    cout << (N - removed_idxs.size()) << endl;
    for (int i = 0; i < N; i++)
    {
        if (removed_idxs.size() > re_idx && i == removed_idxs[re_idx])
        {
            re_idx++;
            continue;
        }
        cout << strs[i] << endl;
    }


    system("pause");
    return 0;
}