阿里内推编程测试题

题目:


思路:

在网上跟大家交流后,才知道自己漏看了题目,后来想了想,现将思路贴出来,供大家交流

目的:最多可以取出多少个能够组成嵌套集

如果存在一个子嵌套集,而新增加的一个二段模式与子嵌套集的某个二段模式冲突(不相互嵌套),它虽然不能加入该嵌套子集,
但是它却可以和该子集中不冲突的其他二段模式组成子集,所以很难处理。因此,从这个角度来思考,太过于复杂。

从上面的思路中我们可以得到一些有用的结论:针对存在的一个子嵌套集,新的二段模式的加入,可以生成新的子嵌套集,也就
是造成分支;进一步我们可以认为,一个二段模式最多可以生成一个分支,而且一个分支主要是因为某个二段模式与其他嵌套集
冲突造成的,也就是一个二段模式可以对应一个嵌套集;N个二段模式最多组成N个独立的嵌套集。


算法流程:
1、初始N个嵌套集,分别包含编号0~N-1二段模式
2、针对某个二段模式,遍历查看是否可以满足加入嵌套集的条件(两两相互嵌套,而且该二段模式之前不存在)
   满足的话,将其加入
3、找出最大的嵌套集

  1. #include <iostream>  
  2. #include <sstream>  
  3. #include <string>  
  4. #include <vector>  
  5. #include <deque>  
  6. #include <cassert>  
  7. #include <map>  
  8. #include <algorithm>  
  9.   
  10. using namespace std;  
  11.   
  12. class Interval  
  13. {  
  14. public:  
  15.     explicit Interval(size_t left, size_t right)  
  16.         : mLeft(left),  
  17.         mRight(right)  
  18.     {  
  19.         assert(left <= right);  
  20.     }  
  21.   
  22.     size_t left() const  
  23.     {  
  24.         return mLeft;  
  25.     }  
  26.   
  27.     size_t right() const  
  28.     {  
  29.         return mRight;  
  30.     }  
  31.     /添加  
  32.     Interval(){};  
  33.     Interval(const Interval &a);  
  34.     Interval &operator = (const Interval &a);  
  35.       
  36. private:  
  37.     size_t mLeft;  
  38.     size_t mRight;  
  39. };  
  40.   
  41. //  
  42. Interval::Interval(const Interval &a)  
  43. {  
  44.     this->mLeft = a.mLeft;  
  45.     this->mRight = a.mRight;  
  46. }  
  47. Interval &Interval::operator =(const Interval &a)  
  48. {  
  49.     this->mLeft = a.mLeft;  
  50.     this->mRight = a.mRight;  
  51.     return this;  
  52. }  
  53. /  
  54.   
  55. inline bool operator<(const Interval& a, const Interval& b)  
  56. {  
  57.     return a.right() < b.left();  
  58. }  
  59.   
  60. class TwoInterval  
  61. {  
  62. public:  
  63.     explicit TwoInterval(const Interval& left, const Interval& right)  
  64.         : mLeft(left),  
  65.         mRight(right)  
  66.     {  
  67.         assert(left < right);  
  68.     }  
  69.   
  70.     const Interval& left() const  
  71.     {  
  72.         return mLeft;  
  73.     }  
  74.   
  75.     const Interval& right() const  
  76.     {  
  77.         return mRight;  
  78.     }  
  79.     /添加  
  80.     TwoInterval(){};  
  81.     TwoInterval(const TwoInterval &a);  
  82.     TwoInterval &operator = (const TwoInterval &a);  
  83.       
  84.   
  85. private:  
  86.     Interval mLeft;  
  87.     Interval mRight;  
  88. };  
  89. //  
  90. TwoInterval::TwoInterval(const TwoInterval &a)  
  91. {  
  92.     this->mLeft = a.mLeft;  
  93.     this->mRight = a.mRight;  
  94. }  
  95. TwoInterval &TwoInterval::operator=(const TwoInterval &a)  
  96. {  
  97.     this->mLeft = a.mLeft;  
  98.     this->mRight = a.mRight;  
  99.     return this;  
  100. }  
  101. /  
  102.   
  103.   
  104. inline bool within(const TwoInterval& a, const TwoInterval& b)  
  105. {  
  106.     return b.left() < a.left() && a.right() < b.right();  
  107. }  
  108.   
  109. void input(vector<TwoInterval>& twos)  
  110. {  
  111.     int m = 0;  
  112.     {  
  113.         string s;  
  114.         getline(cin, s);  
  115.         istringstream is(s);  
  116.         is >> m;  
  117.     }  
  118.     for (int i = 0; i < m; ++i) {  
  119.         string s;  
  120.         getline(cin, s);  
  121.         istringstream is(s);  
  122.         size_t a, b, c, d;  
  123.         is >> a >> b >> c >> d;  
  124.         Interval left(a, b);  
  125.         Interval right(c, d);  
  126.         twos.emplace_back(left, right);  
  127.     }  
  128. }  
  129.   
  130. // ====== 填入你自己的逻辑 ========  
  131.   
  132. /**********************/  
  133. / 
  134. 目的:最多可以取出多少个能够组成嵌套集 
  135.  
  136. 思路: 
  137.  
  138. 如果存在一个子嵌套集,而新增加的一个二段模式与子嵌套集的某个二段模式冲突(不相互嵌套),它虽然不能加入该嵌套子集, 
  139. 但是它却可以和该子集中不冲突的其他二段模式组成子集,所以很难处理。因此,从这个角度来思考,太过于复杂。 
  140.  
  141. 从上面的思路中我们可以得到一些有用的结论:针对存在的一个子嵌套集,新的二段模式的加入,可以生成新的子嵌套集,也就 
  142. 是造成分支;进一步我们可以认为,一个二段模式最多可以生成一个分支,而且一个分支主要是因为某个二段模式与其他嵌套集 
  143. 冲突造成的,也就是一个二段模式可以对应一个嵌套集;N个二段模式最多组成N个独立的嵌套集。 
  144.  
  145. 算法流程: 
  146. 1、初始N个嵌套集,分别包含编号0~N-1二段模式 
  147. 2、针对某个二段模式,遍历查看是否可以满足加入嵌套集的条件(两两相互嵌套,而且该二段模式之前不存在) 
  148.    满足的话,将其加入 
  149. 3、找出最大的嵌套集 
  150.  
  151. /  
  152. /**********************/  
  153.   
  154. inline bool operator==(const Interval& a, const Interval& b)  
  155. {  
  156.     return (a.right() == b.right()) && (a.left() == b.left());  
  157. }  
  158.   
  159.   
  160. inline bool operator==(const TwoInterval& a, const TwoInterval& b)  
  161. {  
  162.     return (a.right() == b.right()) && (a.left() == b.left());  
  163. }  
  164.   
  165. bool confl(const TwoInterval& a, const TwoInterval& b)  
  166. {  
  167.     if (!(within(a, b) || within(b, a))) return true;  
  168.     if (a == b) return true;  
  169.     return false;  
  170. }  
  171.   
  172.   
  173. bool add(vector<TwoInterval> &a, TwoInterval b)  
  174. {  
  175.     for (TwoInterval ti : a)  
  176.     {  
  177.         if (confl(ti,b))  
  178.         {  
  179.               
  180.             return false;  
  181.         }  
  182.     }  
  183.     a.push_back(b);  
  184.     return true;  
  185. }  
  186.   
  187.   
  188. int intussusception(vector<TwoInterval>& two2)  
  189. {  
  190.     int len = two2.size();  
  191.     if (len < 2) return len;  
  192.     int max = 0;  
  193.     vector<vector<TwoInterval> > res(len);  
  194.     for (int i = 0; i < len; i++)  
  195.     {  
  196.         res[i].push_back(two2[i]);  
  197.     }  
  198.     for (int i = 0; i < len; i++)  
  199.     {  
  200.         for (int j = 0; j < len; j++)  
  201.         {  
  202.             add(res[j], two2[i]);  
  203.         }  
  204.     }  
  205.     for (int i = 0; i < len; i++)  
  206.     {  
  207.         if (res[i].size()>max)  
  208.         {  
  209.             max = res[i].size();  
  210.         }  
  211.     }  
  212.     return max;  
  213. }  
  214.   
  215. // ====== 结束 ========  
  216.   
  217. int main() {  
  218.     vector<TwoInterval> twos;  
  219.     input(twos);  
  220.   
  221.     cout << intussusception(twos) << endl;  
  222.   
  223.     return 0;  
  224. }  

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值