poj 2608 Soundex
Soundex
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 8097 | Accepted: 3990 |
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
#include<iostream>
using namespace std;
int main()
{
char letters[21];
int i;
int pre;
int now;
while(cin>>letters)
{
pre=0;
now=0;
for(i=0;i<strlen(letters);i++)
{
if(letters[i]=='B'||letters[i]=='F'||letters[i]=='P'||letters[i]=='V')
now=1;
else if(letters[i]=='C'||letters[i]=='G'||letters[i]=='J'||letters[i]=='K'||letters[i]=='Q'||letters[i]=='S'||letters[i]=='X'||letters[i]=='Z')
now=2;
else if(letters[i]=='D'||letters[i]=='T')
now=3;
else if(letters[i]=='L')
now=4;
else if(letters[i]=='M'||letters[i]=='N')
now=5;
else if(letters[i]=='R')
now=6;
else
now=0;
if(now==pre)
continue;
else if(now!=0)
{
cout<<now;
pre=now;
}
else
pre=now;
}
cout<<endl;
}
return 0;
}
浙公网安备 33010602011771号