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

思路
  对每一个二段式集合挨个遍历,比较嵌套的二段式集合的嵌套深度即可。
#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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值