Skip to content

Instantly share code, notes, and snippets.

@yuikns
Last active August 29, 2015 14:14
Show Gist options
  • Save yuikns/e54bf54231b0587cd190 to your computer and use it in GitHub Desktop.
Save yuikns/e54bf54231b0587cd190 to your computer and use it in GitHub Desktop.
// Copyright [2015] <Yu Jing>
#include <cstdio>
#include <string>
#include <map>
using std::string;
using std::map;
char ch_searcher(const std::string & s) {
map<char, bool> labels;
for (unsigned i = 0; i < s.length(); i++) {
if (labels.find(s[i]) != labels.end()) {
labels[s[i]] = false;
} else {
labels.insert(std::make_pair(s[i], true));
}
}
for (unsigned i = 0; i < s.length(); i++) {
if (labels[s[i]]) return s[i];
}
return -1;
}
int main(int argc, char * argv[]) {
const char * dstr = "abcab";
if (argc > 1) {
printf("%c - %d \n", ch_searcher(argv[1]), ch_searcher(argv[1]));
} else {
printf("%c - %d \n", ch_searcher(dstr), ch_searcher(dstr));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment