您的位置:首页 > 其它

Soundex(map映射)

2015-07-02 11:06 190 查看
Soundex

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 9494Accepted: 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
KHAWN
PFISTER
BOBBY

Sample Output
25
1236
11

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;
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: