poj1002

487-3279

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 234901   Accepted: 40968

Description

Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only part of
the number is used to spell a word. When you get back to your hotel tonight you can order a pizza from Gino‘s by dialing 310-GINO. Another way to make a telephone number memorable is to group the digits in a memorable way. You could order your pizza from Pizza
Hut by calling their ``three tens‘‘ number 3-10-10-10.

The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows:

A, B, and C map to 2

D, E, and F map to 3

G, H, and I map to 4

J, K, and L map to 5

M, N, and O map to 6

P, R, and S map to 7

T, U, and V map to 8

W, X, and Y map to 9

There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010.

Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.)

Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number.

Input

The input will consist of one case. The first line of the input specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on the line. The remaining lines list the telephone numbers in the directory, with each number
alone on a line. Each telephone number consists of a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactly seven of the characters in the string will be digits or letters.

Output

Generate a line of output for each telephone number that appears more than once in any form. The line should give the telephone number in standard form, followed by a space, followed by the number of times the telephone number appears in the directory. Arrange
the output lines by telephone number in ascending lexicographical order. If there are no duplicates in the input print the line:

No duplicates.

Sample Input

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

Sample Output

310-1010 2
487-3279 4
888-4567 3

用映射写的老是超时,后来几番周折改成764K 407MS过掉

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int a[30]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,0,7,7,8,8,8,9,9,9};
int b[10000000]={0},sum,len=0,num=0,k,d[10000000];
char s[100];
int main(){
	int n,flag=0;
	scanf("%d",&n);
	getchar();
	for(int i=0;i<n;i++){
	  gets(s);
	  sum=1;
	  int num=0;
	  for(int i=0;num<7;i++){
	  	if(s[i]>=‘A‘&&s[i]<=‘Z‘&&s[i]!=‘Q‘&&s[i]!=‘Z‘){
	  	  sum*=10;
		  sum+=(a[s[i]-‘A‘]);
	  	  num++;
	  	}
	  	else if(s[i]==‘-‘||s[i]==‘Q‘||s[i]==‘Z‘)continue;
	  	else{
	  	  sum*=10;
	  	  sum+=s[i]-‘0‘;
		  num++;
	  	}
	  }
	  sum%=10000000;
	  b[len++]=sum;
	}
	sort(b,b+n);
	len=0;
    for(int i=0;i<n-1;i+=num){
    	num=1;
    	k=i;
      	while(b[k]==b[k+1]){
      	  num++;
      	  k++;
      	}
      	if(num>1){
      		printf("%03d-%04d %d\n",(int)(b[k]/10000),(int)(b[k]%10000),num);//前导0一定要加的,因此WA了一次
      		flag=1;
      	}
    }
	if(!flag)printf("No duplicates.\n");
	return 0;
}

如果时间限制没有那么严格,用映射简单模拟一下倒也无妨

#include<iostream>
#include<map>
using namespace std;
int a[30]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,0,7,7,8,8,8,9,9,9};
typedef map<string,int> record;
record re;
int main(){
	int n,flag=0;
	string s;
	cin>>n;
	while(n--){
	  cin>>s;
	  int num=0;
	  string s1="";
	  for(int i=0;i<s.length()&&num<7;i++){
	  	if(s[i]>=‘A‘&&s[i]<=‘Z‘&&s[i]!=‘Q‘&&s[i]!=‘Z‘){
	  	  s1+=(char)(a[s[i]-‘A‘]+‘0‘);
	  	  num++;
	  	}
	  	else if(s[i]==‘-‘||s[i]==‘Q‘||s[i]==‘Z‘)continue;
	  	else{
	  	  s1+=s[i];
		  num++;
	  	}
	  	if(num==3)s1+=‘-‘;
	  }
	  re[s1]++;
	}
	for(record::iterator it=re.begin();it!=re.end();it++){
		if(it->second>1){
			cout<<it->first<<‘ ‘<<it->second<<endl;
			flag=1;
		}
		else continue;
	}
	if(!flag)cout<<"No duplicates."<<endl;
	return 0;
}

poj1002,布布扣,bubuko.com

时间: 2024-08-08 13:54:27

poj1002的相关文章

poj1002总结

1.之前一直是runtime error,没有找出具体原因,把冒牌排序改成了合并排序,当输入是100000行时,从大约10s变到1s内 2.感觉是atoi指针导致的,当判断atoi的入参不为NULL时才调用,提交结果变成了WA 3.修改了对输入时有QZ情况的处理,当一行中有QZ时,不是直接忽略该行,而是把QZ当成连字符一样忽略掉 4.开始以为一行最长的情况是15个字符,后来发现没有这个限制,题目中也没有说一行到底有多少长,就设定了1k的字符数组 提交AC

487-3279 (poj1002)

http://poj.org/problem?id=1002 这道题耗时一整天,终于搞定了.这道题一开始写的是超时,最后懂了,问题原因是有二重循环加strcmp比较,时间复杂度是O(n^3)所以超时了. #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<vector> using namespace std; char PanC

poj1002(水题)

1.题目描述 487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 244177   Accepted: 43308 Description Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word o

POJ1002电话号码累计

开始的时候理解错了题目意思,以为是:所有的号码在整理成标准形式以后,去重,字典排序,输出.就一直在想set的自动去重,还有map的索引问题,才发现自己C++深层的东西有许多都不了解.这个地方如果需要这样实现的话就肯定要卡住了. 说说这个题的实际理解.网上不少题解都说这是个水题QAQ.. 1.字符串输入 2.循环处理字符,字母-通过函数映射成相应的数字:数字-减去字符'0'后保存到空s中(此处涉及到字符串String用法,不太会所以还无法上全代码) 3.一个字符串处理结束后,对应此下标的元素+1(

POJ-1002: 487-3279 详解2: 号码全集法

> 分析 >> 由于题目对内存占用的限制在65535k, 因此可以定义 一个以电话号码为索引,值为电话号码出现次数的数组 > 直接附代码 /*--------------------------------- * 使用电话号码全集 * --------------------------------*/ #include "stdio.h" #include "string.h" #include "stdlib.h" /

POJ-1002: 487-3279 详解1: 堆排序法

> 分析 >> 本题的难点在于排序速度上 >> 排序算法要考虑重复项很多,无重复项两种情况 >> 当然由于本题对内存占用的要求不高,也可以不使用排序 > 总体思路 >> 先将电话号码按输入的顺序存下来 >> 对所有号码使用堆排序 >> 排序后根据号码连续出现的频率输出频率大于1的号码 > 输入转换 >> 本题中已详细定义了字母到数字的映射,因此推荐使用映射表较快 > 存储形式 >> 由于

C# ACM poj1002

排序 public static void acm1002(string[] azx) { string[] a = new string[azx.Length]; for (int i = 0; i < azx.Length; i++) { StringBuilder sb = new StringBuilder(); azx[i] = azx[i].Replace("-", ""); for (int j = 0; j < azx[i].Length

关于ACM与OJ

初级: 一.基本算法: (1)枚举. (poj1018,poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. (5)构造法.(poj3295,poj3239) (6.1)模拟法.(poj1008,poj1068,poj2632,poj1573,poj2993,poj2996,poj3087) (6.2)模拟法(高精度算法)(poj1001,poj1503,poj2389,poj2602,poj3982,21位大数的水仙

POJ题目Java代码(一)

POJ 1001 Exponentiation import java.math.BigDecimal; import java.util.Scanner; public class Poj1001 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ BigDecimal bigDecimal = new BigDecimal(sc.next())