问题描述 此代码旨在解决一个字符串变换问题。给定一个起始字符串 A、一个目标字符串 B 以及一组字符串替换规则,规则的形式为 (from, to),表示可以把字符串里的子串 from 替换成 to。代码要找出从起始字符串 A 变换到目标字符串 B 所需的最少替换次数,若无法完成变换则输出 NO ANSWER!。 解法思路 这里运用广度优先搜索(BFS)算法来解决该问题。BFS 是一种适用于在图或树中搜索最短路径的算法,在本题中,每个字符串状态可看作图中的一个节点,每次替换操作就是节点之间的一条边。 具体步骤
-
定义状态结构体 cpp struct State { string str; int steps; State(string s, int st) : str(s), steps(st) {} }; State 结构体用来存储当前的字符串状态以及从起始字符串到该状态所经历的替换次数。
-
实现字符串替换函数 cpp string replace(const string& s, const string& from, const string& to, int pos) { return s.substr(0, pos) + to + s.substr(pos + from.length()); } replace 函数会在字符串 s 的指定位置 pos 处,把 from 子串替换成 to 子串,最后返回替换后的新字符串。
-
实现广度优先搜索函数 cpp int bfs(const string& start, const string& target, const vector<pair<string, string>>& rules) { queue<State> q; unordered_set<string> visited;
q.push(State(start, 0)); visited.insert(start);
while (!q.empty()) { State current = q.front(); q.pop();
C++ if (current.str == target) { return current.steps; } for (const auto& rule : rules) { string from = rule.first; string to = rule.second; int pos = 0; while ((pos = current.str.find(from, pos)) != string::npos) { string next = replace(current.str, from, to, pos); if (visited.find(next) == visited.end()) { q.push(State(next, current.steps + 1)); visited.insert(next); } pos++; } }}
return -1; } 初始化: queue<State> q:用于存储待处理的状态。 unordered_set<string> visited:用于记录已经访问过的字符串状态,防止重复处理。 把起始状态 (start, 0) 加入队列,并标记为已访问。 循环处理队列元素: 从队列头部取出一个状态 current。 若 current.str 等于目标字符串 target,则返回 current.steps。 针对每一条替换规则 (from, to),在 current.str 里找出所有 from 子串的位置,对每个位置进行替换操作得到新字符串 next。 若 next 未被访问过,将 (next, current.steps + 1) 加入队列,并标记为已访问。 若队列为空仍未找到目标字符串:返回 -1 表示无法完成变换。
-
主函数 cpp int main() { string A, B; cin >> A >> B;
vector<pair<string, string>> rules; string from, to; while (cin >> from >> to) { rules.emplace_back(from, to); }
int result = bfs(A, B, rules); if (result == -1) { cout << “NO ANSWER!” << endl; } else { cout << result << endl; }
return 0; } 从标准输入读取起始字符串 A、目标字符串 B 以及所有的替换规则。 调用 bfs 函数进行求解。 根据返回结果输出最少替换次数或者 NO ANSWER!。 复杂度分析 时间复杂度:在最坏的情况下,BFS 会遍历所有可能的字符串状态,每个状态的处理时间为 O(m∗n) ,其中 m 是替换规则的数量, n 是字符串的长度。因此,总的时间复杂度为 O(m∗n∗2 n ) ,这里的 2 n
是可能的字符串状态数量的上限。 空间复杂度:主要的空间开销在于队列和已访问集合,最坏情况下为 O(2 n ) ,因为可能需要存储所有可能的字符串状态。
接下来附上AC代码 #include <iostream> #include <queue> #include <vector> #include <string> #include <unordered_set>
using namespace std;
// 定义一个结构体来存储当前字符串和变换次数 // str 存储当前的字符串状态 // steps 存储从起始字符串到当前字符串所经过的变换次数 struct State { string str; int steps; // 构造函数,用于初始化结构体成员 State(string s, int st) : str(s), steps(st) {} };
// 进行字符串替换 // s 是原始字符串 // from 是要被替换的子字符串 // to 是替换后的子字符串 // pos 是替换操作开始的位置 string replace(const string& s, const string& from, const string& to, int pos) { // 截取从字符串开头到替换位置的部分 // 拼接上替换后的子字符串 // 再拼接上从替换位置之后的部分 return s.substr(0, pos) + to + s.substr(pos + from.length()); }
// 广度优先搜索函数 // start 是起始字符串 // target 是目标字符串 // rules 是一组替换规则,每个规则是一个 pair,first 是要被替换的字符串,second 是替换后的字符串 int bfs(const string& start, const string& target, const vector<pair<string, string>>& rules) { // 用于存储待处理的状态 queue<State> q; // 用于记录已经访问过的字符串状态,避免重复处理 unordered_set<string> visited;
// 将起始状态加入队列
q.push(State(start, 0));
// 标记起始状态已访问
visited.insert(start);
// 当队列不为空时,继续处理
while (!q.empty()) {
// 取出队列头部的状态
State current = q.front();
// 从队列中移除头部元素
q.pop();
// 如果当前字符串等于目标字符串,返回变换次数
if (current.str == target) {
return current.steps;
}
// 尝试所有可能的变换规则
for (const auto& rule : rules) {
// 要被替换的字符串
string from = rule.first;
// 替换后的字符串
string to = rule.second;
// 从字符串开头开始查找要被替换的子字符串
int pos = 0;
// 当找到要被替换的子字符串时
while ((pos = current.str.find(from, pos)) != string::npos) {
// 进行替换操作得到新的字符串
string next = replace(current.str, from, to, pos);
// 如果新字符串未被访问过
if (visited.find(next) == visited.end()) {
// 将新状态加入队列
q.push(State(next, current.steps + 1));
// 标记新字符串已访问
visited.insert(next);
}
// 移动到下一个可能的替换位置
pos++;
}
}
}
// 如果无法从起始字符串变换到目标字符串,返回 -1
return -1;
}
int main() { // 存储起始字符串 string A, B; // 从标准输入读取起始字符串和目标字符串 cin >> A >> B;
// 存储替换规则
vector<pair<string, string>> rules;
// 临时存储要被替换的字符串和替换后的字符串
string from, to;
// 不断从标准输入读取替换规则,直到输入结束
while (cin >> from >> to) {
// 将规则添加到规则向量中
rules.emplace_back(from, to);
}
// 调用广度优先搜索函数进行求解
int result = bfs(A, B, rules);
// 如果无法找到变换方案
if (result == -1) {
// 输出提示信息
cout << "NO ANSWER!" << endl;
} else {
// 输出最少变换次数
cout << result << endl;
}
return 0;
}