Soundex编码

题目描述

Soundex编码是将基于它们的拼写听起来相同的单词归类在一起。例如,“can”和“khawn”,“con”和“gone”在Soundex编码下是等价的。
Soundex编码涉及将每个单词转换成一连串的数字,其中每一个数字代表一个字母:
1表示B、F、P或V
2表示C、G、J、K、Q、S、X或Z
3表示D或T
4表示L
5表示M或N
6表示R
字母A、E、I、O、U、H、W和Y在Soundex编码中不被表示,并且如果存在连续的字母,这些字母是用相同的数字表示的,那么这些字母就仅用一个数字来表示。具有相同Soundex编码的单词被认为是相等的。

输入
输入的每一行给出一个单词,全大写,少于20个字母长

输出
对每行输入,输出一行,给出Soundex编码。

输入样例
KHAWN
PFISTER
BOBBY

输出样例
25
1236
11
.
.
.
.
.
.
分析
一道水题
注意:
字母A、E、I、O、U、H、W和Y在Soundex编码中不被表示,并且如果存在连续的字母,这些字母是用相同的数字表示的,那么这些字母就仅用一个数字来表示
.
.
.
.
.
程序:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
string s;
int work(int w)
{
	char x=s[w];
	int ans=0,ans1=0;
	if (x=='B'||x=='F'||x=='P'||x=='V') ans=1; else
	if (x=='C'||x=='G'||x=='J'||x=='K'||x=='Q'||x=='S'||x=='X'||x=='Z') ans=2; else
	if (x=='D'||x=='T') ans=3; else
	if (x=='L') ans=4; else
	if (x=='M'||x=='N') ans=5; else
	if (x=='R') ans=6;
	
	if (w==0) return ans;
	
	x=s[w-1];
	if (x=='B'||x=='F'||x=='P'||x=='V') ans1=1; else
	if (x=='C'||x=='G'||x=='J'||x=='K'||x=='Q'||x=='S'||x=='X'||x=='Z') ans1=2; else
	if (x=='D'||x=='T') ans1=3; else
	if (x=='L') ans1=4; else
	if (x=='M'||x=='N') ans1=5; else
	if (x=='R') ans1=6;
	if (ans==ans1) return 0; else return ans;
}

int main()
{
	while (cin>>s)
	{
		string zfc="";
		int u=work(0);
		if (u!=0) zfc=zfc+((char)(u+'0'));
		int i=1,l=s.length() -1;
		while (i<=l)
		{
			u=work(i);
			if (u!=0) zfc=zfc+((char)(u+'0'));
			i++;
		}
		cout<<zfc<<endl;
	}
	return 0;
}

转载于:https://www.cnblogs.com/YYC-0304/p/10292826.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值