UVa 739 - Soundex Indexing

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

中文網址

Solution

簡單題。

Code

UVa 739
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
41
42
43
44
45
46
47
48
49
50
#include<cstdio>
#include<cstring>

inline int code(char c)
{
if (c == 'B' || c == 'P' || c == 'F' || c == 'V')
return 1;
else if (c == 'C' || c == 'S' || c == 'K' || c == 'G' || c == 'J' || c == 'Q' || c == 'X' || c == 'Z')
return 2;
else if (c == 'D' || c == 'T')
return 3;
else if (c == 'L')
return 4;
else if (c == 'M' || c == 'N')
return 5;
else if (c == 'R')
return 6;
else return 0;
}
int main()
{
char str[22];
puts(" NAME SOUNDEX CODE");

while (fgets(str, 22, stdin))
{
int count = 3, len = strlen(str) - 1, last = code(str[0]);
str[len] = NULL;
printf(" %-25s%c", str, str[0]);
for (int i = 1; i < len&&count; i++)
{
int c = code(str[i]);
if (c != last&&c)
{
putchar('0' + c);
count--;
}

last = c;
}

while (count--)
putchar('0');
putchar('\n');
}

puts(" END OF OUTPUT");

return 0;
}