Soundex编码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SSLGZ_yyc/article/details/83351066

题目描述

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;
}

猜你喜欢

转载自blog.csdn.net/SSLGZ_yyc/article/details/83351066