Poj 1002 字典树


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



先把字符串转化为数字,通过排序解决。较为费时!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int arr[1000000];
int cmp(const void * a,const void * b){
	return *(int *)a - *(int *)b;
}
int div2[8] = {1000000,100000,10000,1000,100,10,1};
int stoi(char * p){
	int j;
	int len = strlen(p);
	int sum = 0;
	for(j=0; j<len; j++){
		if( p[j] != '-' ){
			sum *= 10;
			if(p[j] >= 'A' && p[j] <= 'Y')
				sum += (p[j] - 'A' - (p[j] > 'Q')) / 3 + 2;
			else if(p[j] >= '0' && p[j] <= '9')
				sum += p[j] - '0';
		}
	}
	return sum;
}
int main(void) {
	//freopen("in.txt","r",stdin);
	int n,i;
	while(scanf("%d", &n) != EOF){
	char temp[100];
	for( i=0; i<n; i++){
		scanf("%s",temp);
		arr[i] = stoi(temp);
	}
	int flag = 0;
	qsort(arr, n, sizeof(int), cmp);
	int cnt = 0;

	for( i=1; i<=n; i++){
		if(arr[i] == arr[i-1]){
			cnt ++;
			flag=1;
		}
		else{
			if(cnt){
				int j;
				int tmp = arr[i-1];
				for( j=0; j<7; j++){
					printf("%d",tmp / div2[j]);
					tmp %= div2[j];
					if(j == 2)
						printf("-");
				}
				printf(" %d\n",cnt+1);
			}
			cnt = 0;
		}
	}

	if( !flag )
		printf("No duplicates. \n");
	}
	return 0;
}

下面使用字典树,解决,较为麻烦。

经常测试,没有太大的改进。运行时间很接近。

因为这里并没有用到字典树的优势,就是统计所有前缀。

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef struct trietree * Ptree;
struct trietree
{
	bool arrive;
	int treenum;
	Ptree next[10];
} node[1000000];
int size;
bool findsolve;
int dispose(char * p);
void addnum(int num); //增加数字串
void newtree(int no);
void dfs(char phone[9], int m, Ptree p);

int arr[100000];

int main()
{
	//freopen("in.txt","r",stdin);
	int n; //串的个数
	int i, j;
	int number;
	char phone[9];
	char ch[80];
	while (scanf("%d", &n) != EOF)
	{
		//scanf("%d", &n);
		findsolve = false; //是否有重复的串
		size = 1;
		newtree(1);
		for (i = 0; i < n; i++)
		{
			scanf("%s", ch);
			number = dispose(ch);
			arr[i] = number;
			//cout << number << endl;
			addnum(number);
		}

		dfs(phone, 0, &node[1]); //遍历
		if (!findsolve)
			printf("No duplicates. \n");

	}
	return 0;
}

void newtree(int no)
{
	int i;
	node[no].arrive = false;
	node[no].treenum = 0;
	for (i = 0; i <= 9; i++)
		node[no].next[i] = NULL;
}

void addnum(int num)
{
	Ptree p = &node[1];
	int i, k;
	for (i = 0; i <= 6; i++)
	{
		k = num % 10;
		num /= 10;
		if (!p->next[k])
		{
			newtree(++size);
			p->next[k] = &node[size];
		}
		p = p->next[k];
	}
	p->arrive = true; //只有叶子节点的 arrive是true
	p->treenum++;
}

void dfs(char phone[9], int m, Ptree p)
{
	if (true == p->arrive)
	{ //如果该节点存在数
		if (p->treenum > 1)
		{
			for (int i = 1; i <= 7; i++)
			{
				if (i == 4)
					printf("-");
				printf("%c", phone[i]);
			}
			printf(" %d\n", p->treenum);
			findsolve = true;
		}
		return;
	}

	for (int i = 0; i <= 9; i++)
	{
		if (p->next[i])
		{
			phone[m + 1] = i + '0';
			//cout << phone[m+1] << endl;
			dfs(phone, m + 1, p->next[i]);
		}
	}
}

int dispose(char * p)
{
	int num = 0;
	char * q = p;
	while (*++p != '\0')
		;
	p--;
	while (p >= q)
	{
		if (*p == '-')
		{
			p--;
			continue;
		}
		num *= 10;
		if (*p >= 'A' && *p <= 'Y')
		{
			num += (*p - 'A' - (*p > 'Q')) / 3 + 2;
		}
		else if (*p >= '0' && *p <= '9')
			num += *p - '0';
		p--;
	}
	return num;
}


你可能感兴趣的:(Poj 1002 字典树)