zoukankan      html  css  js  c++  java
  • 腾讯面试题:A.txt和B.txt两个文件,A有1亿个qq号,B有100万个,用代码实现交、并、差

    在STL中关于有序序列有这么四个算法:

    set_union(beg, end, beg, end2, dest);                    //求并集A∪B

    set_union(beg, end, beg, end2, dest, comp);

    template <class InputIterator1, class InputIterator2, class OutputIterator>
      OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,
                                InputIterator2 first2, InputIterator2 last2,
                                OutputIterator result)
    {
      while (true)
      {
        if (first1==last1) return std::copy(first2,last2,result);
        if (first2==last2) return std::copy(first1,last1,result);
    
        if (*first1<*first2) { *result = *first1; ++first1; }
        else if (*first2<*first1) { *result = *first2; ++first2; }
        else { *result = *first1; ++first1; ++first2; }
        ++result;
      }
    }

    set_intersection(beg, end, beg, end2, dest);          //求交集A∩B

    set_intersection(beg, end, beg, end2, dest, comp);

    template <class InputIterator1, class InputIterator2, class OutputIterator>
      OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,
                                       InputIterator2 first2, InputIterator2 last2,
                                       OutputIterator result)
    {
      while (first1!=last1 && first2!=last2)
      {
        if (*first1<*first2) ++first1;
        else if (*first2<*first1) ++first2;
        else {
          *result = *first1;
          ++result; ++first1; ++first2;
        }
      }
      return result;
    }


    set_difference(beg, end, beg, end2, dest);           //求差集A-B

    set_difference(beg, end, beg, end2, dest, comp);

    template <class InputIterator1, class InputIterator2, class OutputIterator>
      OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1,
                                     InputIterator2 first2, InputIterator2 last2,
                                     OutputIterator result)
    {
      while (first1!=last1 && first2!=last2)
      {
        if (*first1<*first2) { *result = *first1; ++result; ++first1; }
        else if (*first2<*first1) ++first2;
        else { ++first1; ++first2; }
      }
      return std::copy(first1,last1,result);
    }


    set_symmetric_difference(beg, end, beg, end2, dest);                   //求对称差A∪B-A∩B

    set_symmetric_difference(beg, end, beg, end2, dest, comp); 

    template <class InputIterator1, class InputIterator2, class OutputIterator>
      OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1,
                                               InputIterator2 first2, InputIterator2 last2,
                                               OutputIterator result)
    {
      while (true)
      {
        if (first1==last1) return std::copy(first2,last2,result);
        if (first2==last2) return std::copy(first1,last1,result);
    
        if (*first1<*first2) { *result=*first1; ++result; ++first1; }
        else if (*first2<*first1) { *result = *first2; ++result; ++first2; }
        else { ++first1; ++first2; }
      }
    }

    所以能够直接用这样的stl的算法解决。假设一次无法把A文件读入内存,能够分块读取,多处理几次。



  • 相关阅读:
    BFS(广搜训练题目)
    练习赛1(补题)
    练习赛1(AC题)
    codeup 1743: 算法3-4:表达式求值
    数学相关(更新ing)
    c语言常用函数(更新ing)
    大牛的博客(学习不止,更新不止)
    51nod 1005 大数加法
    js1-----预览js内容
    css10---转载---定位,浮动
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/6867339.html
Copyright © 2011-2022 走看看