Soundex
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8200 | Accepted: 4039 |
Description
Soundex coding groups together words that appear to sound alike based on their spelling. For example, "can" and "khawn", "con" and "gone" would be equivalent under Soundex coding.
Soundex coding involves translating each word into a series of digits in which each digit represents a letter:
The letters A, E, I, O, U, H, W, and Y are not represented in Soundex coding, and repeated letters with the same code digit are represented by a single instance of that digit. Words with the same Soundex coding are considered equivalent.
Soundex coding involves translating each word into a series of digits in which each digit represents a letter:
1 represents B, F, P, or V 2 represents C, G, J, K, Q, S, X, or Z 3 represents D or T 4 represents L 5 represents M or N 6 represents R
The letters A, E, I, O, U, H, W, and Y are not represented in Soundex coding, and repeated letters with the same code digit are represented by a single instance of that digit. Words with the same Soundex coding are considered equivalent.
Input
Each line of input contains a single word, all upper case, less than 20 letters long.
Output
For each line of input, produce a line of output giving the Soundex code.
Sample Input
KHAWN PFISTER BOBBY
Sample Output
25 1236 11
Source
26个字母分别对应了不同的值,现在给一个字符串,按照如下规则输出:
1)如果字母有值,则考虑输出;否则不输出。
2)多个拥有相同值的字母并列,仅输出一个值。
直接比较,如果对每一个字符进行取值,取上一次不同字符值的值last,进行比较,如果本字符有值(不为0),且不同于上一次的值last,就输出其值。
注意点:
1)注意打表正确(1WA)。
2)无论这个值是否输出,皆保存本次值为last。
代码(1AC 1WA):
#include <cstdio>
#include <cstdlib>
#include <cstring>
char str[30];
int arr[50];
void init(){
arr[0] = 0; // A
arr[1] = 1; // B
arr[2] = 2; // C
arr[3] = 3; // D
arr[4] = 0; // E
arr[5] = 1; // F
arr[6] = 2; // G
arr[7] = 0; // H
arr[8] = 0; // I
arr[9] = 2; // J
arr[10] = 2; // K
arr[11] = 4; // L
arr[12] = 5; // M
arr[13] = 5; // N
arr[14] = 0; // O
arr[15] = 1; // P
arr[16] = 2; // Q
arr[17] = 6; // R
arr[18] = 2; // S
arr[19] = 3; // T
arr[20] = 0; // U
arr[21] = 1; // V
arr[22] = 0; // W
arr[23] = 2; // X
arr[24] = 0; // Y
arr[25] = 2; // Z
}
int main(void){
char last, tmp;
int len, i;
init();
while (scanf("%s", str) != EOF){
len = strlen(str);
last = 0;
for (i = 0; i < len ; i++){
if (arr[str[i] - 'A'] != 0 && arr[str[i] - 'A'] != last){
printf("%d", (last = arr[str[i] - 'A']));
}
else{
last = arr[str[i] - 'A'];
}
}
printf("\n");
}
return 0;
}
注意点: