Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript C++;:递归函数调用中传递的变量的行为如何?正确修改它的条件? 谁能让我知道为什么C++中的下面代码不起作用?< /P> // count number of ways to construct a target string from a given set of strings #include "bits/stdc++.h" using namespace std; int countConstruct(string target, vector < string > wordBank, map < string, int > memo = {}) { if (memo.find(target) != memo.end()) return memo.at(target); if (target == "") return 1; int totalCount = 0; for (auto word: wordBank) { if (target.find(word) == 0) { auto suffix = target.substr(word.length(), target.length() - word.length()); int numWaysForRest = countConstruct(suffix, wordBank, memo); totalCount += numWaysForRest; } } memo[target] = totalCount; return totalCount; } int main(int argc, char * argv[]) { cout << boolalpha; cout << countConstruct("abcdef", { "ab", "abc", "cd", "def", "abcd", "ef" }) << endl; cout << countConstruct("skateboard", { "bo", "ska", "ate", "sk", "boar" }) << endl; cout << countConstruct( "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", { "e", "eee", "eeee", "eeeee", "eeeeee", "eeeeeee" }) << endl; return 0; } 在上面的C++代码中,如果我让“备注”变量静态,程序就工作了。有谁能告诉我如何使程序在不使其静态的情况下仅通过递归工作?这些函数背后的内部结构是什么?_Javascript_C++_Recursion_Dynamic Programming_Memoization - Fatal编程技术网

Javascript C++;:递归函数调用中传递的变量的行为如何?正确修改它的条件? 谁能让我知道为什么C++中的下面代码不起作用?< /P> // count number of ways to construct a target string from a given set of strings #include "bits/stdc++.h" using namespace std; int countConstruct(string target, vector < string > wordBank, map < string, int > memo = {}) { if (memo.find(target) != memo.end()) return memo.at(target); if (target == "") return 1; int totalCount = 0; for (auto word: wordBank) { if (target.find(word) == 0) { auto suffix = target.substr(word.length(), target.length() - word.length()); int numWaysForRest = countConstruct(suffix, wordBank, memo); totalCount += numWaysForRest; } } memo[target] = totalCount; return totalCount; } int main(int argc, char * argv[]) { cout << boolalpha; cout << countConstruct("abcdef", { "ab", "abc", "cd", "def", "abcd", "ef" }) << endl; cout << countConstruct("skateboard", { "bo", "ska", "ate", "sk", "boar" }) << endl; cout << countConstruct( "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", { "e", "eee", "eeee", "eeeee", "eeeeee", "eeeeeee" }) << endl; return 0; } 在上面的C++代码中,如果我让“备注”变量静态,程序就工作了。有谁能告诉我如何使程序在不使其静态的情况下仅通过递归工作?这些函数背后的内部结构是什么?

Javascript C++;:递归函数调用中传递的变量的行为如何?正确修改它的条件? 谁能让我知道为什么C++中的下面代码不起作用?< /P> // count number of ways to construct a target string from a given set of strings #include "bits/stdc++.h" using namespace std; int countConstruct(string target, vector < string > wordBank, map < string, int > memo = {}) { if (memo.find(target) != memo.end()) return memo.at(target); if (target == "") return 1; int totalCount = 0; for (auto word: wordBank) { if (target.find(word) == 0) { auto suffix = target.substr(word.length(), target.length() - word.length()); int numWaysForRest = countConstruct(suffix, wordBank, memo); totalCount += numWaysForRest; } } memo[target] = totalCount; return totalCount; } int main(int argc, char * argv[]) { cout << boolalpha; cout << countConstruct("abcdef", { "ab", "abc", "cd", "def", "abcd", "ef" }) << endl; cout << countConstruct("skateboard", { "bo", "ska", "ate", "sk", "boar" }) << endl; cout << countConstruct( "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", { "e", "eee", "eeee", "eeeee", "eeeeee", "eeeeeee" }) << endl; return 0; } 在上面的C++代码中,如果我让“备注”变量静态,程序就工作了。有谁能告诉我如何使程序在不使其静态的情况下仅通过递归工作?这些函数背后的内部结构是什么?,javascript,c++,recursion,dynamic-programming,memoization,Javascript,C++,Recursion,Dynamic Programming,Memoization,为了澄清这一点,该程序的目标是将此函数记忆起来以加速递归函数,对吗?问题是第三个测试用例(带有大量“e”的字符串)似乎没有完成 如果是,则问题出在countConstruct声明中。 int countConstruct(字符串目标、向量字库、映射备忘录={}) >代码>备忘录是映射< /COD>类型变量,C++在每次递归调用时创建映射副本。随着地图越来越大,这种拷贝在许多重复调用中会变得非常昂贵。它使用了一个静态memo变量,因为没有在每次递归调用时都进行复制 修复它的一种方法是将memo声

为了澄清这一点,该程序的目标是将此函数记忆起来以加速递归函数,对吗?问题是第三个测试用例(带有大量“e”的字符串)似乎没有完成

如果是,则问题出在countConstruct声明中。
int countConstruct(字符串目标、向量字库、映射备忘录={})

<> >代码>备忘录<代码>是<代码>映射< /COD>类型变量,C++在每次递归调用时创建映射副本。随着地图越来越大,这种拷贝在许多重复调用中会变得非常昂贵。它使用了一个静态
memo
变量,因为没有在每次递归调用时都进行复制

修复它的一种方法是将memo声明为引用:
intcountconstruct(字符串目标、向量字库、map和memo)

通过这种方式,函数不必在每次递归调用时都复制映射,而只需要引用已经存在的映射。参考资料很便宜

这里的缺点是,
memo
arg不再具有默认值,每次从顶层调用函数时都必须显式地传入一个参数:

    map<string, int> memo = {}; 
    cout << countConstruct("abcdef", {"ab", "abc", "cd", "def", "abcd", "ef"}, memo) << endl;`
map memo={};

cout 1)应尽可能避免任何教导
#包括“bits/stdc++.h”
的源代码。2) 你有没有试着调试你的程序看看发生了什么?@Evg 1。我确实理解bits/stdc++.h的缺点,但作为一名有竞争力的程序员,在当前的环境下,我需要更多地关注编码,所以我需要库的可用性。2.我调试了代码,发现备忘录变量没有按预期更新。我不想访问竞争性的医生或是与有竞争力的飞行员一起飞行……如果C++递归可以通过值变量传递,这也使得它复制,为什么不在默认参数上?不管怎样,都不需要使用全局、静态变量或引用参数来实现它,这是问题的目的。请参阅这个问题,为什么C++不能有引用引用传递的默认参数,但更大的原因是C++不是JavaScript。内存管理是C++程序员的职责。
    map<string, int> memo = {}; 
    cout << countConstruct("abcdef", {"ab", "abc", "cd", "def", "abcd", "ef"}, memo) << endl;`