Rezultati

Up. imeNalogaJezikRezultatČas oddaje
rektifikatorji-2018 Prehitevanje C++ 100/100OK 04. okt '18 @ 17:27

Test Točke Porabljen spomin Porabljen čas Status
#1 16/16 3,047 MiB 0,004 s OK
#2 16/16 3,195 MiB 0,010 s OK
#3 17/17 3,195 MiB 0,000 s OK
#4 17/17 4,797 MiB 0,696 s OK
#5 17/17 35,238 MiB 0,000 s OK
#6 17/17 3,035 MiB 0,004 s OK

Ocenjevani program (Source.cpp):
#include <iostream>
#include <string>
#include <algorithm>
#include <map>

using namespace std;

struct Node
{
	string name;
	int prev;
	int next;
};

Node* list;
map<string, int> lookup;


int main()
{
	int list_len;
	cin >> list_len;

	list = new Node[list_len];

	// init
	int head = 0;
	for (int i = 0; i != list_len; i++)
	{
		string name;
		cin >> name;
		lookup[name] = i;

		Node* node = new Node();
		node->name = name;
		node->prev = i - 1;
		if (i == list_len - 1)
			node->next = -1;
		else
			node->next = i + 1;

		list[i] = *node;
	}

	// komentator
	int P;
	cin >> P;
	string x, y;
	for (int i = 0; i != P; i++)
	{
		cin >> x >> y;

		int x_index = lookup[x];
		int y_index = lookup[y];

		if (list[x_index].prev != -1)
			list[list[x_index].prev].next = list[x_index].next;
		else
			head = list[x_index].next;
		if (list[x_index].next != -1)
			list[list[x_index].next].prev = list[x_index].prev;

		list[x_index].next = y_index;
		list[x_index].prev = list[y_index].prev;

		if (list[y_index].prev != -1)
			list[list[y_index].prev].next = x_index;
		list[y_index].prev = x_index;

		if (list[x_index].prev == -1)
			head = x_index;
	}

	int curr = head;
	while (curr != -1)
	{
		cout << list[curr].name << endl;
		curr = list[curr].next;
	}
}