-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path1099_cpp11.cpp
91 lines (79 loc) · 1.85 KB
/
1099_cpp11.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
83
84
85
86
87
88
89
90
#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
#include <queue>
using namespace std;
struct Word
{
string str;
int c;
Word(string s, int cc) {
str=s;
c=cc;
}
};
vector<string> replace(string &src, string &p, string &q) {
size_t i=0, j=0;
vector<string> res;
while(i<src.size()) {
size_t ii=i;
while(ii<src.size() && j<p.size() && src[ii]==p[j]) {
ii++;
j++;
}
if(j==p.size()) {
string replaced = src.substr(0, i) + q + src.substr(i+p.size());
res.push_back(replaced);
}
i++;
j=0;
}
return res;
}
int main()
{
// string src = "fuuuuuck";
// string pp = "uu";
// string qq = "jpu";
// vector<string> list = replace(src, pp, qq);
// for(string str:list) {
// cout << str << endl;
// }
// system("PAUSE");
// return 0;
string a, b;
cin >> a >> b;
queue<Word> q;
q.push(Word(a, 0));
unordered_map<string, string> dict;
while(!cin.eof()) {
string aa, bb;
cin >> aa >> bb;
dict.emplace(aa, bb);
}
while(!q.empty()) {
Word cur = q.front();
q.pop();
if(cur.c>9) {
cout << "NO ANSWER!" << endl;
return 0;
}
for(auto i=dict.begin(); i!=dict.end(); i++) {
string pp = i->first;
string qq = i->second;
vector<string> candidate = replace(cur.str, pp, qq);
if(candidate.size()>0) {
for(auto word:candidate) {
if(word.compare(b)==0) {
cout << cur.c+1;
return 0;
}
q.push(Word(word, cur.c+1));
}
}
}
}
system("PAUSE");
return 0;
}