UVa 10950 - Bad Code

203 篇文章 0 订阅
120 篇文章 0 订阅

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

            问有多少个原串与之对应,注意数字串里可能有一个前导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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值