Skip to content

Instantly share code, notes, and snippets.

@Caellian
Created October 27, 2018 14:43
Show Gist options
  • Select an option

  • Save Caellian/0670b86ca26ef359b3e1e6a222fa0787 to your computer and use it in GitHub Desktop.

Select an option

Save Caellian/0670b86ca26ef359b3e1e6a222fa0787 to your computer and use it in GitHub Desktop.
NKD
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
std::vector<std::string> nkd;
int k, n;
int main()
{
std::cin >> k >> n;
for(int i = 0; i < n; i++) {
std::string in;
std::cin >> in;
if (std::find(nkd.begin(), nkd.end(), in) != nkd.end()) {
nkd.erase(std::find(nkd.begin(), nkd.end(), in));
nkd.push_back(in);
} else {
if (nkd.size() >= k) {
nkd.erase(nkd.end());
nkd.push_back(in);
} else {
nkd.push_back(in);
}
}
}
std::cout << std::endl;
for(int i = k - 1; i >= 0; i--) {
std::cout << nkd[i] << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment