Soundex(map映射)

来源:互联网 发布:网络歌曲红尘歌曲 编辑:程序博客网 时间:2024/05/18 22:10
Soundex
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 9494 Accepted: 4720

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: 
      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

KHAWNPFISTERBOBBY

Sample Output

25123611

Source

Waterloo local 1999.09.25
 
#include<cstdio>#include<iostream>#include<cstring>#include<map>using namespace std;int main(){    string s,s1;    int i,flag;    map<char,int>mp;    mp['B']=mp['F']=mp['P']=mp['V']=1;    mp['C']=mp['G']=mp['J']=mp['K']=mp['Q']=mp['S']=mp['X']=mp['Z']=2;    mp['D']=mp['T']=3;    mp['L']=4;    mp['M']=mp['N']=5;    mp['R']=6;    mp['A']=mp['E']=mp['I']=mp['O']=mp['U']=mp['H']=mp['W']=mp['Y']=0;    while(cin>>s)    {        flag=0;        if(mp[s[0]]!=0)        {            cout<<mp[s[0]];        }        for(i=1;i<s.length();i++)        {             flag=mp[s[i-1]];             if(mp[s[i]]!=flag&&mp[s[i]]!=0)             {                 cout<<mp[s[i]];             }        }        cout<<endl;    }}

0 0