-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8047.cpp
69 lines (65 loc) · 1.58 KB
/
8047.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
#include <bits/stdc++.h>
using namespace std;
int m, n, j;
map<char, vector<char> > map_test;
int visitado[26];
bool DFS(char a, char b, bool &find){
j = int(a - 'a');
visitado[j] = 1;
if (a == b){
find = true;
}
else{
vector<char> vecinos = map_test[a];
for (int i = 0; i < vecinos.size(); i++){
if (b == vecinos[i]){
find = true;
}
else{
int k = int(vecinos[i] - 'a');
if (!visitado[k])
DFS(vecinos[i], b, find);
}
}
}
if (find)
return true;
else{
return false;
}
}
int main()
{
cin >> m >> n;
while (m--){
char a, b;
cin >> a >> b;
map_test[a].push_back(b);
}
while (n--){
string word_1, word_2;
cin >> word_1 >> word_2;
if (word_1.length() != word_2.length())
cout << "no" << endl;
else{
bool found = true;
for (int i = 0; i < word_1.length(); i++){
for (int i = 0; i < 26; i++)
visitado[i] = 0;
char x = word_1[i];
char y = word_2[i];
j = int(word_1[i] - 'a');
if (!visitado[j]){
visitado[j] = 1;
bool find = false;
if (!DFS(x, y, find)){
found = false;
break;
}
}
}
printf(found ? "yes\n" : "no\n");
}
}
return 0;
}