Soundex编码-题解

描述

Soundex编码是将基于它们的拼写听起来相同的单词归类在一起。例如,“can”和“khawn”,“con”和“gone”在Soundex编码下是等价的。
Soundex编码涉及将每个单词转换成一连串的数字,其中每一个数字代表一个字母:

表示B、F、P或V
表示C、G、J、K、Q、S、X或Z
表示D或T
表示L
表示M或N
表示R
字母A、E、I、O、U、H、W和Y在Soundex编码中不被表示,并且如果存在连续的字母,这些字母是用相同的数字表示的,那么这些字母就仅用一个数字来表示。具有相同Soundex编码的单词被认为是相等的。

输入描述

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

输出描述

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

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <bits/stdc++.h>
using namespace std;
int main( ){
string tmp="*123*12**22455*12623*1*2*2";
int i;
string in;
while(cin >> in) {
char temp, x = '0';
int len = in.length();
for( i=0; i<len; i++ ) {
temp = tmp[in[i]-'A'];
if( temp=='*' ){
x = '0';
continue;
}
if(temp == x) {
continue;
}
cout << temp;
x = temp;
}
printf("\n");
}
return 0;
}

Soundex编码-题解
https://chenxi-tijie.pages.dev/2025/07/Soundex编码-题解/
作者
chenxi
发布于
2025年7月4日
许可协议