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
| #include <cstdio> #include <cstring> #include <algorithm> using namespace std;
const int maxn = 20; int k, l1, l2; char Map[27][4], tmp[4]; char s[maxn], lt[maxn * 3];
bool dfs(int p1, int p2) { if (p1 == l1 || p2 == l2) { return p1 == l1 && p2 == l2; } for (int i = 1; i <= k && p2 + i <= l2; ++i) { for (int j = 0; j < i; ++j) { tmp[j] = lt[p2 + j]; } tmp[i] = 0; bool flag = Map[s[p1] - 'a'][0] == 0; if (flag) { strcpy(Map[s[p1] - 'a'], tmp); } if (!strcmp(Map[s[p1] - 'a'], tmp) && dfs(p1 + 1, p2 + i)) { return true; } if (flag) { Map[s[p1] - 'a'][0] = 0; } } return false; }
int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%s%s", &k, s, lt); memset(Map, 0, sizeof Map); l1 = int(strlen(s)), l2 = int(strlen(lt)); puts(dfs(0, 0) ? "1" : "0"); } return 0; }
|