-
Notifications
You must be signed in to change notification settings - Fork 1
/
uva_429.cpp
95 lines (87 loc) · 1.91 KB
/
uva_429.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
#include <bits/stdc++.h>
#define REP(i, a, b) for(int i = a, _b = b; i < _b; ++i)
#define FOR(i, a, b) for(int i = a, _b = b; i <= _b; ++i)
#define endl "\n"
#define puts(_content_) cout << (_content_) << "\n"
#define newline cout << "\n"
using namespace std;
typedef pair<int, int> ii;
const int MAXV = 205;
map<string, int> num;
vector<int> adj[MAXV];
int V;
int bfs (int src, int des) {
queue<ii> que;
vector<bool> visited(V, false);
que.push(ii(src, 0));
while(!que.empty()) {
int u, dept;
tie(u, dept) = que.front();
que.pop();
if(u == des) return dept;
if(visited[u]) continue;
visited[u] = true;
for(int v: adj[u]) {
if(!visited[v]) {
que.push(ii(v, dept+1));
}
}
}
return -1;
}
void parse (string & foo, string & bar, string & line) {
REP(i, 0, line.length()) {
if(line[i] == ' ') {
foo = line.substr(0, i);
bar = line.substr(i+1);
break;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
#ifndef Home
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int N;
cin >> N;
string line;
while(N--) {
string word;
V = 0;
while(1) {
cin >> word;
if(word[0] == '*') break;
num[word] = V++;
}
for(auto p: num) {
string word = p.first;
int id = p.second;
REP(i, 0, word.length()) {
char tmp = word[i];
FOR(c, 'a', 'z') {
if(c == word[i]) continue;
word[i] = c;
if(num.find(word) != num.end()) {
adj[id].push_back(num[word]);
}
word[i] = tmp;
}
}
}
string foo, bar;
cin.ignore();
while(getline(cin, line)) {
if(line.length() == 0) break;
parse(foo, bar, line);
int answer = bfs(num[foo], num[bar]);
cout << foo << " " << bar << " " << answer << endl;
}
if(N != 0) newline;
num.clear();
REP(v, 0, V) adj[v].clear();
}
return 0;
}