forked from ajahuang/UVa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVa 10260 - Soundex.cpp
40 lines (38 loc) · 1.03 KB
/
UVa 10260 - Soundex.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Prepare the mapping of Soundex coding.
string soundex[6] = { "BFPV",
"CGJKQSXZ",
"DT",
"L",
"MN",
"R" };
size_t soundexSize = sizeof(soundex) / sizeof(soundex[0]);
string word;
while (cin >> word)
{
string code;
size_t prevIndex = soundexSize;
for (size_t i = 0; i < word.size(); ++i)
{
size_t j = 0;
for ( ; j < soundexSize; ++j)
{
if (soundex[j].find(word[i]) != string::npos)
{
if (j != prevIndex)
code.push_back(static_cast<char>('0' + j + 1));
prevIndex = j;
break;
}
}
if (j == soundexSize)
prevIndex = soundexSize;
}
cout << code << endl;
}
return 0;
}