Poj 1002 字典樹

來源:互聯網
上載者: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 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

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
先把字串轉化為數字,通過排序解決。較為費時!
#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是truep->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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.