Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <vector>
- using namespace std;
- vector<string> split(string& str) {
- vector<string> v;
- istringstream ss(str);
- string s;
- while (ss >> s) {
- v.push_back(s);
- }
- return v;
- }
- int getIndex(vector<string>& v, string s) {
- for (int i = 0; i < v.size(); i++) {
- if (v[i] == s) {
- return i;
- }
- }
- return -1;
- }
- int main() {
- int commandNumbers, index = 0;
- cin >> commandNumbers;
- cin.ignore();
- string line;
- getline(cin, line);
- vector<string> command, houses = split(line);
- for (int i = 0; i < commandNumbers; i++) {
- int currentIndex = index;
- getline(cin, line);
- command = split(line);
- if (command[0] == "Forward" || command[0] == "Back") {
- int steps = stoi(command[1]);
- currentIndex += (command[0] == "Forward" ? steps : -steps);
- if (currentIndex >= 0 && currentIndex < houses.size()) {
- index = currentIndex;
- houses.erase(houses.begin() + index);
- }
- }
- else if (command[0] == "Gift") {
- currentIndex = stoi(command[1]);
- if (currentIndex >= 0 && currentIndex < houses.size()) {
- index = currentIndex;
- houses.insert(houses.begin() + index, command[2]);
- }
- }
- else {
- int index1 = getIndex(houses, command[1]),
- index2 = getIndex(houses, command[2]);
- if (index1 != -1 && index2 != -1) {
- string value = houses[index1];
- houses[index1] = houses[index2];
- houses[index2] = value;
- }
- }
- }
- cout << "Position: " << index << endl;
- if (!houses.empty()) {
- for (int i = 0; i < houses.size(); i++) {
- cout << houses[i] << (i != houses.size() - 1 ? ", " : "");
- }
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement