Advertisement
adfasdfadsfasdf

Untitled

Jul 21st, 2023
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. std::string output(int c, int s_idx, std::array<std::array<int, 20>, 26>& partials, std::array<int, 26> &freq, std::string &s) {
  4.     if (s_idx == partials.size()) return "!";
  5.     std::string ret = "!";
  6.  
  7.     while (s_idx < s.size() && freq[c] == 0) {
  8.         s_idx += 1;
  9.     }
  10.     for (int c_idx = 0; c_idx < 26; c_idx += 1) {
  11.         if (freq[c_idx] % 2 == 0) continue;
  12.         if (partials[s_idx][c_idx] == 0) continue;
  13.  
  14.         std::string best = output(c_idx + 1, s_idx + 1, partials, freq, s);
  15.         if (best != "!") {
  16.             ret = char(c_idx + 'A') + best;
  17.             break;
  18.         }
  19.     }
  20.     return ret;
  21. }
  22.  
  23. std::string solution(std::string s) {
  24.     // Implement your solution here
  25.     std::array<int, 26> freq = {};
  26.     std::array<std::array<int, 20>, 26> partials = {};
  27.  
  28.     for (int idx = 0; idx < freq.size(); idx += 1) freq[idx] = 0;
  29.  
  30.     for (int idx = 0; idx < s.size(); idx += 1) {
  31.         char ch = s[idx];
  32.         if (idx > 0) {
  33.             for (int c = 0; c < 26; c++) {
  34.                 partials[idx][c] = partials[idx - 1][c];
  35.             }
  36.         }
  37.         partials[idx][ch - 'A'] += 1;
  38.         freq[ch - 'A'] += 1;
  39.     }
  40.  
  41.     for (auto c = 0; c < 26; c++) {
  42.         std::cout << freq[c] << ", ";
  43.     }
  44.  
  45.     for (auto c = 0; c < 26; c++) {
  46.         for (auto i = 0; i < partials.size(); i++) {
  47.             std::cout << partials[i][c] << ", ";
  48.         }
  49.         std::cout << std::endl;
  50.     }
  51.     std::cout << std::endl;
  52.  
  53.     return output(0, 0, partials, freq, s);
  54. }
  55.  
  56. int main() {
  57.     std::cout << solution("BAAXA") << std::endl;
  58.     return 0;
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement