zoukankan      html  css  js  c++  java
  • 如何判断是否中奖

    如何判断是否中奖

    问题描述:

    假设开奖结果为:A B C,A、B、C可能相同也可能不同,抽奖结果为:X Y Z,X、Y、Z可能相同也可能不同。如何判断A B C与X Y Z有多少个相同的数字。

    本质上是求两个集合的交集。

    交集操作:

    1)  直观的做法:

    针对集合2种的每个元素,查找其在集合1种是否出现了,如果出现则纳入集合3。对集合2种的元素操作完后,对得到的集合3进行去重,即为交集。

    如果是顺序查找则时间复杂度是O(M*N)——实现1

    如果是二分查找,二分查找的前提是排序,则时间复杂度是O(N*logN)或O(M*logM)。

             2)改进的做法:

    分别对集合1、2排序,顺序扫描,进行判断,对匹配的元素纳入交集。由于需要排序,所以时间复杂度为O(N*logN)或O(M*logM)——实现2

    实现:

    1)  顺序查找

    // 实现1
    #include <iostream>
    #include <vector>
    #include <set>
    #include <cstring>
    using namespace std;
    
    int inter(const vector<char>& set1, const vector<char>& set2, set<char>& set3)
    {
        // 因为是顺序查找,所以不用计较 set1 和 set2 的顺序
        // 如果是二分查找,则要考虑二者的顺序,如果 set1.size() > set2.size()
        // 则 for set2, find set1;
        // 时间复杂度为 O(set2.size()*log(set1.size())) < O(set1.size()*log(set2.size()))
        // 如果 set1.size() < set2.size()
        // 则 for set1, find set2; 
        // 时间复杂度为 O(set1.size()*log(set2.size())) < O(set2.size()*log(set1.size()))
        set3.clear();
        for (vector<int>::size_type i = 0; i != set1.size(); ++i)
        {
            for (vector<int>::size_type j = 0; j != set2.size(); ++j)
            {
                if (set1[i] == set2[j])
                {
                    set3.insert(set1[i]);
                }
            }
        }
        return set3.size();
    }
    
    int main()
    {
        char a[] = "abcdefg";
        char b[] = "abcxxfg";
        // char a[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
        // char b[] = {'a', 'b', 'c', 'x', 'x', 'f', 'g'};
        vector<char> set1(a, a+strlen(a));
        vector<char> set2(b, b+strlen(b));
        
        set<char> set3;
        inter(set1, set2, set3);
        
        for (set<char>::const_iterator cit = set3.begin(); cit!= set3.end(); ++cit)
        {
            cout << *cit << ' ';
        }
        cout << endl;
        system("PAUSE");
        return 0;
    }

    2)  先排序后扫描

    // 实现2
    #include <iostream>
    #include <vector>
    #include <set>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    int inter_2(const vector<char>& seta, const vector<char>& setb, vector<char>& set3)
    {
        set3.clear();
        vector<char> set1(seta);
        vector<char> set2(setb);
        sort(set1.begin(), set1.end());
        sort(set2.begin(), set2.end());
        for (int i = 0, j = 0; i != set1.size() && j != set2.size();)
        {
            if (set1[i] == set2[j])
            {
                set3.push_back(set1[i]);
                ++i;
                ++j;
            }
            else if (set1[i] < set2[j])
            {
                ++i;
            }
            else
            {
                ++j;
            }
        }
        return set3.size();
    }
    
    int main()
    {
        char a[] = "abcdefg";
        char b[] = "abcxxfg";
        // char a[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
        // char b[] = {'a', 'b', 'c', 'x', 'x', 'f', 'g'};
        vector<char> set1(a, a+strlen(a));
        vector<char> set2(b, b+strlen(b));
        
        vector<char> set3;
        inter_2(set1, set2, set3);
        
        for (vector<char>::const_iterator cit = set3.begin(); cit!= set3.end(); ++cit)
        {
            cout << *cit << ' ';
        }
        cout << endl;
        system("PAUSE");
        return 0;
    }

    (完)

     

    文档信息


    ·版权声明:自由转载-非商用-非衍生-保持署名 | Creative Commons BY-NC-ND 3.0
    ·博客地址:http://www.cnblogs.com/unixfy
    ·博客作者:unixfy
    ·作者邮箱:goonyangxiaofang(AT)163.com
    ·如果你觉得本博文的内容对你有价值,欢迎对博主 
    小额赞助支持


     

     

  • 相关阅读:
    English,The Da Vinci Code, Chapter 23
    python,meatobject
    English,The Da Vinci Code, Chapter 22
    English,The Da Vinci Code, Chapter 21
    English,The Da Vinci Code, Chapter 20
    English,The Da Vinci Code, Chapter 19
    python,xml,ELement Tree
    English,The Da Vinci Code, Chapter 18
    English,The Da Vinci Code, Chapter 17
    English,The Da Vinci Code, Chapter 16
  • 原文地址:https://www.cnblogs.com/unixfy/p/3147377.html
Copyright © 2011-2022 走看看