uva 1509 - Leet(暴力)

题目大意:给出k,表示一个字符可以对应k给字符编码,给出字符串1,问时候有符合的编码可以生成字符串2.

解题思路:暴力枚举,对于每个碰到的字符记录对应的编码。

#include <cstdio>
#include <cstring>

int k, T, len_1, len_2;
char str[60], strMap[60], Map[256][50];

bool DFS(int pos_1, int pos_2) {
	if (pos_1 == len_1 || pos_2 == len_2)
		return pos_1 == len_1 && pos_2 == len_2;

	if (Map[str[pos_1]][0]) {
		for (int i = 0; Map[str[pos_1]][i]; i++)
			if (Map[str[pos_1]][i] != strMap[pos_2++])
				return false;
		if (DFS(pos_1 + 1, pos_2))
			return true;
	}
	else for (int i = 1; i <= k; i++) {
		int pos = 0;
		for (int j = pos_2; j < pos_2 + i; j++)
			Map[str[pos_1]][pos++] = strMap[j];
		Map[str[pos_1]][pos] = 0;
		if (DFS(pos_1 + 1, pos_2 + i))
			return true;
		Map[str[pos_1]][0] = 0;
	}
	return false;
}

int main() {
	scanf("%d", &T);
	while (T--) {
		memset(Map, 0, sizeof(Map));
		scanf("%d%s%s", &k, str, strMap);
		len_1 = strlen(str), len_2 = strlen(strMap);
		printf("%d\n", DFS(0, 0));
	}
	return 0;
}


你可能感兴趣的:(uva 1509 - Leet(暴力))