fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. void sequentiallyFindStringAllUnique(string s) {
  6. // pad the string to size 256
  7. if (s.size() < 256) {
  8. s.append(256-s.size(),0);
  9. }
  10. // first scan, maintain a[a[i]] = a[i] property
  11. for (int i = 0; i<s.size(); i++) {
  12. while(s[((int)s[i])] != s[i]) {
  13. swap(s[i], s[((int)s[i])]);
  14. }
  15. }
  16. // second scan destroy a[a[i]] = a[i] property for duplicates
  17. for (int i = 0; i<s.size(); i++) {
  18. if(s[i] != i && (s[s[i]] == s[i])) {
  19. s[s[i]] = i;
  20. }
  21. }
  22. // third scan, output all unique chars
  23. for (int i = 0; i<256; i++) {
  24. if(s[i] == i) cout<<s[i];
  25. }
  26. cout<<endl;
  27. }
  28.  
  29. int main() {
  30. sequentiallyFindStringAllUnique("aa12b33c442");
  31. sequentiallyFindStringAllUnique("1");
  32. sequentiallyFindStringAllUnique("");
  33. sequentiallyFindStringAllUnique("121");
  34. return 0;
  35. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
1bc
1

2