-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuva4637.cpp
111 lines (88 loc) · 1.75 KB
/
uva4637.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <limits.h>
#include <list>
#include <queue>
#include <map>
using namespace std;
vector<pair<string ,string> > substitute;
map<string, int> dist; /* first map :D */
string alfa, gamma;
string sed(string s1, string s2, string s3)
{
vector<int> index;
int curent_index;
int pos = 0;
while(s1.find(s2, pos) != string::npos)
{
curent_index = s1.find(s2, pos);
index.push_back(curent_index);
pos = curent_index + s2.length();
}
int count = 0;
for(int i = 0; i < index.size(); i++)
{
s1.replace(index[i] + count*(s3.length() - s2.length()), s2.length(), s3);
count++;
}
return s1;
}
void bfs(string curent)
{
queue<string> q;
dist[curent] = 0;
q.push(curent);
while(!q.empty() && q.front() != gamma)
{
string t = q.front();
q.pop();
vector<pair<string ,string> >::iterator v;
for(v = substitute.begin(); v != substitute.end(); v++)
{
string s1 = v->first;
string s2 = v->second;
string next = sed(t, s1, s2);
if(next != t && next.length() <= gamma.length())
{
//cout << next << endl;
if(!dist.count(next))
{
dist[next] = dist[t] + 1;
q.push(next);
//cout << next << endl;
}
}
}
}
}
int main()
{
int N;
while(true)
{
cin >> N;
if(N == 0)
break;
substitute.clear();
dist.clear();
substitute.resize(N);
for(int i = 0; i < N; i++)
cin >> substitute[i].first >> substitute[i].second;
cin >> alfa;
cin >> gamma;
bfs(alfa);
if(!dist.count(gamma))
cout << "-1" << endl;
else
cout << dist[gamma] << endl;
/*string s1, s2, s3;
cin >> s1 >> s2 >> s3;
cout << sed(s1, s2, s3) << endl; */
}
return 0;
}