find_shortest_sequences.cc 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "find_shortest_sequences.h"
  2. #include "find_shortest_sequences_from_to.h"
  3. #include <iostream>
  4. std::vector<std::string> FindShortestSequences(
  5. std::string_view code,
  6. std::function<std::optional<char>(char, char)> apply_cmd) {
  7. std::vector<std::string> results;
  8. char from = 'A';
  9. for (char to : code) {
  10. auto sub_results = FindShortestSequencesFromTo(from, to, apply_cmd);
  11. from = to;
  12. if (results.empty()) {
  13. results = std::move(sub_results);
  14. continue;
  15. }
  16. std::vector<std::string> new_results;
  17. for (size_t i = 0; i < results.size(); ++i) {
  18. for (size_t j = 0; j < sub_results.size(); ++j) {
  19. new_results.emplace_back(results[i] + sub_results[j]);
  20. }
  21. }
  22. results = std::move(new_results);
  23. }
  24. size_t min_size = results[0].size();
  25. for (const auto &r : results) {
  26. if (r.size() < min_size) {
  27. min_size = r.size();
  28. }
  29. }
  30. std::vector<std::string> min_results;
  31. for (auto &r : results) {
  32. if (r.size() == min_size) {
  33. min_results.emplace_back(std::move(r));
  34. }
  35. }
  36. return min_results;
  37. }