阿里巴巴2017校招C++岗位在线编程题-RNA嵌套

来源:互联网 发布:网络维护工具app 编辑:程序博客网 时间:2024/06/08 11:28
思路
  对每一个二段式集合挨个遍历,比较嵌套的二段式集合的嵌套深度即可。
#include <iostream>#include <sstream>#include <string>#include <vector>#include <deque>#include <cassert>#include <map>#include <algorithm>using namespace std;class Interval{public:    explicit Interval(size_t left, size_t right)        : mLeft(left),        mRight(right)    {        assert(left <= right);    }    size_t left() const    {        return mLeft;    }    size_t right() const    {        return mRight;    }    /////添加    Interval(){};    Interval(const Interval &a);    Interval &operator = (const Interval &a);    ////private:    size_t mLeft;    size_t mRight;};//////Interval::Interval(const Interval &a){    this->mLeft = a.mLeft;    this->mRight = a.mRight;}Interval &Interval::operator =(const Interval &a){    this->mLeft = a.mLeft;    this->mRight = a.mRight;    return *this;}/////inline bool operator<(const Interval& a, const Interval& b){    return a.right() < b.left();}class TwoInterval{public:    explicit TwoInterval(const Interval& left, const Interval& right)        : mLeft(left),        mRight(right)    {        assert(left < right);    }    const Interval& left() const    {        return mLeft;    }    const Interval& right() const    {        return mRight;    }    /////添加    TwoInterval(){};    TwoInterval(const TwoInterval &a);    TwoInterval &operator = (const TwoInterval &a);    ////private:    Interval mLeft;    Interval mRight;};//////TwoInterval::TwoInterval(const TwoInterval &a){    this->mLeft = a.mLeft;    this->mRight = a.mRight;}TwoInterval &TwoInterval::operator=(const TwoInterval &a){    this->mLeft = a.mLeft;    this->mRight = a.mRight;    return *this;}/////inline bool within(const TwoInterval& a, const TwoInterval& b){    return b.left() < a.left() && a.right() < b.right();}void input(vector<TwoInterval>& twos){    int m = 0;    {        string s;        getline(cin, s);        istringstream is(s);        is >> m;    }    for (int i = 0; i < m; ++i) {        string s;        getline(cin, s);        istringstream is(s);        size_t a, b, c, d;        is >> a >> b >> c >> d;        Interval left(a, b);        Interval right(c, d);        twos.emplace_back(left, right);    }}/////********************************////int intussusception(vector<TwoInterval>& two2){    vector<TwoInterval> result;    int len = two2.size();    if (len < 2) return len;    int resultNum = 0;    for (int k = 0; k < len; k++)    {        result.push_back(two2[k]);        for (int i = 1; i < len; i++)        {            int result_size = result.size();            for (int j = 0; j < result_size; j++)            {                if (within(two2[i], result[j]))                {                    if (j == (result_size-1))                    {                        result.push_back(two2[i]);                    }                    else                    {                        continue;                    }                }                if(within(result[j],two2[i]))                {                    result.insert(result.begin()+j,two2[i]);                }            }        }        if (resultNum < result.size())        {            resultNum = result.size();        }        result.clear();    }    return resultNum;}// ====== 结束 ========int main() {    vector<TwoInterval> twos;    input(twos);    cout << intussusception(twos) << endl;    return 0;}


阅读全文
1 0