多重同时向量迭代的复杂度降低为O(n)

时间:2016-12-08 02:46:31

标签: c++ algorithm

所以我有2个字符串向量,其中包含以下内容:

tokens: name name place thing thing
u_tokens: name place thing

现在我的任务是同时遍历这两个向量并找到每个单词的出现并将其存储在第三个向量中。这是我做过的最小工作实现(我的任务没有提到重复项,所以我没有考虑删除它):

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
    vector<int> counts;
    vector<string> tokens;
    vector<string> u_tokens;

    tokens.push_back("name");
    tokens.push_back("name");
    tokens.push_back("place");
    tokens.push_back("thing");
    tokens.push_back("thing");
    u_tokens.push_back("name");
    u_tokens.push_back("place");
    u_tokens.push_back("thing");

    string temp;
    int temp_count = 0;
    for (int i = 0; i < tokens.size(); i++)
    {
        temp = tokens[i];
        for (int j = 0; j < u_tokens.size(); j++)
        {
            if(temp == u_tokens[j])
            {
                temp_count++;
            }
        }

        temp = tokens[i];
        for (int k = 0; k < tokens.size(); k++)
        {
            if (temp == tokens[k])
            {
                temp_count++;
            }
        }
        counts.push_back(temp_count);
        temp_count = 0;
    }

    for (vector<int>::const_iterator i = counts.begin(); i != counts.end(); ++i)
        cout << *i << "  ";
    return 0;
}

然而,我注意到,这显然有O(n^2)复杂性。如何将其缩减为O(n)?有可能吗?

的问候。

1 个答案:

答案 0 :(得分:1)

#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;

void CountOccurences(const vector<string>& input, unordered_map<string, size_t>& occurences)
{
    for (int i = 0; i < input.size(); i++)
    {
        occurences[input[i]]++;
    }
}

int main()
{
    vector<string> tokens;
    vector<string> u_tokens;
    unordered_map<string, size_t> occurences;

    tokens.push_back("name");
    tokens.push_back("name");
    tokens.push_back("place");
    tokens.push_back("thing");
    tokens.push_back("thing");
    u_tokens.push_back("name");
    u_tokens.push_back("place");
    u_tokens.push_back("thing");

    CountOccurences(tokens, occurences);
    CountOccurences(u_tokens, occurences);

    for (auto i : occurences)
        cout << i.first << "=" << i.second << " ";
    return 0;
}

使用std::unordered_map作为O(1)访问容器来创建O(N)解决方案。当然,在记忆成本方面。

链接到在线编译的program