洛谷 P1032 字串变换
天哪,巨佬 tym 都橙名了……
正文
本题疑似错题,不保证存在靠谱的多项式复杂度的做法。测试数据非常的水,各种做法都可以通过,不代表算法正确。因此本题题目和数据仅供参考。
……
在一个字符串中,我们有若干种变化规则:(其中 是变化规则数)
问最少变几次能把 变成 。
爆搜!爆搜!!爆搜!!!
然后……
第五个数据非常的 lxl ,而第四个数据非常大的!
DFS 不行,转攻 BFS。
但是……
……
考虑加上 vis 数组。
这样,时间复杂度就变成了 O(玄学) 。
这样,就过了。
不过……字符串咋 vis 数组呢啊喂!
az……
办法当然还是有的,那就是——set。
贴上最终代码(请忽略珂朵莉):
#include <bits/stdc++.h>
using namespace std;
int cnt = 0;
string a[10], b[10];
string x, y;
struct node {
string cur;
int cnt;
node(string a, int b) {
cur = a;
cnt = b;
}
};
queue<node> q;
set<string> SSerxhs;
int main() {
cin >> x >> y;
string p, f;
while (cin >> p >> f) {
a[cnt] = p;
b[cnt] = f;
cnt++;
}
q.push(node(x, 0));
while (q.size()) {
node t = q.front();
q.pop();
if (t.cnt > 10) {
puts("NO ANSWER!");
return 0;
} else if (t.cur == y) {
printf("%d", t.cnt);
return 0;
}
if (SSerxhs.count(t.cur)) {
continue;
}
SSerxhs.insert(t.cur);
for (int tp = 0; tp < cnt; tp++) {
for (int i = 0; i + a[tp].size() <= t.cur.size(); i++) {
if (t.cur.substr(i, a[tp].size()) == a[tp]) {
string tmp = t.cur;
tmp.erase(i, a[tp].size());
tmp.insert(i, b[tp]);
q.push(node(tmp, t.cnt + 1));
}
}
}
}
puts("NO ANSWER!");
return 0;
}
拜拜!
分类:
题解
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构