-
Notifications
You must be signed in to change notification settings - Fork 0
/
P1032.cpp
82 lines (81 loc) · 1.75 KB
/
P1032.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <bits/stdc++.h>
using namespace std;
struct rule
{
string origin, change;
};
struct rule rules[7];
string a, b;
int len;
string copyString(string a, int start, int end)
{
string result;
for (int i = start; i < end; i++)
{
result.push_back(a[i]);
}
return result;
}
int min_step = 1 << 30;
void dfs(string n, int s)
{
if (s > min_step || s > 10)
{
return;
}
if (n == b)
{
min_step = min(min_step, s);
return;
}
for (int k = 0; k < len; k++)
{
string right, left;
for (int i = 0; i < n.length(); i++)
{
bool flag = true;
for (int j = 0; j < rules[k].origin.length(); j++)
{
if (i + j >= n.length()||n[i + j] != rules[k].origin[j])
{
flag = false;
break;
}
}
if (flag)
{
for (int j = i + rules[k].origin.length(); j < n.length(); j++)
{
left.push_back(n[j]);
}
dfs(right + rules[k].change + left, s + 1);
}
right.push_back(n[i]);
}
}
}
int main()
{
cin >> a >> b;
getchar();
string temp;
while(cin>>rules[len].origin>>rules[len].change){
len++;
}
// while (getline(cin, temp) && temp != "")
// {
// int end = temp.find(' ');
// rules[len].origin = copyString(temp, 0, end);
// end = temp.rfind(' ');
// rules[len].change = copyString(temp, end + 1, temp.length());
// len++;
// }
dfs(a, 0);
if (min_step > 10)
{
cout << "NO ANSWER!";
}
else
cout << min_step;
return 0;
}