487-3279

Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

题目链接:

sdut:   http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1001

poj:    http://poj.org/problem?id=1002

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.

输入

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.

输出

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.

示例输入

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 题目大意:给出大写字母和数字,按照一定的规则映射到电话号码(7位),查找所有的的电话号码,若是有重复,则先按照xxx-xxxx的格式输出电话号码(按照字典序),紧接着一个空格,输出重复的次数;要注意,字母Q和Z不会出现在输入的字符串中,而且,给出的“伪号码”的个数最多可以到100000个。
解题思路:由于伪号码的数量可能会非常大,如果定义二维数组保存伪号码的话,或许会容易导致超内存,最好的法子是使用哈希查找的方法,用一个数组hash[]保存重复的次数,下标保存电话号码,由于电话号码最长是7位,所以要定义一个10000000大的数组,就是hash[10000000],保存的时候就已经自动排完序了,所以省去了排序的步骤,效率相应也会比较高,最后遍历输出满足要求的电话号码和重复次数即可。(注意,若是使用c++的话无法运行程序,好像是超内存了,改成c就能正常运行了,真心不知道什么原因)
代码:
 #include<stdio.h>
#include<string.h>
int hash[]={};
int zhuanzhi(int n)
{
int sum=,i;
for(i=;i<n;i++)
sum*=;
return sum;
}
int zhuanhuan(char f[])
{
int sum=,i;
int t=strlen(f);
for(i=t-;i>=;i--)
{
sum=sum+(f[i]-'')*zhuanzhi(t-i);
}
return sum;
}
int main()
{
int zong,i,j;
scanf("%d",&zong);
while(zong--)
{
char f[],g[];
scanf("%s",f);
int s=-;
for(i=;f[i]!='\0';i++)
{
switch(f[i])
{
case '':
g[++s]='';
break;
case '':
g[++s]='';
break;
case 'A':case 'B':case 'C':case '':
g[++s]='';
break;
case 'D':case 'E':case 'F':case '':
g[++s]='';
break;
case 'G':case 'H':case 'I':case '':
g[++s]='';
break;
case 'J':case 'K':case 'L':case '':
g[++s]='';
break;
case 'M':case 'N':case 'O':case '':
g[++s]='';
break;
case 'P':case 'R':case 'S':case '':
g[++s]='';
break;
case 'T':case 'U':case 'V':case '':
g[++s]='';
break;
case 'W':case 'X':case 'Y':case '':
g[++s]='';
break;
}
}
g[++s]='\0';
hash[zhuanhuan(g)]++;
}
int count=;
for(i=;i<;i++)
if(hash[i]>)
{
printf("%03d-%04d %d\n",i/,i%,hash[i]);
count++;
}
if(count==)printf("No duplicates.\n");
return ;
}

方法二:用c++标准模板库关联容器map做,在sdut上ac了,但是在poj上超时了,不知道问题出在哪里。问题找到了,交的时候要选择“c++”,而默认的提交方式是“g++”,所以要注意这一点才行。

 #include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
int n;
cin>>n;
map<string,int>mapx;
pair<map<string,int>::iterator,bool>pairx;
int i;
while(n--)
{
string f,temp("");
cin>>f;
int flag=;
for(i=; i<=f.length(); i++)
{
if(flag==)
{
temp.append("-");
flag++;
}
if(f[i]=='A'||f[i]=='B'||f[i]=='C'||f[i]=='')
{
temp.append("");
flag++;
}
else if(f[i]=='D'||f[i]=='E'||f[i]=='F'||f[i]=='')
{
temp.append("");
flag++;
}
else if(f[i]=='G'||f[i]=='H'||f[i]=='I'||f[i]=='')
{
temp.append("");
flag++;
}
else if(f[i]=='J'||f[i]=='K'||f[i]=='L'||f[i]=='')
{
temp.append("");
flag++;
}
else if(f[i]=='M'||f[i]=='N'||f[i]=='O'||f[i]=='')
{
temp.append("");
flag++;
}
else if(f[i]=='P'||f[i]=='R'||f[i]=='S'||f[i]=='')
{
temp.append("");
flag++;
}
else if(f[i]=='T'||f[i]=='U'||f[i]=='V'||f[i]=='')
{
temp.append("");
flag++;
}
else if(f[i]=='W'||f[i]=='X'||f[i]=='Y'||f[i]=='')
{
temp.append("");
flag++;
}
else if(f[i]=='')
{
temp.append("");
flag++;
}
else if(f[i]=='')
{
temp.append("");
flag++;
}
}
pairx=mapx.insert(pair<string,int>(temp,));
if(pairx.second==)
{
++pairx.first->second;
}
else continue;
}
int flag2=;
map<string,int>::iterator p;
for(p=mapx.begin(); p!=mapx.end(); p++)
{
if(p->second==)
continue;
else
{
cout<<p->first<<" "<<p->second+<<endl;
flag2=;
}
}
if(flag2==)
{
cout<<"No duplicates."<<endl;
}
return ;
}
 

sdut 487-3279【哈希查找,sscanf ,map】的更多相关文章

  1. QMap QHash的选择(QString这种复杂的比较,哈希算法比map快很多)

    QMap QHash有近乎相同的功能.很多资料里面介绍过他们之间的区别了.但是都没有说明在使用中如何选择他们. 实际上他们除了存储顺序的差别外,只有key操作的区别. 哈希算法是将包含较多信息的“ke ...

  2. 查找算法(7)--Hash search--哈希查找

    1.哈希查找 (1)什么是哈希表(Hash) 我们使用一个下标范围比较大的数组来存储元素.可以设计一个函数(哈希函数, 也叫做散列函数),使得每个元素的关键字都与一个函数值(即数组下标)相对应,于是用 ...

  3. 数据结构与算法之PHP查找算法(哈希查找)

    一.哈希查找的定义 提起哈希,我第一印象就是PHP里的关联数组,它是由一组key/value的键值对组成的集合,应用了散列技术. 哈希表的定义如下: 哈希表(Hash table,也叫散列表),是根据 ...

  4. python数据结构与算法 29-1 哈希查找

    ).称为哈希查找. 要做到这种性能,我们要知道元素的可能位置.假设每一个元素就在他应该在的位置上,那么要查找的时候仅仅须要一次比較得到有没有的答案,但以下将会看到.不是这么回事. 到10. water ...

  5. python 哈希查找

    import random INDEXBOX= #哈希表元素个数 MAXNUM= #数据个数 class Node: #声明链表结构 def __init__(self,val): self.val= ...

  6. hash 哈希查找复杂度为什么这么低?

    hash 哈希查找复杂度为什么这么低? (2017-06-23 21:20:36) 转载▼   分类: c from: 作者:jillzhang 出处:http://jillzhang.cnblogs ...

  7. SDUT 3379 数据结构实验之查找七:线性之哈希表

    数据结构实验之查找七:线性之哈希表 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 根据给定 ...

  8. SDUT 3377 数据结构实验之查找五:平方之哈希表

    数据结构实验之查找五:平方之哈希表 Time Limit: 400MS Memory Limit: 65536KB Submit Statistic Problem Description 给定的一组 ...

  9. [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map

    13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the numbe ...

随机推荐

  1. 在django项目外,使用django.db链接数据库(postgres)

    要用python链接到数据库,又不想写太多代码.想到了django,就偷懒了下.用django.db直连. django版本:1.6.5 (1.5以后可以用以下代码) #coding=utf-8 __ ...

  2. SqlBulkCopy 批量复制数据到数据表

    使用 SqlBulkCopy 类只能向 SQL Server 表写入数据.但是,数据源不限于 SQL Server:可以使用任何数据源,只要数据可加载到 DataTable 实例或可使用 IDataR ...

  3. android上的图片占用内存问题

    近日正在把ios程序移植到android上,以前没做过android的程序,于是,想当然地把ios的图片资源放到了android工程的drawable文件夹下,这些图片都是png. 程序界面也很正常. ...

  4. Effective C++ -----条款53:不要轻忽编译期的警告

    严肃对待编译器发出的警告信息.努力在你的编译器的最高(最严苛)警告级别下争取“无任何警告”的荣誉. 不要过度依赖编译器的报警能力,因为不同的编译器对待事情的态度并不相同.一旦移植到另一个编译器上,你元 ...

  5. Effective C++ -----条款47:请使用traits classes表现类型信息

    Traits classes使得“类型相关信息”在编译期可用.它们以template和“templates特化”完成实现. 整合重载技术(overloading)后,traits classes有可能 ...

  6. codeforces 495A. Digital Counter 解题报告

    题目链接:http://codeforces.com/problemset/problem/495/A 这个题目意思好绕好绕~~好绕~~~~~,昨天早上做得 virtual 看不懂,晚上继续看还是,差 ...

  7. 【leetcode】Reverse Linked List(easy)

    Reverse a singly linked list. 思路:没啥好说的.秒... ListNode* reverseList(ListNode* head) { ListNode * rList ...

  8. 【leetcode】3Sum (medium)

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  9. window.parent与window.openner区别介绍

    今天总结一下js中几个对象的区别和用法: 首先来说说 parent.window与top.window的用法 "window.location.href"."locati ...

  10. Gym 100703K Word order 贪心

    题目链接 题意:给定一个长度为n的字符串,字符串仅由"F","N","A"三种字符组成,现有一种操作P,即把两个相邻的字符调换位置.要求把所 ...