-
Notifications
You must be signed in to change notification settings - Fork 0
/
8905.cpp
59 lines (53 loc) · 1.08 KB
/
8905.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
#include <iostream>
#include <string>
#include <map>
#include <string.h>
using namespace std;
int T, k;
string A, B;
int lenA, lenB;
map <char, string> mapping;
bool alphabet[26];
bool realans = true;
void init() {
A.clear(); B.clear();
memset(alphabet, 0, sizeof(alphabet));
mapping.clear();
}
bool backtr(int i1, int i2) {
if (i1 == lenA && i2 == lenB) return true;
if (i2 >= lenB) return false;
bool ans = true, tmp = true;
char ch = A[i1];
if (!alphabet[ch - 'a']) {
alphabet[ch - 'a'] = true;
for (int i = 1; i <= k; ++i) {
mapping[ch] = B.substr(i2, i);
tmp = backtr(i1 + 1, i2 + i);
if (tmp == true) break;
}
alphabet[ch - 'a'] = false;
ans = tmp;
}
else {
string mapped = mapping[ch];
if (B.substr(i2, mapped.size()) == mapped)
return backtr(i1 + 1, i2 + mapped.size());
else {
//alphabet[ch-'a'] = false;
return false;
}
}
return ans;
}
int main() {
for (scanf("%d", &T); T--;) {
scanf("%d", &k);
cin >> A >> B;
lenA = A.size(), lenB = B.size();
realans = backtr(0, 0);
if (!realans) printf("0\n");
else printf("1\n");
}
return 0;
}