C++ 返回子字符串
我怎样才能用它来表示x个数量的数字?对于我的方法,它硬编码为2个子字符串。还有没有时间复杂度更低的更好方法?这里可能有一个循环孔,需要固定num im通过的数量,因为我根本没有使用um参数。您当前的方法存在一些问题,包括硬编码的最大ngram数量和固定的ngram大小。此外,短变量名和缺少注释无助于向正在阅读代码的人解释代码 一个更简单的解决方案是使用C++ 返回子字符串,c++,C++,我怎样才能用它来表示x个数量的数字?对于我的方法,它硬编码为2个子字符串。还有没有时间复杂度更低的更好方法?这里可能有一个循环孔,需要固定num im通过的数量,因为我根本没有使用um参数。您当前的方法存在一些问题,包括硬编码的最大ngram数量和固定的ngram大小。此外,短变量名和缺少注释无助于向正在阅读代码的人解释代码 一个更简单的解决方案是使用map来计算每个ngram发生的次数,然后找到计数最高的一个。这将给rouglyN.logN带来时间复杂性。或者,无序映射将更接近线性时间复杂度
map
来计算每个ngram发生的次数,然后找到计数最高的一个。这将给rouglyN.logN
带来时间复杂性。或者,无序映射
将更接近线性时间复杂度
当然会有一种边缘情况,其中多个ngram出现在相同的最高计数。您需要决定应该使用各种策略中的哪一种来解决这个问题。在我的示例中,我利用std::map
的内在顺序来选择排序顺序最低的ngram。如果使用unordered_-map
,您将需要一种不同的策略以确定的方式解决争用
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
std::string ngram(const std::string &input, int num)
{
if (num <= 0 || num > input.size()) return "";
// Count ngrams of size 'num'
std::map<std::string, int> ngram_count;
for(size_t i = 0; i <= input.size() - num; i++)
{
++ngram_count[input.substr(i, num)];
}
// Select ngram with highest count
std::map<std::string, int>::iterator highest = std::max_element(
ngram_count.begin(), ngram_count.end(),
[](const std::pair<std::string, int>& a, const std::pair<std::string, int>& b)
{
return a.second < b.second;
});
// Return ngram with highest count, otherwise empty string
return highest != ngram_count.end() ? highest->first : "";
}
int main()
{
std::cout << ngram("engineering", 2) << std::endl;
std::cout << ngram("engineering", 3) << std::endl;
return 0;
}
#包括
#包括
#包括
#包括
std::string ngram(const std::string&input,int num)
{
if(num input.size())返回“”;
//计数大小为“num”的nGram
标准::map ngram_计数;
对于(大小i=0;i第一个:”;
}
int main()
{
std::cout我做的和paddy有点不同,所以我想我会把它贴出来。使用std::set
。他解释了这个问题,所以你的答案应该得到认可
struct test {
test(const std::string& str) :val(str), cnt(0) {}
test(const test& thet) { *this = thet; }
std::string val;
int cnt;
friend bool operator < (const test& a, const test& b) { return a.val < b.val; }
};
using test_set_type = std::set<test>;
const test ngram(std::string A, int num) {
test_set_type set;
for (auto it = A.begin(); it < A.end() - num + 1; ++it)
{
auto found = set.find(std::string(it, it + num));
if (found != set.end())
++const_cast<test&>(*found).cnt;
else
set.insert(std::string(it, it + num));
}
int find = -1;
test_set_type::iterator high = set.begin();
for (auto it = set.begin(); it != set.end(); ++it)
if(it->cnt > find)
++find, high= it;
return *high;
}
int main() {
int num = 2;
std::string word("engineering");
std::cout << ngram(word, num).val << std::endl;
return 0;
}
struct测试{
test(const std::string&str):val(str),cnt(0){
test(const test&thet){*this=thet;}
std::字符串val;
int-cnt;
友元布尔运算符<(常数测试&a,常数测试&b){返回a.valcnt>查找)
++发现,高=它;
返回*高;
}
int main(){
int num=2;
标准::字符串字(“工程”);
std::难道你不能编辑和删除该问题!!!