/C06033 - Trộn xâu

<Problem>

https://code.ptit.edu.vn/student/question/C06033
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define max(i, j) ((i > j) ? i : j)

void Union(char res[], char s1[], char s2[], int n) {
	int i = 0;
	int j = 0;
	int lr = 0;

	while (lr < 2 * n - 1) {
		res[lr++] = s2[j++];
		res[lr++] = s1[i++];
	}
	res[2 * n] = '\0';
}

void Reload(char s1[], char s2[], char res[], int n) {
	for (int i = 0; i < n; i++) {
		s1[i] = res[i];
		s2[i] = res[i + n];
	}
}

int main() {
	int n;

	while (scanf("%d", &n) != -1) {
		if (n == 0) break;

		getchar();

		char s1[200];
		char s2[200];
		char s[400];

		gets(s1);
		gets(s2);
		gets(s);

		bool check = true;
		int cnt = 0;

		char firstAtm[400];
		char res[400];
		Union(res, s1, s2, n);

		strcpy(firstAtm, res);

		while (true) {
			cnt++;
			Union(res, s1, s2, n);

			if (cnt > 1 && strcmp(firstAtm, res) == 0) {
				check = false;
				break;
			}
			if (strcmp(res, s) == 0) break;

			Reload(s1, s2, res, n);
		}

		if (check) printf("%d", cnt);
		else printf("-1");

		printf("\n");
	}

	return 0;
}