80分求助!5#WA
查看原帖
80分求助!5#WA
674147
vanueber楼主2022/8/15 11:33
#include <iostream>
#include <string>
#include <queue>

using namespace std;

struct rule
{
    string a, b;
    int len;
} r[10];

string x, y, t;
queue<string> p;
queue<int> step;
int i = 1, pos, tmp;

int main()
{
    cin >> x >> y;
    while (cin >> r[i].a)
    {
        cin >> r[i].b;
        r[i].len = r[i].a.length();
        i++;
    }
    p.push(x);
    step.push(0);
    while (!p.empty())
    {
        if (step.front() > 10)
        {
            p.pop();
            step.pop();
            continue;
        }
        if (p.front() == y)
        {
            cout << step.front();
            return 0;
        }
        for (int k = 1; k < i; k++)
        {
            t = p.front();
            pos = t.find(r[k].a);
            tmp = 0;
            while (pos != string::npos)
            {
                tmp++;
                t.replace(pos, r[k].len, r[k].b);
                p.push(t);
                step.push(step.front() + tmp);
                pos = t.find(r[k].a, pos + 1);
            }
        }
        p.pop();
        step.pop();
    }
    cout << "NO ANSWER!";
    return 0;
}

在线等,急!

2022/8/15 11:33
加载中...