社区讨论

80分求调

P1032[NOIP 2002 提高组] 字串变换(疑似错题)参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@m4kuupul
此快照首次捕获于
2024/12/12 13:04
去年
此快照最后确认于
2024/12/12 18:38
去年
查看原帖

code:

CPP
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <queue>
using namespace std;
#define max(l, r) ((l) < (r) ? (r) : (l))
#define min(l, r) ((l) > (r) ? (r) : (l))
struct String { string x, y; };
struct strings { string s; int stp = 0; };
static void solve() {
    string n, m, a, b;
    cin >> n >> m;
    String x;
    vector<String>rule;
    while (cin >> a >> b) x.x = a, x.y = b, rule.push_back(x);
    unordered_map<string, bool>usd;
    queue<strings>q;
    strings X;
    X.s = n;
    q.push(X);
    usd[n] = true;
    while (!q.empty()) {
        strings tmp = q.front(), move;
        q.pop();
        for (auto& k : rule) {
            move = tmp;
            if (move.s.find_first_of(k.x)!=string::npos)move.s.replace(move.s.find_first_of(k.x), k.x.size(), k.y);
            if (usd[move.s] == true) continue;
             if (move.s == m) {
                cout << ((tmp.stp + 1) <= 10 ? to_string(tmp.stp + 1): "NO ANSWER!") << '\n';
                return;
            }
            move.stp = tmp.stp + 1;
            if ((tmp.stp + 1) <= 10)q.push(move);
            usd[move.s] = true;
        }
    }
    cout << "NO ANSWER!\n";
    return;
}
int main() {
    ios::sync_with_stdio(0);
    cout.tie(0);
    cin.tie(0);
    solve();
    return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...