Skip to content

Instantly share code, notes, and snippets.

@prehistoricpenguin
Created March 17, 2015 02:58
Show Gist options
  • Save prehistoricpenguin/ca9c246a8004b682cebb to your computer and use it in GitHub Desktop.
Save prehistoricpenguin/ca9c246a8004b682cebb to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <unordered_map>
#include <limits>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
int main() {
std::string org;
std::string type;
std::cin >> org >> type;
auto trans_pred = [](char ch) { return std::toupper(ch); };
std::transform(std::begin(org), std::end(org), std::begin(org), trans_pred);
std::transform(std::begin(type), std::end(type), std::begin(type), trans_pred);
std::set<char> memed;
auto mem_print = [&](char ch) {
if (!memed.count(ch)) {
std::cout << ch;
memed.insert(ch);
}
};
size_t j = 0;
for (size_t i = 0; i < type.size(); ++i, ++j) {
if (type[i] != org[j]) {
while (type[i] != org[j]) {
mem_print(org[j++]);
}
}
}
while (j < org.size()) {
mem_print(org[j++]);
}
return 0;
}
@prehistoricpenguin
Copy link
Author

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <unordered_map>
#include <limits>
#include <map>
#include <iterator>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>

int main() {
    std::string org;
    std::string type;
    std::cin >> org >> type;

    auto trans_pred = [](char ch) { return std::toupper(ch); };
    std::transform(std::begin(org), std::end(org), std::begin(org), trans_pred);
    std::transform(std::begin(type), std::end(type), std::begin(type), trans_pred);

    std::map<char, size_t> mem_meet;
    for (size_t i = 0; i < org.size(); ++i) {
        char ch = org[i];
        if (!mem_meet.count(ch)) {
            mem_meet[ch] = i;
        }
    }
    std::set<char> set_org(std::begin(org), std::end(org));
    std::set<char> set_type(std::begin(type), std::end(type));
    std::vector<char> diff_res;
    std::set_difference(std::begin(set_org), std::end(set_org),
            std::begin(set_type), std::end(set_type),
            std::back_inserter(diff_res));
    std::sort(std::begin(diff_res), std::end(diff_res),
            [&](const char& lhs, const char& rhs) {
                return mem_meet[lhs] < mem_meet[rhs];
            });
    std::copy(std::begin(diff_res), std::end(diff_res),
            std::ostream_iterator<char>(std::cout, ""));

    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment