fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int uniqueDecimalRepresentations(const std::string& binary) {
  5. std::unordered_set<int> uniqueDecimals;
  6. int n = binary.size();
  7.  
  8. // Iterate through each character in the binary string
  9. for (int i = 0; i < n; i++) {
  10. // If the current character is '0', we add 0 to the set
  11. if (binary[i] == '0') {
  12. uniqueDecimals.insert(0);
  13. } else {
  14. // If the current character is '1', we iterate through the rest of the string
  15. // and convert the binary substring to decimal and add it to the set
  16. int num = 0;
  17. for (int j = i; j < n; j++) {
  18. num = num * 2 + (binary[j] - '0');
  19. uniqueDecimals.insert(num);
  20. }
  21. }
  22. }
  23.  
  24. // The size of the set gives us the count of unique decimal representations
  25. return uniqueDecimals.size();
  26. }
  27.  
  28. int main() {
  29. std::string binary = "011";
  30. std::cout << uniqueDecimalRepresentations(binary) << std::endl; // Output: 3
  31.  
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0.01s 5304KB
stdin
Standard input is empty
stdout
3