#include #include #include #include #include #include using namespace std; char buf[1000000]; struct Pos { // vrchol grafu int tp; // počet zpracovaných znaků z R int nod; int sp; // počet zpracovaných znaků z aktuálního fragmentu Pos() {} Pos(int a, int b, int c) : tp(a), nod(b), sp(c) {} bool operator==(const Pos &b) const { return tp == b.tp && nod == b.nod && sp == b.sp; } }; namespace std { template<> struct hash { size_t operator()(const Pos& a) const { return hash()(a.tp) ^ (hash()(a.nod) << 3) ^ (hash()(a.sp) << 5) ^ (hash()(a.tp) >> 2) ^ (hash()(a.nod) >> 1) ^ hash()(a.sp); } }; } int main() { int n, m; scanf("%d %d ", &n, &m); vector nodes; for (int i = 0; i < n; i++) { scanf("%s", buf); nodes.push_back(string(buf)); } vector> g(n); for (int i = 0; i < m; i++) { int a, b; scanf("%d %d ", &a, &b); a--; b--; g[a].push_back(b); } int d, start, end; scanf("%d %d %d ", &d, &start, &end); start--; end--; scanf("%s", buf); string target(buf); deque> fr; unordered_map back; back.reserve(target.size()*5*d); fr.push_back(make_pair(Pos(0, start, 0), 0)); unordered_map dists; dists.reserve(target.size()*5*d); while (!fr.empty()) { Pos x = fr.front().first; int dd = fr.front().second; fr.pop_front(); if (dd > d) continue; if (dd > dists[x]) continue; if (x.tp == target.size() && x.nod == end && x.sp == nodes[x.nod].size()) { vector path; Pos cp = x; path.push_back(cp.nod); while (back.count(cp)) { Pos prev = back[cp]; if (prev.nod != cp.nod) { path.push_back(prev.nod); } cp = prev; } reverse(path.begin(), path.end()); for (int i = 0; i < path.size(); i++) { printf("%d%c", path[i] + 1, i + 1 == path.size() ? '\n' : ' '); } return 0; } if (x.sp < nodes[x.nod].size()) { if (x.tp < target.size()) { if (nodes[x.nod][x.sp] == target[x.tp]) { Pos nx(x.tp+1, x.nod, x.sp+1); if (dists.count(nx) == 0 || dists[nx] > dd) { back[nx] = x; fr.push_front(make_pair(nx, dd)); dists[nx] = dd; } } else { Pos nx(x.tp+1, x.nod, x.sp+1); if (dists.count(nx) == 0) { back[nx] = x; fr.push_back(make_pair(nx, dd+1)); dists[nx] = dd+1; } } } Pos nx(x.tp, x.nod, x.sp+1); if (dists.count(nx) == 0) { back[nx] = x; fr.push_back(make_pair(nx, dd+1)); dists[nx] = dd+1; } } else { for (int i = 0; i < g[x.nod].size(); i++) { int nnod = g[x.nod][i]; Pos nx(x.tp, nnod, 0); if (dists.count(nx) == 0 || dists[nx] > dd) { back[nx] = x; fr.push_front(make_pair(nx, dd)); dists[nx] = dd; } } } if (x.tp < target.size()) { Pos nx(x.tp+1, x.nod, x.sp); if (dists.count(nx) == 0) { back[nx] = x; fr.push_back(make_pair(nx, dd+1)); dists[nx] = dd+1; } } } printf("-1\n"); return 0; }