#include "designer.h" std::list designer::generate(std::mt19937& rng) const { std::list result; size_t cur = 0; while (cur < text_.length()) { const solution& curSol = get(cur); const std::vector& posLens = curSol.lengths; std::uniform_int_distribution lenDist(0, posLens.size() - 1); size_t len = posLens.at(lenDist(rng)); const ps_type& prefix = curSol.prefix; std::uniform_int_distribution cardDist(0, prefix.getCount() - 1); size_t cardIndex = cardDist(rng); std::tuple pd = prefix.at(cardIndex); result.emplace_back(std::get<0>(pd), std::get<1>(pd), len); cur += len; } return result; } solution designer::calculate(size_t i) const { if (i == text_.length()) { return {titles_, {}, 0}; } const ps_type& prefix = titles_.find(text_, i); bool foundScore = false; size_t bestScore; std::vector bestLens; for (int j = 1; (j <= prefix.getDepth()) && (i + j <= text_.length()); j++) { const solution& subSol = get(i + j); if (subSol.score > 0 || (i + j == text_.length())) { size_t tempScore = subSol.score + 1; if (!foundScore || tempScore < bestScore) { foundScore = true; bestScore = tempScore; bestLens.clear(); bestLens.push_back(j); } else if (tempScore == bestScore) { bestLens.push_back(j); } } } if (!foundScore) { return {titles_, {}, 0}; } else { return {prefix, std::move(bestLens), bestScore}; } } const solution& designer::get(size_t i) const { if (!solutions_.at(i)) { solutions_[i] = std::make_unique(calculate(i)); } return *solutions_.at(i); }