Rezultati

Up. imeNalogaJezikRezultatČas oddaje
undefined-2017 Prijateljske besede C++ 100/100OK 20. apr '17 @ 17:30

Test Točke Porabljen spomin Porabljen čas Status
#1 12/12 3,137 MiB 0,004 s OK
#2 12/12 3,195 MiB 0,010 s OK
#3 12/12 3,445 MiB 0,924 s OK
#4 12/12 3,363 MiB 1,173 s OK
#5 13/13 3,309 MiB 0,041 s OK
#6 13/13 3,281 MiB 0,704 s OK
#7 13/13 3,254 MiB 0,016 s OK
#8 13/13 3,309 MiB 0,662 s OK

Ocenjevani program (Source.cpp):
#include <iostream>
#include <string>
#include <unordered_set>
#include <stack>
#include <vector>

bool areCompanions(std::string a, std::string b) {
	return (a.substr(0, 3) == b.substr(0, 3)) || (a.substr(a.length() - 3) == b.substr(b.length() - 3));
}

int main() {
	//freopen("C:\\Users\\matej.drobnic\\Desktop\\test.txt", "r", stdin);

	std::string line;
	int n;

	std::getline(std::cin, line);
	n = stoi(line);

	std::getline(std::cin, line);
	std::string firstWord = line;

	std::unordered_set<int> processedStrings;

	std::stack<std::string> stringsToProcess;

	std::vector<std::string> words;

	int friendlyCount = 1;

	for (int i = 1; i < n; i++) {
		std::getline(std::cin, line);
		words.push_back(line);
	}

	stringsToProcess.push(firstWord);


	while (!stringsToProcess.empty()) {
		std::string next = stringsToProcess.top();
		stringsToProcess.pop();


		for (int i = 0; i < words.size(); i++) {
			std::string other = words[i];

			if (processedStrings.find(i) != processedStrings.end()) {
				continue;
			}

			if (areCompanions(next, other)) {
				friendlyCount++;
				processedStrings.insert(i);
				stringsToProcess.push(other);
			}
		}
	}

	std::cout << friendlyCount << std::endl;
}