PKU_1002_487-3279

487-3279


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

分析

这道题可以用三个方法
一、利用树
    类似于字典树的方法,建立一棵搜索树。提高速度、降低内存的方法是遍历树进行输出时尝试把递归转化为非 递归。
二、每次插入号码都进行排序
 速度关键在于排序。方便的话使用c语言自带qsort,要快的话当然是堆排序最快。 三、使用堆上内存  这个概念我也不太懂,看到有用c语言的人使用了vector,可以把数组开到很大,然后使用桶排序, 还不太懂,有空再看看。 这道题我的用时竟然达到了1297ms,内存占用量为4616k,
求高手搭救,怎么可以把时间和内存减下来。我看到有些大
牛用时和内存简直少得惊人!

Source Code

#include <iostream>
#include <string>
using namespace std;

char t[25]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9};
int total=0;

struct phoneNum
{
	string num;
	phoneNum *leftChild,*rightChild;
	int appear;
};

//把所有形式的号码转化为七个数字组成的string类
string trans(string s)
{
	string result="";
	int len=s.length();
	int i;
	for (i=0;i<len;i++)
	{
		char c=s[i];
		if (c!='-')
		{
			if ((c>='A')&&(c<='Y'))
			{
				c=t[c-'A']+'0';
			}
			result+=c;
		}
	}
	return result;
}

//插入结点
void insertNode(phoneNum **head,string s)
{
	phoneNum *p=*head,*q=*head;               //注意要改变head指针的值,需要通过head指针的地址来改变
	while (p!=NULL)                           //而head指针本身就为*phoneNum类型,故需向函数传入**phoneNum类型的值
	{
		if (p->num==s)                        //若已经存在该结点,则不需重复插入,只需计数加一即可
		{
			p->appear+=1;
			total=1;
			return ;                          /*   标记一  */
		}
		else                                  //一直查找,知道p为空指针或者找到匹配
		{
			q=p;
			if (p->num>s)
			{
				p=p->leftChild;
			}
			else 
			{
				p=p->rightChild;
			}
		}
	}
	p=new(phoneNum);                          //注意标记一,只有查找到空指针扔无法找到匹配的才运行到这里
	p->num=s;
	p->appear=1;
	p->leftChild=NULL;
	p->rightChild=NULL;
	if (*head==NULL)                          //要注意刚开始树仍未建起来
	{
		*head=p;
	}
	else
		if (q->num>p->num)
			{
				q->leftChild=p;
			}
			else
			{
				q->rightChild=p;
			}		
	return ;
}


//中序遍历输出树
void inOrderTraverse(phoneNum *p)
{
	if (p!=NULL)
	{
		inOrderTraverse(p->leftChild);
		if (p->appear>1)
		{
			int i;
			for (i=0;i<=2;i++)
				cout<<p->num[i];
			cout<<'-';
			for (i=3;i<=6;i++)
				cout<<p->num[i];
			cout<<' '<<p->appear<<endl;
		}
		inOrderTraverse(p->rightChild);
	}
}

int main()
{
	phoneNum *head=NULL;
	int n;
	cin>>n;
	int k=n;
	while (k--)
	{
		string s;
		cin>>s;
		s=trans(s);
		insertNode(&head,s);
	}
	if (total==0)
	{
		cout<<"No duplicates."<<endl;
	}
	else 
	{
		inOrderTraverse(head);
	}
	return 0;
}

参考链接:

使用vector,桶排:http://acm.awaysoft.com/viewsource.php?id=27

使用查找树:http://blog.163.com/zhoumhan_0351/blog/static/39954227200832622642266/





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值