在多个同时矢量迭代中,复杂性降低至O(n)

Complexity Reduction to O(n) Over Multiple Simultaeneous Vector Iteration

本文关键字:复杂性 迭代      更新时间:2023-10-16

所以我有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)?有可能吗?

问候。

#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)解决方案。当然,记忆成本。

链接到在线编译程序