// CERC 2012 // Problem E: Word Equations // Brute force solution (forward traversal), O(tp) // Author: Arkadiusz Pawlik #include #include #include #include #include using namespace std; const int MAXK = 1000; const int MAXS = 5; const int MAXT = 5000; struct eq { string s[2]; eq * e[2]; eq(string s0="", string s1="") { s[0] = s0; s[1] = s1; e[0] = e[1] = 0; } int term(string const & p, int j) { int maxi = s[0].length(); int maxj = p.length(); for (int i=0; ibest(p, e[0]->best(p, j)) : term(p, j); } }; int main() { int z; for (cin >> z; z; --z) { map M; string s[3], t, p; int k; for (cin >> k; k; --k) { cin >> s[2] >> s[1] >> s[0]; if (s[0][0]<='Z') cin >> s[1] >> s[1]; M[s[2]] = eq(s[0], s[1]); } for (map::iterator i = M.begin(); i != M.end(); ++i) if (i->second.s[0][0]<='Z') for (int k=0; k<2; ++k) i->second.e[k] = &M[i->second.s[k]]; cin >> t >> p; cout << (M[t].best(p, 0) < p.length() ? "NO" : "YES") << endl; } return 0; }