-
Notifications
You must be signed in to change notification settings - Fork 0
/
10150 - Doublets.cpp
112 lines (97 loc) · 1.8 KB
/
10150 - Doublets.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 <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
using namespace std;
vector<string> dictionary[30];
map <string, vector<string> > adj;
bool are_doublets(string a, string b)
{
int len = a.length();
int c = 0;
for(int i = 0; i < len; i++) {
if(a[i] != b[i]) c++;
}
return c == 1;
}
void compute(vector<string> words)
{
int sz = words.size();
for(int i = 0; i<sz; i++) {
for(int j = i+1; j<sz; j++) {
if(are_doublets(words[i], words[j])) {
adj[words[i]].push_back(words[j]);
adj[words[j]].push_back(words[i]);
}
}
}
}
bool solved(string s, string t, vector<string> &ret)
{
map<string, string> parent;
set<string> seen;
bool found = false;
seen.insert(s);
queue<string> Q;
parent[s] = "";
Q.push(s);
while(!Q.empty()) {
string u = Q.front();
Q.pop();
int sz = adj[u].size();
for(int i = 0; i<sz; i++) {
string v = adj[u][i];
if(seen.find(v) == seen.end()) {
parent[v] = u;
seen.insert(v);
Q.push(v);
if(v == t) {
found = true;
break;
}
}
}
}
if(found) {
while(1) {
ret.push_back(t);
if(t == s) break;
t = parent[t];
}
}
return found;
}
int main()
{
string word;
bool alreadySolved[30] = {false};
while(getline(cin, word)) {
if(word == "") break;
dictionary[word.length()].push_back(word);
}
string a, b;
bool enter = false;
while(cin >> a >> b) {
if(enter) puts("");
enter = true;
int len = a.length();
if(len != b.length()) puts("No solution.");
else {
vector<string> ret;
if(!alreadySolved[len]) {
alreadySolved[len] = true;
compute(dictionary[len]);
}
if(solved(a, b, ret)) {
for(int i = ret.size()-1; i>=0; i--) {
cout << ret[i] << endl;
}
}
else puts("No solution.");
}
}
return 0;
}