SHARE
    TWEET
    aqibm

    Untitled

    Apr 8th, 2025
    49
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    text 2.73 KB | None | 0 0
    1. #include <iostream>
    2. #include <vector>
    3. #include <string>
    4. #include <unordered_set>
    5. #include <algorithm>
    6. using namespace std;
    7. class Solution {
    8. public:
    9. // Main function: returns the maximum unique split as a vector of strings.
    10. vector<string> maxUniqueSplit(string s) {
    11. unordered_set<string> seen;
    12. auto res = backtrack(s, 0, seen);
    13. return res.second;
    14. }
    15. private:
    16. // Returns a pair {maxCount, splitVector} for substring s[start:].
    17. // maxCount = maximum number of unique substrings obtainable
    18. // splitVector = corresponding splitting (in order) that achieves that count.
    19. pair<int, vector<string>> backtrack(const string &s, int start, unordered_set<string> &seen) {
    20. int n = s.size();
    21. if (start == n) {
    22. // Reached the end: return count 0 and an empty splitting.
    23. return {0, vector<string>()};
    24. }
    25. int maxCount = 0;
    26. vector<string> bestSplitting; // best splitting for this subproblem
    27. // Try every possible substring starting from index 'start'
    28. for (int end = start + 1; end <= n; ++end) {
    29. string substring = s.substr(start, end - start);
    30. // If this substring hasn't been used yet
    31. if (seen.find(substring) == seen.end()) {
    32. // Mark it as used
    33. seen.insert(substring);
    34. // Recurse from 'end'
    35. auto candidate = backtrack(s, end, seen);
    36. int currCount = 1 + candidate.first;
    37. // Check if this candidate produces a larger count.
    38. if (currCount > maxCount) {
    39. maxCount = currCount;
    40. bestSplitting = candidate.second;
    41. // Prepend the current substring.
    42. bestSplitting.insert(bestSplitting.begin(), substring);
    43. }
    44. // Backtrack: remove the substring from seen
    45. seen.erase(substring);
    46. }
    47. }
    48. return {maxCount, bestSplitting};
    49. }
    50. };
    51. int main() {
    52. Solution sol;
    53. // Example 1:
    54. string s1 = "goooooogle";
    55. // Expected (one possible answer): ["g", "o", "oo", "ooo", "gl", "e"]
    56. vector<string> split1 = sol.maxUniqueSplit(s1);
    57. cout << "Splitting for \"" << s1 << "\": ";
    58. for (auto &part : split1) {
    59. cout << "\"" << part << "\" ";
    60. }
    61. cout << "\n";
    62. // Example 2:
    63. string s2 = "abccccd";
    64. // Expected (one possible answer): ["a", "b", "c", "cc", "cd"]
    65. vector<string> split2 = sol.maxUniqueSplit(s2);
    66. cout << "Splitting for \"" << s2 << "\": ";
    67. for (auto &part : split2) {
    68. cout << "\"" << part << "\" ";
    69. }
    70. cout << "\n";
    71. return 0;
    72. }
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /