Soundex 算法 (散列算法)(非线性检索人名)

Soundex 算法遵循如下4个原则

【规则1】首先保存姓名首字母,然后从剩余字母中删除所有a、e、h、i、o、u、w、y;
【规则2】按照如下规则为剩余字母标上序号;
b, f, p, v — 1
c, g, j, k, q, s, x, z — 2
d, t — 3
l — 4
m, n — 5
r — 6
【规则3】在原姓名中,相邻连续出现的相同字母只保留第一次出现者,其余删除;
【规则4】最终结果需要形成 “字母 , 数字 , 数字 , 数字 ”形态,为此,
若数字超过 3 个,则只保留前 3 个数字,其余删除; 若数字个数少于 3 个,
则后面补 0 以保持 3 位数形态。

原姓名的’姓’通过 Soundex 算法得到一个 ‘键’ 与对应的’名字’形成一个 ‘键值对’ 方便后续搜索。

虽然 Soundex 算法是搜索算法但这里为求简单就只写一个输出通过 ‘姓’ 转变成的 ‘键’ 。

#include <stdio.h>

int main()
{
	char name[12];//名字 
	char surname[12];//姓
	char c = 0;
	c = getchar();
	for(int i = 0; c != ' '; i++)
	{
		name[i] = c;
		c = getchar();
	}
	c = getchar();
	for(int i = 0; c != '\n'; i++)
	{
		surname[i] = c;
		c = getchar();
	}
	printf("%c", surname[0]);
	int i = 0;
	int n = 0;
	for(i = 1; surname[i] != '\0' && i <= 3; i++)
	{
		if(surname[i] == surname[i-1])
		{
			continue;
		}
		if(surname[i] == 'b' || surname[i] == 'f' || surname[i] == 'p' || surname[i] == 'v')
		{
			printf("1");
		}
		else if(surname[i] == 'c' || surname[i] == 'g' || surname[i] == 'j' || surname[i] == 'k' || surname[i] == 'q' || surname[i] == 's' || surname[i] == 'x' || surname[i] == 'z')
		{
			printf("2");
		}
		else if(surname[i] == 'd' ||surname[i] == 't')
		{
			printf("3");
		}
		else if(surname[i] == 'l')
		{
			printf("4");
		}
		else if(surname[i] == 'm' || surname[i] == 'n')
		{
			printf("5");
		}
		else if(surname[i] == 'r')
		{
			printf("6");
		}
		else
		{
			n++;
			continue;
		}
	}
	for(; i <= 3; i++)
	{
		printf("0");
	}
	for(i = 0; i < n; i++)
	{
		printf("0");
	}
	printf("  ");
	puts(name);
	return 0;
}

大家有兴趣的话,可以试试你的名字哦!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值