1002:方便记忆的电话号码(from 北大oj)
问题描述:
总时间限制: 2000ms 内存限制: 65536kB
描述
英文字母(除Q和Z外)和电话号码存在着对应关系,如下所示:
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
标准的电话号码格式是xxx-xxxx,其中x表示0-9中的一个数字。有时为了方便记忆电话号码,我们会将电话号码的数字转变为英文字母,如把263-7422记成America。有时,我们还加上“-”作为分隔符,如把449-6753记成Hi-World。当然,我们未必要将所有的数字都转变为字母,比如474-6635可以记成iPhone-5。
总之,一个方便记忆的电话号码由数字和除Q、Z外的英文字母组成,并且可以在任意位置插入任意多的“-”符号。
现在 ,我们有一个列表,记录着许多方便记忆的电话号码。不同的方便记忆的电话号码可能对应相同的标准号码,你的任务就是找出它们。
输入
第一行是一个正整数n(n <= 100000),表示列表中的电话号码数。
其后n行,每行是一个方便记忆的电话号码,它由数字和除Q、Z外的英文字母、“-”符号组成,其中数字和字母的总数一定为7,字符串总长度不超过200。
输出
输出包括若干行,每行包括一个标准电话号码(xxx-xxxx)以及它重复出现的次数k(k >= 2),中间用空格分隔。输出的标准电话号码需按照升序排序。
如果没有重复出现的标准电话号码,则输出一行“No duplicates.”。
样例输入
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
参考代码(超时未通过):
#encoding:utf-8
def transform(s):
#转换为标准形式xxx-xxxx
temp = s.replace('-','',-1)
tempList = list(temp)
tempList.insert(3,'-')
temp = ''.join(tempList)
return temp
def judge(s):
#将英文字母全部转化为数字
temp = s
Dict = {'A':2,'B':2,'C':2,'D':3,'E':3,'F':3,'G':4,'H':4,'I':4,'J':5,'K':5,'L':5,'M':6,
'N':6,'O':6,'P':7,'R':7,'S':7,'T':8,'U':8,'V':8,'W':9,"X":9,'Y':9}
for i in range(len(s)):
if s[i] in Dict:
temp = temp.replace(s[i],str(Dict[s[i]]))
temp = transform(temp)
if temp in results:
results[temp]+=1
else:
results[temp]=1
n = eval(input())
nums = []
results = {}
flag = False
for i in range(n):
nums.append(input())
for i in range(n):
if nums[i].replace('-','').isdigit(): #不含英文字母
temp = transform(nums[i])
if temp in results:
results[temp] += 1
else:
results[temp] = 1
else:
judge(nums[i])
resultsList = sorted(list(results.keys())) #按照号码升序输出
for item in resultsList:
if results[item] >1:
flag = True
print(item+' '+str(results[item]))
if not flag:
print("No duplicates.")
在参考了另外一份通过的c++代码后,笔者发现本道题确实做麻烦了。以下更新两份Accepted代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
using namespace std;
char convert(char c){
if(c <= 'C') return '2';
else if(c <= 'F') return '3';
else if(c <= 'I') return '4';
else if(c <= 'L') return '5';
else if(c <= 'O') return '6';
else if(c <= 'S') return '7';
else if(c <= 'V') return '8';
else if(c <= 'Y') return '9';
}
int main()
{
int N;
bool flag = false;
cin >> N;
map<string,int> telp_count;
while(N--){
string s,res;
cin >> s;
int i = 0;
for(string::iterator s_it = s.begin();s_it != s.end();++s_it){
if(*s_it >= '0' && *s_it <= '9') res.push_back(*s_it);
if(*s_it >= 'A' && *s_it <= 'Z') res.push_back(convert(*s_it));
}
res.insert(3,1,'-');
++telp_count[res];
}
map<string,int>::iterator map_it = telp_count.begin();
for(;map_it != telp_count.end();++map_it)
if(map_it->second >= 2){
flag = true;
cout << map_it->first << " " << map_it->second << endl;
}
if(!flag)
cout << "No duplicates." << endl;
return 0;
以及python满分代码:
def convert(c):
if c <= 'C':
return 2
elif c <= 'F':
return 3
elif c <= 'I':
return 4
elif c <= 'L':
return 5
elif c <= 'O':
return 6
elif c <= 'S':
return 7
elif c <= 'V':
return 8
elif c <= 'Y':
return 9
n = eval(input())
nums = []
flag = False
results = {}
for i in range(n):
nums.append(input())
for i in range(n):
res = ''
for j in range(len(nums[i])):
if nums[i][j] >= '0' and nums[i][j] <= '9':
res = res.__add__(nums[i][j])
elif nums[i][j] >= 'A' and nums[i][j] <= 'Z':
res = res.__add__(str(convert(nums[i][j])))
temp = list(res)
temp.insert(3,'-')
temp = ''.join(temp)
results[temp] = results.setdefault(temp,0)+1
resultsList = sorted(list(results.keys())) #按照序列输出
for item in resultsList:
if results[item] >1:
flag = True
print(item+' '+str(results[item]))
if not flag:
print("No duplicates.")