Poj 1002 dictionary tree

Source: Internet
Author: User

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
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 cocould 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 shoshould 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

124873279ITS-EASY888-45673-10-10-10888-GLOPTUT-GLOP967-11-11310-GINOF101010888-1200-4-8-7-3-2-7-9-487-3279

Sample output

310-1010 2487-3279 4888-4567 3
First, convert the string to a number and sort it. Time-consuming!
#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;}

The following uses the dictionary tree to solve the problem.

Often tested, without much improvement. The running time is very close.

Because the advantage of the dictionary tree is not used here, it is to count all prefixes.

# 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); // increase the number of strings 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; // Number of strings int I, j; int nu Mber; char phone [9]; char ch [80]; while (scanf ("% d", & N )! = EOF) {// scanf ("% d", & N); findsolve = false; // check whether duplicate string 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]); // traverse 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; // only the arrive of the leaf node is truep-> treenum ++ ;} void DFS (char phone [9], int M, ptree p) {If (true = p-> arrive) {// if this node has a number 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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.