相关疑难解决方法(0)

Fibonacci序列的计算复杂性

我理解Big-O表示法,但我不知道如何为许多函数计算它.特别是,我一直试图弄清楚Fibonacci序列的幼稚版本的计算复杂性:

int Fibonacci(int n)
{
    if (n <= 1)
        return n;
    else
        return Fibonacci(n - 1) + Fibonacci(n - 2);
}
Run Code Online (Sandbox Code Playgroud)

Fibonacci序列的计算复杂度是多少以及如何计算?

complexity-theory big-o fibonacci time-complexity

310
推荐指数
8
解决办法
29万
查看次数

想要了解动态编程的人的简单示例

我正在为想要学习动态编程的人寻找一个易于理解的例子.关于什么是动态编程,这里有很好的答案.斐波那契序列是一个很好的例子,但它太小而不能划伤表面.它看起来是一个很好的主题,虽然我尚未参加算法课程,但希望它在我的春季名单上.

algorithm dynamic-programming

94
推荐指数
5
解决办法
9万
查看次数

如何使用Python找到两个字符串之间的最长公共子串?

我想编写一个 Python 代码来计算输入中两个字符串之间的最长公共子串。

例子:

word1 = input('Give 1. word: xlaqseabcitt')
word2 = input('Give 2. word: peoritabcpeor')
Run Code Online (Sandbox Code Playgroud)

想要的输出:

abc
Run Code Online (Sandbox Code Playgroud)

到目前为止我有这样的代码:

word1 = input("Give 1. word: ") 
word2 = input("Give 2. word: ")

longestSegment = "" 
tempSegment = ""

for i in range(len(word1)): 
    if word1[i] == word2[i]:
        tempSegment += word1[i] 
    else: 
        tempSegment = ""

if len(tempSegment) > len(longestSegment):
    longestSegment = tempSegment

print(longestSegment)
Run Code Online (Sandbox Code Playgroud)

当 word2 比 word1 短时,我最终会遇到 IndexError,并且它没有给我公共子字符串。

编辑:我找到了这个解决方案:

string1 = input('Give 1. word: ')
string2 = input('Give 2. word: ') …
Run Code Online (Sandbox Code Playgroud)

python

9
推荐指数
1
解决办法
3734
查看次数

并行循环中的惰性向量访问

性能关键的并行代码中,我有一个向量,其元素是:

  • 计算起来非常昂贵,结果是确定性的(给定位置的元素值仅取决于位置)
  • 随机访问(通常访问次数大于或大于向量的大小)
  • 集群访问(许多访问请求相同的值)
  • 向量由不同的线程共享(竞争条件?)
  • 为避免堆碎片整理,永远不应重新创建对象,但只要可能,就可以重置和回收
  • 放置在向量中的值将由多态对象提供

目前,我预先计算了向量的所有可能值,因此竞争条件不应成为问题.为了提高性能,我正在考虑创建一个惰性向量,这样代码只在请求向量元素时执行计算.在并行区域中,可能会发生多个线程同时请求并且可能同时计算相同元素的情况.我如何处理这种可能的竞争条件?

下面是我想要实现的一个例子.它在Windows 10,Visual Studio 17下编译和运行.我使用C++ 17.

// Lazy.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <stdlib.h>  
#include <chrono>
#include <math.h>
const double START_SUM = 1;
const double END_SUM = 1000;

//base object responsible for providing the values
class Evaluator
{
public:
    Evaluator() {};
    ~Evaluator() {};
    //Function with deterministic output, depending on the position
    virtual double expensiveFunction(int pos) const = 0;
};
// …
Run Code Online (Sandbox Code Playgroud)

c++ parallel-processing openmp c++17

6
推荐指数
1
解决办法
487
查看次数

蟒蛇lambda可以用来改变另一个函数的内部工作吗?

我有以下代码片段,我需要(大规模)加速.它是非常低效的.

possible_combos.append([comb for comb in
    itertools.combinations_with_replacement(input_list, target_number_of_packages)
        if np.sum([j[1] for j in comb])==target_number_of_packages])
Run Code Online (Sandbox Code Playgroud)

拆解:

possible_combos 
Run Code Online (Sandbox Code Playgroud)

是输出

input_list
Run Code Online (Sandbox Code Playgroud)

是一个元组列表([...],number_of_packages)

target_number_of_packages
Run Code Online (Sandbox Code Playgroud)

是我需要达到的包数.我可以根据需要组合列表"input_list"的多个元素(允许重复),但是在添加"number_of_packages"时需要准确到达target_number_of_packages,否则组合无效.

我在考虑类似的事情

possible_combos.append([comb for comb in
    itertools.combinations_with_replacement(input_list, lambda x:x=...)))
Run Code Online (Sandbox Code Playgroud)

但无法填补空白.

我的问题是,是否可以这样使用lambda?我不需要如何处理这个特定用例的答案,因为我已经解决了不同的问题(使用itertools产品作为递归生成器函数),但我仍然想知道,是否有解决方案?

简而言之:是否有可能使用lambda来改变另一个函数内的值对飞

问题的最小例子:

input_list=[1,2,3,4,5,6] #in minmal form

target_number_of_packages=4

possible_combos should be [[1,1,1,1],[2,1,1],[2,2],[3,1],[4]]
Run Code Online (Sandbox Code Playgroud)

我正在寻找大致相当于,但速度快于的东西,

possible_combos=[comb for comb in
    itertools.combinations_with_replacement(input_list) if np.sum(comb)==target_number_of_packages]
Run Code Online (Sandbox Code Playgroud)

只需将np.sum(comb)== target放入itertools.combinations_with_replacement中 - 如果可能的话.

我已经改变了这个问题,因为我以不同的方式解决了潜在的问题,但其中的一部分仍然是我想知道的.由于没有答案,我认为编辑是适当的.

python lambda python-3.x

6
推荐指数
1
解决办法
236
查看次数

"动态"编程与"普通"编程有何不同?

每当我看到计算机竞赛的解决方案时,我总会看到"动态编程"这个术语.我用Google搜索了这个术语并阅读了一些文章,但它们都没有提供编程VS"动态"编程的简单示例.那么"动态"编程与"普通"编程有何不同?(请简单说明!)

algorithm dynamic-programming

5
推荐指数
1
解决办法
1739
查看次数

重叠子问题和最优子结构有什么区别?

我了解两种方法的目标方法,其中最优子结构根据输入 n 计算最优解,而重叠子问题针对输入范围内的所有解,比如从 1 到 n。

对于像杆切割问题这样的问题。在这种情况下,在找到最佳切割时,我们是否考虑每个切割,因此可以将其视为重叠子问题并自下而上地工作。或者我们是否考虑给定输入 n 的最佳切割并自上而下工作。

因此,虽然他们最终确实处理了最优性,但两种方法之间的确切区别是什么。

我尝试参考这个重叠子问题最优子结构这个页面

另外,这是否与制表(自上而下)和记忆(自下而上)的解决方法有关?

这个线程提出了一个有效的观点,但我希望它可以更容易地分解。

dynamic-programming overlapping

3
推荐指数
1
解决办法
2537
查看次数

重新排列等式

我的C代码中有以下等式

k * dl * (1.0 + pHold / centre
       + (pHold * pHold) / (2.0 * centre * centre)
       - square / (2.0 * centre))
Run Code Online (Sandbox Code Playgroud)

我知道浮点除法比乘法要贵得多,而且我已经和它搏斗了一段时间.有没有办法重新排列这个来划分一个师?

谢谢

c math floating-point optimization performance

1
推荐指数
1
解决办法
240
查看次数