Witam, mam problem z tym programem, wyświetla złe wyniki. Wie może ktoś dlaczego? Zadanie typu CanConstruct. Mamy podany string a i vector b. Używając elementów z vectora próbujemy utworzyć wyraz a, jeśli można go ułożyć wyrzucamy true, inaczej false.
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;
//unordered_map<string, bool> memo;
bool canConstruct(string target, vector<string> wordBank, unordered_map<string, bool> &memo)
{
if (target == "")
return true;
if (memo.find(target) != memo.end())
return memo[target];
for (string word : wordBank) {
if (target.find(word, 0) == 0 && target.find(word, 0) != string::npos) {
string suffix = target; suffix.erase(0, word.length());
if (canConstruct(suffix, wordBank,memo) == true)
memo[target] = true;
return true;
}
}
memo[target] = false;
return false;
}
int main()
{
//TEST CASES:
unordered_map<string, bool> memo;
//cout << canConstruct("abcdef", { "ab","abc","cd","def","abcd" },memo) << '\n';
cout << canConstruct("skateboard", { "bo","rd","ate","t","ska","sk","boar" }, memo) << '\n';
cout << canConstruct("enterapotentpot", { "a","p","ent","enter","ot","o","t" }, memo) << '\n';
cout << canConstruct("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef", {
"e",
"ee",
"eee",
"eeee",
"eeeee",
"eeeeee"
}, memo);
}