题目描述
题目要求计算从起始单词变换到目标单词的最短步数。每次变换只能改变单词中的一个字母,且变换后的单词必须存在于给定的字典中。所有单词长度相同。对于每对起始词和目标词,输出最短变换步数。
输入格式
第一行一个整数 NNN,表示测试用例的数量,后面跟一个空行。
每个测试用例包含两个部分:
- 字典部分:若干行,每行一个单词(小写字母,长度不超过 101010),以一行单独的
*结束。字典中最多 200200200 个单词。 - 查询部分:若干行,每行两个单词(起始词和目标词),用一个空格分隔。每对单词均保证存在变换路径。输入以文件结束符(EOF\texttt{EOF}EOF)终止。
两个连续的测试用例之间由一个空行分隔。
输出格式
对于每个查询,输出一行,包含起始词、目标词和最短步数,之间用单个空格分隔。两个连续的测试用例输出之间由一个空行分隔。
样例
输入
1
dip
lip
mad
map
maple
may
pad
pip
pod
pop
sap
sip
slice
slick
spice
stick
stock
*
spice stock
may pod
输出
spice stock 4
may pod 3
题目分析
本题的核心是在单词图中求两点之间的最短路径。每个单词是一个节点,若两个单词长度相同且仅有一个字母不同,则它们之间有一条无向边。
图的构建
由于字典中最多 200200200 个单词,可以枚举所有单词对,检查它们是否长度相同且恰好有一个字符不同。若是,则在两者之间建立一条边。构建图的时间复杂度为 O(n2×L)O(n^2 \times L)O(n2×L),n≤200n \le 200n≤200,L≤10L \le 10L≤10,完全可接受。
最短路径计算
对于每个查询(起始词 sss 和目标词 ttt),使用广度优先搜索(BFS\texttt{BFS}BFS)计算从 sss 到 ttt 的最短路径长度。由于所有边权均为 111,BFS\texttt{BFS}BFS 可以保证找到最短路径。
实现细节
- 字典中单词可能重复,需去重。
- 需要为每个单词分配一个整数索引,以便在图中使用。
- BFS\texttt{BFS}BFS 时记录每个节点的距离,初始距离 dist[s]=0\textit{dist}[s] = 0dist[s]=0,队列中存放节点编号。
- 由于每组查询独立,可以在每次查询时重新运行 BFS\texttt{BFS}BFS。
复杂度分析
- 图构建:O(n2×L)O(n^2 \times L)O(n2×L),n≤200n \le 200n≤200,L≤10L \le 10L≤10,约 4×1054 \times 10^54×105 次操作。
- 每次查询 BFS\texttt{BFS}BFS:O(n+m)O(n + m)O(n+m),其中 mmm 为边数,最多 O(n2)O(n^2)O(n2)。
- 总复杂度可接受。
代码实现
// Word Transformation
// UVa ID: 429
// Verdict: Accepted
// Submission Date: 2016-07-17
// UVa Run Time: 0.030s
//
// 版权所有(C)2016,邱秋。metaphysis # yeah dot net
#include <bits/stdc++.h>
using namespace std;
const int MAX_STEPS = 1000000;
int main(int argc, char *argv[])
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
string line;
getline(cin, line);
int cases = stoi(line);
getline(cin, line);
for (int m = 1; m <= cases; m++)
{
if (m > 1) cout << endl;
set<string> existed;
map<string, int> words;
vector<string> dictionary;
int n = 0;
while (getline(cin, line), line != "*")
{
if (existed.find(line) == existed.end())
{
existed.insert(line);
dictionary.push_back(line);
words[line] = n++;
}
}
vector<vector<int>> edges(n, vector<int>());
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
{
if (dictionary[i].length() != dictionary[j].length()) continue;
int difference = 0;
for (int k = 0; k < dictionary[i].length(); k++)
if (dictionary[i][k] != dictionary[j][k])
difference++;
if (difference == 1)
{
edges[i].push_back(j);
edges[j].push_back(i);
}
}
while (getline(cin, line), line.length() > 0)
{
istringstream iss(line);
string first, second;
iss >> first >> second;
int start = words[first], end = words[second];
vector<bool> visited(n);
vector<int> distances(n);
fill(distances.begin(), distances.end(), MAX_STEPS);
fill(visited.begin(), visited.end(), false);
distances[start] = 0;
int current = start;
while (visited[current] == false)
{
visited[current] = true;
for (int i = 0; i < edges[current].size(); i++)
{
int next = edges[current][i];
if (visited[next] == false && distances[next] > distances[current] + 1)
{
distances[next] = distances[current] + 1;
}
}
int max_distances = MAX_STEPS;
for (int i = 0; i < n; i++)
if (visited[i] == false && distances[i] < max_distances)
{
current = i;
max_distances = distances[i];
}
}
cout << first << ' ' << second << ' ' << distances[end] << '\n';
}
}
return 0;
}
407



被折叠的 条评论
为什么被折叠?



