题目描述

一串由长长的数字组成的电话号码通常很难记忆。为了方便记忆,有种方法是用单词来方便记忆。例如用“Three Tens”来记忆电话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-4567,310-GINO的标准形式是310-4466,3-10-10-10的标准形式是310-1010。

如果两个电话号码的标准形式是一样的,那么这两个电话号码就是一样的。

现在有一本电话簿,请从中找出哪些电话号码是重复的。

输入输出格式

输入格式:

第一行一个正整数N,表示有多少个电话号码。

以下N行,每行一个电话号码,电话号码由数字、大写字母(除Q、Z)和连字符组成。电话号码长度不会超过1000。所有电话号码都合法。

输出格式:

将所有重复的电话号码按字典序以标准形式输出,并且在每个电话号码后跟一个整数,表示该电话号码共出现了多少次,电话号码和整数间用一个空格隔开。不要输出多余空行。

如果没有重复的电话号码,则输出:No duplicates.

输入输出样例

输入样例#1: 复制

3
TUT-GLOP
3-10-10-10
310-1010
输出样例#1: 复制

310-1010 2

说明

【数据范围】

对于30%的数据,N<=20。

对于50%的数据,N<=10000。

对于100%的数据,N<=100000。

不用开map

/*用一个map把所有的字母表示的数字存起来,0和1题目中没说,但也要表示。
一个<string,int>类型的map表示a这个字符串出现的次数。
把每个读入的字符串转化为标准形式存起来,如果有出现次数超过两次的,存起来,答案数++。
最后将答案按字典序排序输出。 (如果map的初始化写到了函数里,别忘记调用!!!,一开始没调用,全输出的空格,调了半个多小时)。*/ #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<string>
using namespace std; int n,cnt,now,pos,sum[];
string s,temp,tot[];
map<char,char> excel;
map<string,int> a;
struct Ans
{
int cs;
string chuan;
}ans[]; void init() //初始化函数
{
excel['']='';excel['']='';
excel['A']=excel['B']=excel['C']=excel['']='';
excel['D']=excel['E']=excel['F']=excel['']='';
excel['G']=excel['H']=excel['I']=excel['']='';
excel['J']=excel['K']=excel['L']=excel['']='';
excel['M']=excel['N']=excel['O']=excel['']='';
excel['P']=excel['R']=excel['S']=excel['']='';
excel['T']=excel['U']=excel['V']=excel['']='';
excel['W']=excel['X']=excel['Y']=excel['']='';
} bool cmp(Ans a,Ans b)
{
return a.chuan+b.chuan<b.chuan+a.chuan;
} int main()
{
init(); //千万千万别忘记调用
scanf("%d",&n);
for(int i=;i<=n;i++)
{
cin>>s;
temp.clear();pos=; //将temp清空,pos归零
for(int j=;j<s.length();j++)
{
if(s[j]!='-') //转化为数字
{
temp+=excel[s[j]];
pos++;
}
if(pos==) temp+='-',pos=-; //到了该加'-'的地方,加上'-',同时将pos设为负值,防止重复添加
}
if(!a[temp]) tot[++cnt]=temp; //如果这个字符串没出现过,将这个字符串加入到已有的字符串中
a[temp]++; //该字符串出现的次数++
}
for(int i=;i<=cnt;i++) //找哪个字符串是重复的
{
if(a[tot[i]]>)
{
ans[++now].chuan=tot[i]; //存答案
ans[now].cs=a[tot[i]];
}
}
if(!now) //没有重复的
{
printf("No duplicates.");
return ;
}
sort(ans+,ans+now+,cmp); //按字典序排列
for(int i=;i<=now;i++)
{
cout<<ans[i].chuan<<' ';
printf("%d\n",ans[i].cs);
}
return ;
}

map TLE一个点

/*用一个map把所有的字母表示的数字存起来,0和1题目中没说,但也要表示。
一个<string,int>类型的map表示a这个字符串出现的次数。
把每个读入的字符串转化为标准形式存起来,如果有出现次数超过两次的,存起来,答案数++。
最后将答案按字典序排序输出。 (如果map的初始化写到了函数里,别忘记调用!!!,一开始调用,全输出的空格,调了半个多小时)。*/ #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<string>
using namespace std; int n,cnt,now,pos,sum[];
string s,temp,tot[];
char excel[];
map<string,int> a;
struct Ans
{
int cs;
string chuan;
}ans[]; void init() //初始化函数
{
excel['']='';excel['']='';
excel['A']=excel['B']=excel['C']=excel['']='';
excel['D']=excel['E']=excel['F']=excel['']='';
excel['G']=excel['H']=excel['I']=excel['']='';
excel['J']=excel['K']=excel['L']=excel['']='';
excel['M']=excel['N']=excel['O']=excel['']='';
excel['P']=excel['R']=excel['S']=excel['']='';
excel['T']=excel['U']=excel['V']=excel['']='';
excel['W']=excel['X']=excel['Y']=excel['']='';
} bool cmp(Ans a,Ans b)
{
return a.chuan+b.chuan<b.chuan+a.chuan;
} int main()
{
init(); //千万千万别忘记调用
scanf("%d",&n);
for(int i=;i<=n;i++)
{
cin>>s;
temp.clear();pos=; //将temp清空,pos归零
for(int j=;j<s.length();j++)
{
if(s[j]!='-') //转化为数字
{
temp+=excel[s[j]];
pos++;
}
if(pos==) temp+='-',pos=-; //到了该加'-'的地方,加上'-',同时将pos设为负值,防止重复添加
}
if(!a[temp]) tot[++cnt]=temp; //如果这个字符串没出现过,将这个字符串加入到已有的字符串中
a[temp]++; //该字符串出现的次数++
}
for(int i=;i<=cnt;i++) //找哪个字符串是重复的
{
if(a[tot[i]]>)
{
ans[++now].chuan=tot[i]; //存答案
ans[now].cs=a[tot[i]];
}
}
if(!now) //没有重复的
{
printf("No duplicates.");
return ;
}
sort(ans+,ans+now+,cmp); //按字典序排列
for(int i=;i<=now;i++)
{
cout<<ans[i].chuan<<' ';
printf("%d\n",ans[i].cs);
}
return ;
}

char数组AC

P2037 电话号码的更多相关文章

  1. 洛谷P2037 电话号码

    P2037 电话号码 题目描述 一串由长长的数字组成的电话号码通常很难记忆.为了方便记忆,有种方法是用单词来方便记忆.例如用“Three Tens”来记忆电话3-10-10-10. 电话号码的标准形式 ...

  2. [LeetCode] Valid Phone Numbers 验证电话号码

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  3. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  4. 【代码笔记】iOS-替换电话号码中间4位为-号

    一,效果图. 二,代码. RootViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional se ...

  5. 国内固定电话正则验证:'tel': [/0\d{2,3}-\d{7,8}(|([-\u8f6c]{1}\d{1,5}))$/, "请填写有效的电话号码"],

    // 验证字段 $('#info_form').validator({ rules : { checkMobile : function(ele) { return checkMobile(ele); ...

  6. C#中使用正则表达式验证电话号码、手机号、身份证号、数字和邮编

      验证电话号码的主要代码如下: public bool IsTelephone(string str_telephone) { return System.Text.RegularExpressio ...

  7. 防止在iOS设备中的Safari将数字识别为电话号码

    在测试中发现iPad上的Safari总会把长串数字识别为电话号码,文字变成蓝色,点击还会弹出菜单添加到通讯录. 别的地方倒也罢了,如果在用户名中出现数字(手机注册新浪微博的话用户名就是“手机用户xxx ...

  8. java 验证手机号码、电话号码(包括最新的电信、联通和移动号码)

    一.目前的号码段(2016-12-8更新)   二.代码 package com.test; import java.util.regex.Pattern; public class CheckPho ...

  9. php电话号码正则表达式常用例子

    电话号码正则表达式(支持手机号码,3-4位区号,7-8位直播号码,1-4位分机号) 02   03 ((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{ ...

随机推荐

  1. 简单即时通讯、聊天室--java NIO版本

    实现的功能: 运行一个服务端,运行多个客户端.在客户端1,发送消息,其余客户端都能收到客户端1发送的消息. 重点: 1.ByteBuffer在使用时,注意flip()方法的调用,否则读取不到消息. 服 ...

  2. vue npm run build 失败

    之前删除过 node-moudel 文件夹,然后 npm install 重新安装,一切OK.打包的时候,报错,找不到caniuse什么的.再删除node-moudel,重新cnpm install ...

  3. 音视频入门-09-RGB&YUV互转-使用开源库

    * 音视频入门文章目录 * 介绍开源库 使用第三方开源库来简化开发,屏蔽一些底层的复杂度,节省大量编写代码的时间. libyuv: Google 开源的实现各种 YUV 与 RGB 之间相互转换.旋转 ...

  4. PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  5. Extending WCF using IServiceBehavior, IOperationBehavior, and IParameterInspector

    [ServiceContract(Name = "PasswordGenerator")] public interface IPasswordGenerator { [Opera ...

  6. 基于re模块的计算器

    最终计算器需求: 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - ...

  7. ffmpeg 播放器原理

    1  播放器过程 线程1 : readPackets-------> audio_packets队列   video packets 队列 线程2: decodeAudio && ...

  8. Nginx的启动、停止等命令

    Windows下Nginx的启动.停止等命令 在Windows下使用Nginx,我们需要掌握一些基本的操作命令,比如:启动.停止Nginx服务,重新载入Nginx等,下面我就进行一些简单的介绍.1.启 ...

  9. jQuery效果之滑动

    jQuery 滑动方法有三种:slideDown().slideUp().slideToggle(). jQuery slideDown() 方法用于向下滑动元素, 语法:$(selector).sl ...

  10. select加锁分析(Mysql)

    [原创]惊!史上最全的select加锁分析(Mysql) 前言 大家在面试中有没遇到面试官问你下面六句Sql的区别呢 select * from table where id = ? select * ...