#include <iostream>
#include <string>
using namespace std;
void sequentiallyFindStringAllUnique(string s) {
// pad the string to size 256
if (s.size() < 256) {
s.append(256-s.size(),0);
}
// first scan, maintain a[a[i]] = a[i] property
for (int i = 0; i<s.size(); i++) {
while(s[((int)s[i])] != s[i]) {
swap(s[i], s[((int)s[i])]);
}
}
// second scan destroy a[a[i]] = a[i] property for duplicates
for (int i = 0; i<s.size(); i++) {
if(s[i] != i && (s[s[i]] == s[i])) {
s[s[i]] = i;
}
}
// third scan, output all unique chars
for (int i = 0; i<256; i++) {
if(s[i] == i) cout<<s[i];
}
cout<<endl;
}
int main() {
sequentiallyFindStringAllUnique("aa12b33c442");
sequentiallyFindStringAllUnique("1");
sequentiallyFindStringAllUnique("");
sequentiallyFindStringAllUnique("121");
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdm9pZCBzZXF1ZW50aWFsbHlGaW5kU3RyaW5nQWxsVW5pcXVlKHN0cmluZyBzKSB7CgkvLyBwYWQgdGhlIHN0cmluZyB0byBzaXplIDI1NgoJaWYgKHMuc2l6ZSgpIDwgMjU2KSB7CgkJcy5hcHBlbmQoMjU2LXMuc2l6ZSgpLDApOwoJfQoJLy8gZmlyc3Qgc2NhbiwgbWFpbnRhaW4gYVthW2ldXSA9IGFbaV0gcHJvcGVydHkKCWZvciAoaW50IGkgPSAwOyBpPHMuc2l6ZSgpOyBpKyspIHsKCQl3aGlsZShzWygoaW50KXNbaV0pXSAhPSBzW2ldKSB7CgkJCXN3YXAoc1tpXSwgc1soKGludClzW2ldKV0pOwoJCX0KCX0KCS8vIHNlY29uZCBzY2FuIGRlc3Ryb3kgYVthW2ldXSA9IGFbaV0gcHJvcGVydHkgZm9yIGR1cGxpY2F0ZXMKCWZvciAoaW50IGkgPSAwOyBpPHMuc2l6ZSgpOyBpKyspIHsKCQlpZihzW2ldICE9IGkgJiYgKHNbc1tpXV0gPT0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