Monday, September 14, 2015

UVa 10701 - Pre, in and post

// UVa 10701 - Pre, in and post
#include <iostream>
#include <map>
#include <string>
using namespace std;

map<char, int> preorder;
string s1, s2, sol;

void build(int ini, int fin) {
	if (ini <= fin) {
		int root = ini;
		for (int k = ini + 1; k <= fin; k++)
			if (preorder[s2[k]] < preorder[s2[root]])
				root = k;
		build(ini, root - 1);
		build(root + 1, fin);
		sol += s2[root];
	}
}

int main() {
	int cases;
	cin >> cases;
	for (; cases > 0; cases--) {
		preorder.clear();
		int n;
		cin >> n >> s1 >> s2;
		for (int i = 0; i < n; i++)
			preorder[s1[i]] = i;

		sol = "";
		build(0, n - 1);
		cout << sol << endl;
	}
	return 0;
}

No comments:

Post a Comment