首页 > 代码库 > UVa 10950 - Bad Code

UVa 10950 - Bad Code

题目:有一种编码方式,串只有小写字母构成,每个小写字母对应一个数字,现在给你妆化后的数字串,

            问有多少个原串与之对应,注意数字串里可能有一个前导0。

分析:搜索。按字母顺序存储映射表,按字母顺序匹配搜索即可。

说明:注意最多只输出前100个。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

char buf[101];

int  code[128];
char maps[128][5];
char letter[128];

int  Count = 0;
char save[101];
void dfs(int s, int d, int n, int m)
{
	if (Count == 100) return;
	char word[101];
	if (s == n) {
		save[d] = 0;
		printf("%s\n",save);
		Count ++;
		return;
	}
	for (int k = 0 ; k < m ; ++ k) {
		int count = 0;
		for (int i = s ; i < n ; ++ i) {
			word[count ++] = buf[i];
			word[count] = 0;
			if (!strcmp(word, maps[k]) || (word[0] == '0' && !strcmp(word+1, maps[k]))) {
				save[d] = letter[k];
				dfs(i+1, d+1, n, m);
			}
		}
	}
}

int main()
{
	int  n,t = 1;
	char c;
	while (~scanf("%d",&n) && n) {
		memset(code, 0, sizeof(code));
		for (int i = 0 ; i < n ; ++ i) {
			getchar();
			scanf("%c",&c);
			scanf("%d",&code[c]);
		}
		
		int count = 0;
		for (int i = 'a' ; i <= 'z' ; ++ i)
			if (code[i] > 0 && code[i] < 100) {
				letter[count] = i;
				if (code[i] > 9) {
					maps[count][0] = code[i]/10 + '0';
					maps[count][1] = code[i]%10 + '0';
					maps[count][2] = 0;
				}else {
					maps[count][0] = code[i] + '0';
					maps[count][1] = 0;
				}
				count ++;
			}
		
		scanf("%s",buf);
		printf("Case #%d\n",t ++);
		Count = 0;
		dfs(0, 0, strlen(buf), count);
		printf("\n");
	}
	return 0;
}

UVa 10950 - Bad Code