#include <bits/stdc++.h>
using namespace std;
string a, b, huan[8][2];
int n = 1;
struct node {
string str;
int num;
};
int bfs() {
queue<node> q;
node now, neww;
now.num = 0;
now.str = a;
int x;
string y, z;
q.push(now);
while (!q.empty()) {
now = q.front();
if (now.str == b || now.num > 10) {
break;
}
q.pop();
for (int i = 1; i <= n; i++) {
z = now.str;
while (1) {
y = now.str;
x = z.find(huan[i][0]);
if (x == -1) {
break;
}
y.replace(x, huan[i][0].size(), huan[i][1]);
neww.num = now.num + 1;
neww.str = y;
q.push(neww);
z[x] = '~';
}
}
}
if (now.num <= 10 && !q.empty()) {
return now.num;
}
return -1;
}
int main() {
cin >> a >> b;
while (cin >> huan[n][0] >> huan[n][1]) {
n++;
}
n--;
if (n == 0 && a != b) {
cout << "NO ANSWER!";
return 0;
}
if (a == b) {
cout << 0;
return 0;
}
int x = bfs();
if (x >= 0) {
cout << x;
} else {
cout << "NO ANSWER!";
}
return 0;
}
应该是“爆队列长度”了吧......