-
Notifications
You must be signed in to change notification settings - Fork 3
/
wordladder2.cpp
112 lines (105 loc) · 2.42 KB
/
wordladder2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <bits/stdc++.h>
using namespace std;
using vs = vector<string>;
using vi = vector<int>;
using v2i = vector<vi>;
using ti2 = tuple<int, int>;
using ti3 = tuple<int, int, int>;
using vti2 = vector<ti2>;
using vti3 = vector<ti3>;
int N, INF = 1e9;
vs words;
v2i D;
int word_dist(int a, int b) {
int res = 0;
for (size_t i = 0; i < words[a].size(); ++i) {
if (words[a][i] != words[b][i]) ++res;
}
return res;
}
vs diffs(int a, int b) {
vs res;
vi pos;
for (size_t i = 0; i < words[a].size(); ++i) {
if (words[a][i] != words[b][i]) pos.push_back(i);
}
string A, B = words[b];
A = words[a];
A[pos[0]] = B[pos[0]];
res.push_back(A);
A = words[a];
A[pos[1]] = B[pos[1]];
res.push_back(A);
return res;
}
string solve(vti2 &pairs) {
int a, b;
string res;
for (auto &p : pairs) {
tie(a, b) = p;
vs R = diffs(a, b);
if (res.empty()) res = R[0];
res = min(res, R[0]);
res = min(res, R[1]);
}
return res;
}
vti2 trace(v2i &dist, int cur) {
vti2 res;
queue<int> Q;
Q.push(cur);
while (!Q.empty()) {
cur = Q.front(); Q.pop();
for (int i = 0; i < N; ++i) {
int d = D[i][cur];
if (d > 2) continue;
if (d == 2 && dist[i][0] + d == dist[cur][1]) {
res.emplace_back(i, cur);
}
if (d == 1 && dist[i][1] + d == dist[cur][1]) {
Q.push(i);
}
}
}
return res;
}
int dijkstra(int src, int dest, vti2 &pairs) {
int cur, nxt, k, nk, s, ns, d;
v2i dist(N, vi(2, INF));
priority_queue<ti3, vti3, greater<ti3>> Q;
Q.emplace(0, 0, src);
dist[src][0] = 0;
while (!Q.empty()) {
tie(s, k, cur) = Q.top(); Q.pop();
for (nxt = 0; nxt < N; ++nxt) {
d = D[cur][nxt];
nk = d == 2;
if (d > 2 || (nk && k)) continue;
if (k) nk = 1;
if (s + d < dist[nxt][nk]) {
dist[nxt][nk] = s + d;
Q.emplace(s + d, d == 2 ? 1 : k, nxt);
}
}
}
if (dist[dest][0] <= dist[dest][1]) return dist[dest][0];
pairs = trace(dist, dest);
return dist[dest][1];
}
int main() {
cin >> N;
words.resize(N);
D.assign(N, vi(N, INF));
for (auto &word : words) cin >> word;
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j < N; ++j) {
D[i][j] = D[j][i] = word_dist(i, j);
}
}
vti2 pairs;
int steps = dijkstra(0, 1, pairs);
if (steps == -1 || pairs.empty()) cout << 0 << endl;
else cout << solve(pairs) << endl;
cout << (steps == INF ? -1 : steps) << endl;
return 0;
}