zoukankan      html  css  js  c++  java
  • Soundex编码

    题目描述

    Soundex编码是将基于它们的拼写听起来相同的单词归类在一起。例如,“can”和“khawn”,“con”和“gone”在Soundex编码下是等价的。
    Soundex编码涉及将每个单词转换成一连串的数字,其中每一个数字代表一个字母:
    1表示B、F、P或V
    2表示C、G、J、K、Q、S、X或Z
    3表示D或T
    4表示L
    5表示M或N
    6表示R
    字母A、E、I、O、U、H、W和Y在Soundex编码中不被表示,并且如果存在连续的字母,这些字母是用相同的数字表示的,那么这些字母就仅用一个数字来表示。具有相同Soundex编码的单词被认为是相等的。

    输入
    输入的每一行给出一个单词,全大写,少于20个字母长

    输出
    对每行输入,输出一行,给出Soundex编码。

    输入样例
    KHAWN
    PFISTER
    BOBBY

    输出样例
    25
    1236
    11
    .
    .
    .
    .
    .
    .
    分析
    一道水题
    注意:
    字母A、E、I、O、U、H、W和Y在Soundex编码中不被表示,并且如果存在连续的字母,这些字母是用相同的数字表示的,那么这些字母就仅用一个数字来表示
    .
    .
    .
    .
    .
    程序:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    string s;
    int work(int w)
    {
    	char x=s[w];
    	int ans=0,ans1=0;
    	if (x=='B'||x=='F'||x=='P'||x=='V') ans=1; else
    	if (x=='C'||x=='G'||x=='J'||x=='K'||x=='Q'||x=='S'||x=='X'||x=='Z') ans=2; else
    	if (x=='D'||x=='T') ans=3; else
    	if (x=='L') ans=4; else
    	if (x=='M'||x=='N') ans=5; else
    	if (x=='R') ans=6;
    	
    	if (w==0) return ans;
    	
    	x=s[w-1];
    	if (x=='B'||x=='F'||x=='P'||x=='V') ans1=1; else
    	if (x=='C'||x=='G'||x=='J'||x=='K'||x=='Q'||x=='S'||x=='X'||x=='Z') ans1=2; else
    	if (x=='D'||x=='T') ans1=3; else
    	if (x=='L') ans1=4; else
    	if (x=='M'||x=='N') ans1=5; else
    	if (x=='R') ans1=6;
    	if (ans==ans1) return 0; else return ans;
    }
    
    int main()
    {
    	while (cin>>s)
    	{
    		string zfc="";
    		int u=work(0);
    		if (u!=0) zfc=zfc+((char)(u+'0'));
    		int i=1,l=s.length() -1;
    		while (i<=l)
    		{
    			u=work(i);
    			if (u!=0) zfc=zfc+((char)(u+'0'));
    			i++;
    		}
    		cout<<zfc<<endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    关于Oracle 数据库使用dba_tables或者all_tables或者user_tables统计数据时,与直接查询表统计时数据不一致的记录
    js格式化时间
    slf4j介绍以及与Log4j、Log4j2、LogBack整合方法
    LogBack日志小记
    log4j2 日志框架小记
    Log4j日志框架小记
    29.求数字组最大子序列之和
    28.找出最的几位数
    27.数组元素重复度过数组长度一半
    26.字符串全排列
  • 原文地址:https://www.cnblogs.com/YYC-0304/p/10292826.html
Copyright © 2011-2022 走看看