#include <iostream> #include <map> #include <unordered_set> #include <string> using namespace std; // 默认输入合法 int main(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); std::setvbuf(stdout, nullptr, _IOFBF, BUFSIZ); //当作字符串处理方便很多 string num; cin>>num; //记录已经有的字符 unordered_set<char> charSet; //逆向遍历 const auto end = num.crend(); auto begin = num.crbegin(); while (begin != end) { //若不存在该字符 if(charSet.find(*begin) == charSet.end()){ charSet.insert(*begin); cout<<*begin; } begin++; } return 0; }
不存在就输出字符。逆向遍历