Skip to content

Instantly share code, notes, and snippets.

@Masterkatze
Last active May 26, 2018 09:37
Show Gist options
  • Save Masterkatze/b979506c23f751603dffd567951a7ffe to your computer and use it in GitHub Desktop.
Save Masterkatze/b979506c23f751603dffd567951a7ffe to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
int main()
{
// s - subject list, t - target list
// s - data model, t - runtime model
std::vector<std::wstring> s = {L"b", L"c", L"a", L"e", L"d"};
std::vector<std::wstring> t = {L"c", L"e", L"d", L"a", L"g", L"f"};
//std::vector<std::wstring> s = {L"b", L"c", L"a", L"e", L"d"};
//std::vector<std::wstring> t = {};
//std::vector<std::wstring> s = {};
//std::vector<std::wstring> t = {L"c", L"e", L"d", L"a", L"g", L"f"};
auto sort_alphabetical = [](std::wstring left, std::wstring right) { return left < right; };
std::sort(s.begin(), s.end(), sort_alphabetical);
std::sort(t.begin(), t.end(), sort_alphabetical);
for (auto const& c : s) { std::wcout << c << ' '; } std::wcout << std::endl;
for (auto const& c : t) { std::wcout << c << ' '; } std::wcout << std::endl;
std::vector<std::wstring> d, i;
size_t x = 0, y = 0;
while (x < s.size() || y < t.size())
{
// If the target list is exhausted, delete the current element from the subject list
// If the target list is exhausted, a[x] can’t appear in it, and should be deleted.
if (y >= t.size())
{
d.push_back(s.at(x));
x++;
}
// If the subject list is exhausted, insert the current element from the target list
// If the subject list is exhausted, b[y] can’t appear in it, and should be inserted.
else if (x >= s.size())
{
i.push_back(t.at(y));
y++;
}
// If the current subject element precedes the current target element, delete the current subject element.
// If s[x] is ordered before t[y], then s[x] cannot appear in t[y:], and should be deleted.
else if (s.at(x) < t.at(y)) // "a" < "b"
{
d.push_back(s.at(x));
x++;
}
// If the current subject element follows the current target element, insert the current target element.
// If s[x] is ordered after t[y], then t[y] cannot appear in s[x:], and must be inserted.
else if (s.at(x) > t.at(y)) // "b" > "a"
{
i.push_back(t.at(y));
y++;
}
// The current elements match; consider the next pair.
else
{
x++;
y++;
}
}
std::wcout << "s: "; for (auto const& c : s) { std::wcout << c << ' '; } std::wcout << std::endl;
std::wcout << "t: "; for (auto const& c : t) { std::wcout << c << ' '; } std::wcout << std::endl;
std::wcout << "i: "; for (auto const& c : i) { std::wcout << c << ' '; } std::wcout << std::endl;
std::wcout << "d: "; for (auto const& c : d) { std::wcout << c << ' '; } std::wcout << std::endl;
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment