-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path1099.cpp
73 lines (59 loc) · 1.49 KB
/
1099.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
#include <iostream>
#include <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;
}
};
int main()
{
string a, b;
cin >> a >> b;
queue<Word> q;
q.push(Word(a, 0));
map<string, string> dict;
string aa, bb;
while(cin >> aa >> bb) {
dict.insert(make_pair(aa, bb));
}
while(!q.empty()) {
Word cur = q.front();
q.pop();
if(cur.c>9) {
cout << "NO ANSWER!" << endl;
return 0;
}
for(map<string, string>::iterator i=dict.begin(); i!=dict.end(); i++) {
string pp = i->first;
string qq = i->second;
size_t found = 0;
while(found!=string::npos) {
if(found == 0) {
found = cur.str.find(pp);
if(found!=string::npos)
found++;
} else {
found = cur.str.find(pp, found);
}
if(found!=string::npos) {
string word = cur.str.substr(0, found-1) + qq + cur.str.substr(found-1+pp.size());
//cout << word << endl;
if(word.compare(b)==0) {
cout << cur.c+1;
return 0;
}
q.push(Word(word, cur.c+1));
}
}
}
}
return 0;
}