2.2 487-3279

【问题描述】

        每个人都喜欢有令人难忘的电话号码。要想让电话号码变得令人难忘的一种方法是拼出一个令人难忘的单词或短语。例如,你可以拨打滑铁卢大学的电话,拨打令人难忘的电话号码TUT-GLOP。

       有时只有一部分号码被用来拼写一个单词,例如,你可以拨打310-gino从Gino's订购披萨。

       要使电话号码令人难忘的另一种方法是以一种令人难忘的方式对数字进行分组。你可以从比萨饼小屋中订购比萨饼,方法是拨打他们的“310”,即号码3-10-10-10。

       电话号码的标准格式是七位的十进制数字,第三和第四位之间包含连字符(例如888-1200)。电话的键盘提供字母到数字的映射,如下所示:

       A, B, C映射到2

       D, E, F映射到3

       G, H, I映射到4

       J, K, L映射到5

       M, N, O映射到6

       P, R, S映射到7

       T, U, V映射到8

       W, X, Y映射到9

       Q和Z没有映射。连接符不拨号,必要时可加上或去除。TUT-GLOP的标准格式是888-4567310-GINO的标准格式是310-44663-10-10-10的标准格式是310-1010。

       当两个电话号码有相同的标准格式时是等价的(拨同样的号码)。

       你的公司正在编制本地企业的电话号码目录,作为质量控制的一部分,你需要检查没有两个(或多个)企业具有相同的电话号码。


【输入形式】

输入包括一个案例。输入的第一行为一个正整数,指定目录中电话号码的数目(最多100000)。其余的各行列出目录中的电话号码,每个号码单独占一行。每个电话号码都是一个由十进制数字、大写字母(不包括Q和z)和连字符组成的字符串。字符串中的七个字符或是数字或是字母。

【输出形式】

对于出现超过一次的每个号码,按照标准格式每个输出一行,然后是空格,接着输出出现的次数。只出现1次的电话号码不输出。

【样例输入】

12
4873279
ITS-EASY
888-4567
3-10-10-10
888-GLOP
TUT-GLOP
967-11-11
310-GINO
F101010
888-1200
-4-8-7-3-2-7-9-
487-3279

【样例输出】

310-1010 2
487-3279 4
888-4567 3
【普通解法】

#include <iostream>
#include <algorithm>
using namespace std;
struct STR{
	string name;
	int time;
};
string change(string str)
{
	string s;
	for(int i=0;i<(int)str.length();i++)
	{
		if(str[i]=='-')
		continue;
		else if(str[i]>='0'&&str[i]<='9')
		s+=str[i];
		else if(str[i]>='A'&&str[i]<='C')
		s+='2';
		else if(str[i]>='D'&&str[i]<='F')
		s+='3';
		else if(str[i]>='G'&&str[i]<='I')
		s+='4';
		else if(str[i]>='J'&&str[i]<='L')
		s+='5';
		else if(str[i]>='M'&&str[i]<='O')
		s+='6';
		else if(str[i]>='P'&&str[i]<='S')
		s+='7';
		else if(str[i]>='T'&&str[i]<='V')
		s+='8';
		else if(str[i]>='W'&&str[i]<='Y')
		s+='9';
	}
	s.insert(3,"-");
	return s;
}
bool cmp(const STR& left,const STR& right)
{
	return left.name<right.name;
}
int main(){
	int t;
	cin>>t;
	STR s[t];
	int k=0; 
	for(int i=0;i<t;i++)
	{
		int flag=0;
		string str;
		cin>>str;
		str=change(str);//先将输入的号码变为标准格式
		for(int j=0;j<k;j++)
		if(str==s[j].name)//先判断这个人是否已经存在了
		{
			s[j].time++;
			flag=1;
		}
		if(flag==0)
		{
			s[k].name=str;
			s[k].time=1;
			k++;
		}
	}
	sort(s,s+k,cmp);
	for(int i=0;i<k;i++)
	{
		if(s[i].time!=1)
		cout<<s[i].name<<" "<<s[i].time<<endl;
	}
}

【应用map】

#include <iostream>
#include <map>
using namespace std;
string change(string str)
{
	string s;
	for(int i=0;i<(int)str.length();i++)
	{
		if(str[i]=='-')
		continue;
		else if(str[i]>='0'&&str[i]<='9')
		s+=str[i];
		else if(str[i]>='A'&&str[i]<='C')
		s+='2';
		else if(str[i]>='D'&&str[i]<='F')
		s+='3';
		else if(str[i]>='G'&&str[i]<='I')
		s+='4';
		else if(str[i]>='J'&&str[i]<='L')
		s+='5';
		else if(str[i]>='M'&&str[i]<='O')
		s+='6';
		else if(str[i]>='P'&&str[i]<='S')
		s+='7';
		else if(str[i]>='T'&&str[i]<='V')
		s+='8';
		else if(str[i]>='W'&&str[i]<='Y')
		s+='9';
	}
	s.insert(3,"-");
	return s;
}
int main(){
	int n;
	cin>>n;
	map<string,int> phone;
	while(n--){
		string str;
		cin>>str;
		str=change(str);
		phone[str]++;//下标对应string,值对应int 
	}
	for(map<string,int>::iterator it=phone.begin();it!=phone.end();it++)
	{
		if(it->second>1)
		cout<<it->first<<" "<<it->second<<endl;
	}
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值