题目描述

一串由长长的数字组成的电话号码通常很难记忆。为了方便记忆,有种方法是用单词来方便记忆。例如用“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. WxWidgets与其他工具包的比较(15种方案)

    一些一般注意事项: wxWidgets不仅适用于C ++,而且具有python,perl,php,java,lua,lisp,erlang,eiffel,C#(.NET),BASIC,ruby甚至ja ...

  2. vue 写一个瀑布流插件

    效果如图所示: 采用了预先加载图片,再计算高度的办法..网络差的情况下,可能有点卡 新建 vue-water-easy.vue  组件文件 <template> <div class ...

  3. UOJ #7 NOI2014购票(点分治+cdq分治+斜率优化+动态规划)

    重写一遍很久以前写过的题. 考虑链上的问题.容易想到设f[i]为i到1的最少购票费用,转移有f[i]=min{f[j]+(dep[i]-dep[j])*p[i]+q[i]} (dep[i]-dep[j ...

  4. Qt界面阴影效果(背景图片)

    实现原理: 1.顶层窗体设置为无边框,背景半透明 2.顶层窗体的子窗体使用带有阴影的图片做背景 代码: //CMainWindow.h#ifndef CMAINWINDOW_H#define CMAI ...

  5. POJ 3233-Matrix Power Series( S = A + A^2 + A^3 + … + A^k 矩阵快速幂取模)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 20309   Accepted:  ...

  6. springboot项目实用代码整理

    // 判断JSONOBJECT是否为空 CommonUtils.checkJSONObjectIsEmpty(storeInfo) // 判断字符串是否为空," "也为空 Stri ...

  7. Eclipse下使用Maven创建项目出现的archetype错误,记,转

    记自:http://blog.csdn.net/ZhuboSun/article/details/50099635 [1]出现的错误提示: Unable to create project from ...

  8. 编写Postgres扩展之五:代码组织和版本控制

    原文:http://big-elephants.com/2015-11/writing-postgres-extensions-part-v/ 编译:Tacey Wong 在关于编写Postgres扩 ...

  9. python—各种常用函数及库

    列表list1.append(x)         将x添加到列表末尾 list1.sort()                对列表元素排序 list1.reverse()            将 ...

  10. putty使用方法

    putty是一种体体积小,无需安装的一款免费安全使用方便的绿色软件,它主要用于远程控制linux系统,只要获取了远程的linux的地址,便可以远程控制linux系统以方便管理,越来越受到各方面的欢迎. ...