阿里巴巴2017校招C++岗位在线编程题-RNA嵌套
2017-07-12 20:25
465 查看
思路
对每一个二段式集合挨个遍历,比较嵌套的二段式集合的嵌套深度即可。
#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&l 4000 t;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; }
相关文章推荐
- 阿里巴巴2017校招C++岗位在线编程题-求集合D的最大值,最小值和元素个数三者之和
- 阿里巴巴2016校招在线笔试附加题(C++研发)
- 阿里巴巴2015秋季校招(客户端开发工程师岗位)在线笔试题
- 今日头条2017秋季校招在线编程题解法参考
- 阿里巴巴2017 Java在线编程题
- 2017阿里巴巴校招在线笔试——货架格子编号
- 网易2017校招内推在线笔试编程题3
- 兔子繁衍问题求解(阿里巴巴2017在线编程题)
- 2017 8.25 阿里巴巴校招 在线笔试题
- 去哪儿网2017校招在线笔试(前端工程师)编程题及JavaScript代码
- 采购单-京东2017在线笔试编程题C++
- 【在线笔试题解题报告系列】网易2017校招内推笔试之编程题【持续更新】
- 有多少个进程被 fork 出来了?----阿里巴巴2015校招研发在线笔试题
- indeed2017校招在线编程题(网测)二
- 网易2017校招内推笔试练习卷编程题2--出专辑
- 有两个32bit的数A、B,使用下面方式得到32bit的数C、D。哪一种可以使用C、D得到A、B的值?----阿里巴巴2015校招研发在线
- 如何设计数据表、解决数据库并发访问瓶颈、数据库事务----阿里巴巴2015校招研发在线
- 下列不属于hash碰撞解决方法的是()。----阿里巴巴2015校招研发在线笔试题
- 如何扩展 web 服务器?----阿里巴巴2015校招研发在线
- 2016届 阿里巴巴校招研发工程师C/C++笔试题--2015.08.23