#include #include #include #include #include #include using namespace std; set W; int w, l; string from, to; map BFS(string start) { map R; set U; vector Q[2]; int row = 0, depth = 0; R[start] = depth; U.insert(start); Q[row].push_back(start); while (!Q[row].empty()) { Q[row ^ 1].clear(); depth++; for (vector::iterator it = Q[row].begin(); it != Q[row].end(); it++) { string q = *it; for (int i = 0; i < l; i++) { for (int j = 'A'; j <= 'Z'; j++) { if (q[i] == j) continue; string q_ = q; q_[i] = j; if (U.find(q_) != U.end()) continue; U.insert(q_); if (W.find(q_) != W.end()) { Q[row ^ 1].push_back(q_); } R[q_] = depth; } } } row ^= 1; } return R; } int main() { cin >> w; cin >> from; cin >> to; l = from.length(); W.insert(from); W.insert(to); for (int i = 2; i < w; i++) { string s; cin >> s; W.insert(s); } map F = BFS(from); map T = BFS(to); string word = "0"; int ans = -1; if (F.find(to) != F.end()) { ans = F[to]; } for (map::iterator it = F.begin(); it != F.end(); it++) { string s = it->first; if (T.find(s) == T.end()) continue; int dist = it->second + T[s]; if (ans == -1 || ans > dist || (ans == dist && word > s)) { word = s; ans = dist; } } cout << word << endl; cout << ans << endl; return 0; }