Thursday, May 14, 2015

UVa 10728 - Help!

// UVa 10728 - Help!

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <sstream>
using namespace std;

vector<string> a, b;
map<string, bool> taken;
map<string, string> value;
int n;

void add(const string & var, const string & val) {
	value[var] = val;
	taken[var] = true;
}

bool isgood() {
	value.clear();
	taken.clear();
	bool new_rules;
	do {
		new_rules = false;
		for (int i = 0; i < n; i++) {

			// force rules
			if (a[i][0] == '<' && taken["a" + a[i]])
				a[i] = value["a" + a[i]];
			if (b[i][0] == '<' && taken["b" + b[i]])
				b[i] = value["b" + b[i]];

			// find new rules
			if (a[i][0] == '<' && b[i][0] != '<') {
				add("a" + a[i], b[i]);
				a[i] = b[i];
				new_rules = true;
			} else if (b[i][0] == '<' && a[i][0] != '<') {
				add("b" + b[i], a[i]);
				b[i] = a[i];
				new_rules = true;
			}
		}
	} while (new_rules);

	// compare
	for (int i = 0; i < n; i++)
		if (a[i][0] != '<' && b[i][0] != '<' && a[i] != b[i])
			return false;
	return true;
}

int main() {
	int t;
	cin >> t;
	string line;
	getline(cin, line);
	for (; t; t--) {
		a.clear();
		b.clear();
		getline(cin, line);
		istringstream strm1(line);
		string s;
		while (strm1 >> s)
			a.push_back(s);

		getline(cin, line);
		istringstream strm2(line);
		while (strm2 >> s)
			b.push_back(s);

		bool good = true;
		n = a.size();
		if (n != b.size())
			good = false;
		else
			good = isgood();

		// print
		if (good) {
			for (int i = 0; i < n; i++)
				if (a[i][0] == '<')
					a[i] = "daniel";
			if (n > 0)
				cout << a[0];
			for (int i = 1; i < n; i++)
				cout << " " << a[i];
			cout << endl;
		} else
			cout << "-" << endl;
	}
	return 0;
}

No comments:

Post a Comment