一. 题目

487-3279
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 274040   Accepted: 48891

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

Source

 

二. 题意

  • 电话簿上的电话号码为了方便记忆,并非全用数字表示,有些用字母表示或用 “ - ” 符号分割
  • 字母与数字之间存在一定的映射关系,“ - ” 没有实际意义
  • 通过转换后得到统一标准的电话号码,其构成为"xxx-xxxx", 总共由八位字符(七位数字和一个"-")符号构成
  • 现在给定一个电话清单,从中找出重复的电话号码

三. 分析

  • 算法核心: 三种解决方法

    • 桶排序
    • 字典树(Trie树)
    • 快速排序
  • 实现细节:
    • 桶排序

      • 桶的大小为七位数的上限(10000000)
      • 每个桶存储对应7位数的电话号码出现的次数
      • 将电话号码转换为7位数,递增对应该七位数的桶元素的值
      • 遍历所有桶,得到和输出大于1的所有桶元素
      • 小细节: 字符到数的转换,输出格式的正确转换
    • 字典树(Trie树)
      • 建立Trie树: 用静态数组存储每个node,  简化代码实现
      • 遍历所有输入电话号码,转换为数字,将该数字插入Trie树
      • 每插入一个完整电话号码,在其最后插入的node中设置两个标记:出现次数 和 完整电话号码的结束标记
      • 深度遍历(dfs)Trie树, 输出所有出现次数大于1的完整电话号码
      • Trie树的所有child构成为从左到右(0 - 9),遍历的顺序也是如此,从而保证了电话号码的输出顺序也是从小到大
    • 快速排序
      • 用一个二维字符数组存储所有的电话号码
      • 对该数组进行快速排序
      • 遍历该有序数组,顺序输出大于1的所有电话号码
      • 个人实现算法的宗旨是尽量少用库,能实现的尽量自己实现
        • 对于此题,发现如果用自己实现的快速排序,发现会超时
        • 换成库的qsort则可以通过
        • 分析发现实际上库对于qsort做了很多的优化,并非基本的快排,有时间可以研究一下
        • 两个qsort优化的参考文档:

           http://blog.chinaunix.net/uid-25510439-id-275436.html

           http://blog.csdn.net/insistgogo/article/details/7785038

四. 题解

  • 桶排序
 #include <stdio.h>

 #define PHONE_NUMBER 256
#define MAX_NUMBER 10000000
#define BASE_NUMBER 1000000
#define LENGTH_NUMBER 8
#define MAP_SIZE ('Z' - 'A' + 1)
int map_num[MAP_SIZE] = {, , , , , , , , ,
, , , , , , , -, ,
, , , , , , , -};
int bucket[MAX_NUMBER]; int main()
{
int i = , j = , count = , nums = ; scanf("%d\n", &count);
for (j = ; j < count; j++) {
char phone_number[PHONE_NUMBER];
int tmp = ; scanf("%s\n", phone_number); for (i = ; phone_number[i] != '\0'; i++) {
if ('-' == phone_number[i]) continue;
tmp = tmp * +
((phone_number[i] >= 'A' && phone_number[i] <= 'Z') ?
map_num[phone_number[i] - 'A'] : phone_number[i] - '');
}
bucket[tmp]++;
} for (i = ; i < MAX_NUMBER; i++) {
int b_tmp, base_number = BASE_NUMBER; if (bucket[i] < ) continue; b_tmp = i;
nums++; for (j = ; j < LENGTH_NUMBER; j++) {
if ( == j) { printf("-"); continue; }
printf("%d", b_tmp / base_number);
b_tmp %= base_number;
base_number /= ;
}
printf(" %d\n", bucket[i]);
} if ( == nums) printf("No duplicates.\n"); return ;
}
  • 字典树(Trie树)
 #include <stdio.h>

 #define BASE_NUMBER 1000000
#define MAP_SIZE ('Z' - 'A' + 1)
#define TRIE_CHILD_CNTS 10
#define PHONE_NUMBER 256
#define PHONE_NUMBER_LENGTH 7
#define BASE_NUMBER 1000000 int map_num[MAP_SIZE] = {, , , , , , , , ,
, , , , , , , -, ,
, , , , , , , -};
int tree_index = ;
int is_duplicated = ; typedef struct Trie {
int is_word;
int word_cnts;
struct Trie* childs[TRIE_CHILD_CNTS];
} trie_tree_t; trie_tree_t trie_nodes[BASE_NUMBER]; void new_trie_tree(int index) {
int i;
trie_nodes[index].is_word = ;
trie_nodes[index].word_cnts = ;
for (i = ; i < TRIE_CHILD_CNTS; i++) trie_nodes[index].childs[i] = NULL;
} void add_trie_node(int num) {
int i, j, base_number = BASE_NUMBER;
trie_tree_t* tree = &trie_nodes[]; for (i = ; i < PHONE_NUMBER_LENGTH; i++) {
j = num / base_number;
num %= base_number;
base_number /= ;
if (NULL == tree->childs[j]) {
new_trie_tree(++tree_index);
tree->childs[j] = &trie_nodes[tree_index];
}
tree = tree->childs[j];
} tree->is_word = ;
tree->word_cnts++;
} void dfs(int phone_number[PHONE_NUMBER_LENGTH], int number_index, trie_tree_t *tree_p)
{
int i;
if (tree_p->is_word && < tree_p->word_cnts) {
for (i = ; i < PHONE_NUMBER_LENGTH; i++) {
if ( == i) printf("-"); printf("%d", phone_number[i]);
} printf(" %d\n", tree_p->word_cnts);
is_duplicated = ;
} for (i = ; i < TRIE_CHILD_CNTS; i++) {
if (tree_p->childs[i] && number_index < PHONE_NUMBER_LENGTH) {
phone_number[number_index] = i;
dfs(phone_number, number_index + , tree_p->childs[i]);
}
}
} int main()
{
int i = , j = , tmp = , count = ;
int number[PHONE_NUMBER_LENGTH]; scanf("%d\n", &count);
for (j = ; j < count; j++) {
char phone_number[PHONE_NUMBER];
int tmp = ;
scanf("%s\n", phone_number); for (i = ; phone_number[i] != '\0'; i++) {
if ('-' == phone_number[i]) continue;
tmp = tmp * +
((phone_number[i] >= 'A' && phone_number[i] <= 'Z') ?
map_num[phone_number[i] - 'A'] : phone_number[i] - '');
} add_trie_node(tmp);
} dfs(number, , &trie_nodes[]); if ( == is_duplicated) printf("No duplicates.\n"); return ;
}
  • 快速排序
 #include <stdio.h>
#include <stdlib.h>
#include <string.h> #define PHONE_NUMBER 256
#define MAX_NUMBERS 100000
#define LENGTH_NUMBER 9
#define MAP_SIZE ('Z' - 'A' + 1)
#define SEP_INDEX 3 #define CUSTOM 0
#define USING_POINTER_FOR_COMPARE 0 char map_num[MAP_SIZE] = {'', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '-1', '',
'', '', '', '', '', '', '', '-1'}; char numbers[MAX_NUMBERS][LENGTH_NUMBER]; #if USING_POINTER_FOR_COMPARE
char *p_numbers[MAX_NUMBERS];
#endif int is_duplicate = ; int mystrcmp(const char *str1, const char *str2)
{
while (*str1 == *str2) {
if (*str1 == '\0') return ;
str1++; str2++;
} return *str1 - *str2;
} #if CUSTOM #if USING_POINTER_FOR_COMPARE void quicksort(char *A[], int left, int right, int(*func)(const char*, const char*)) {
int i = left, j = right;
char *t, *tmp;
tmp = A[i]; if (i > j) return; while (i != j) {
while (func(tmp, A[j]) <= && i < j) j--;
while (func(A[i], tmp) <= && i < j) i++; if (i < j) {
t = A[i];
A[i] = A[j];
A[j] = t;
}
}
A[left] = A[i];
A[i] = tmp; quicksort(A, left, i - , func);
quicksort(A, i + , right, func);
} #else void mystrcpy(char *str1, const char *str2)
{
while (*str2 != '\0') {
*str1 = *str2;
str1++; str2++;
} *str1 = '\0';
} void quicksort(char A[][LENGTH_NUMBER], int left, int right, int(*func)(const char*, const char*)) {
int i = left, j = right;
char t[LENGTH_NUMBER], tmp[LENGTH_NUMBER];
mystrcpy(tmp, A[i]); if (i > j) return; while (i != j) {
while (func(tmp, A[j]) <= && i < j) j--;
while (func(A[i], tmp) <= && i < j) i++; if (i < j) {
mystrcpy(t, A[i]);
mystrcpy(A[i], A[j]);
mystrcpy(A[j], t);
}
}
mystrcpy(A[left], A[i]);
mystrcpy(A[i], tmp); quicksort(A, left, i - , func);
quicksort(A, i + , right, func);
} #endif #endif int sort_function(const void *a,const void *b)
{
return(strcmp((char*)a,(char*)b));
} int main()
{
int i = , j = , count = , nums = , number_index = ; scanf("%d\n", &count);
for (j = ; j < count; j++) {
char phone_number[PHONE_NUMBER]; scanf("%s\n", phone_number); for (i = ; phone_number[i] != '\0'; i++) {
if ('-' == phone_number[i]) continue;
if (SEP_INDEX == number_index) numbers[j][number_index++] = '-';
numbers[j][number_index++] = ((phone_number[i] >= 'A' && phone_number[i] <= 'Z') ?
map_num[phone_number[i] - 'A'] : phone_number[i]);
}
number_index = ; #if USING_POINTER_FOR_COMPARE
p_numbers[j] = numbers[j];
#endif } #if CUSTOM
quicksort(p_numbers, , count - , mystrcmp); #if USING_POINTER_FOR_COMPARE for (j = ; j < count - ; j++) {
if ( == mystrcmp(p_numbers[j], p_numbers[j + ])) {
is_duplicate = ;
nums++;
} else {
if ( < nums) printf("%s %d\n", p_numbers[j], nums);
nums = ;
}
} if ( < nums) printf("%s %d\n", p_numbers[j], nums); #else for (j = ; j < count - ; j++) {
if ( == mystrcmp(numbers[j], numbers[j + ])) {
is_duplicate = ;
nums++;
} else {
if ( < nums) printf("%s %d\n", numbers[j], nums);
nums = ;
}
} if ( < nums) printf("%s %d\n", numbers[j], nums); #endif #else qsort(numbers, count, LENGTH_NUMBER, sort_function); for (j = ; j < count - ; j++) {
if ( == mystrcmp(numbers[j], numbers[j + ])) {
is_duplicate = ;
nums++;
} else {
if ( < nums) printf("%s %d\n", numbers[j], nums);
nums = ;
}
} if ( < nums) printf("%s %d\n", numbers[j], nums); #endif if ( == is_duplicate) printf("No duplicates.\n"); return ;
}

[POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序的更多相关文章

  1. 字典树(Trie树)的实现及应用

    >>字典树的概念 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树.与二叉查找树不同,Trie树的 ...

  2. Atitit 常见的树形结构 红黑树  二叉树   B树 B+树  Trie树 attilax理解与总结

    Atitit 常见的树形结构 红黑树  二叉树   B树 B+树  Trie树 attilax理解与总结 1.1. 树形结构-- 一对多的关系1 1.2. 树的相关术语: 1 1.3. 常见的树形结构 ...

  3. 洛谷$P4585\ [FJOI2015]$火星商店问题 线段树+$trie$树

    正解:线段树+$trie$树 解题报告: 传送门$QwQ$ $umm$题目有点儿长我先写下题目大意趴$QwQ$,就说有$n$个初始均为空的集合和$m$次操作,每次操作为向某个集合内加入一个数$x$,或 ...

  4. luoguP6623 [省选联考 2020 A 卷] 树(trie树)

    luoguP6623 [省选联考 2020 A 卷] 树(trie树) Luogu 题外话: ...想不出来啥好说的了. 我认识的人基本都切这道题了. 就我只会10分暴力. 我是傻逼. 题解时间 先不 ...

  5. 字典树 trie树 学习

    一字典树 字典树,又称单词查找树,Trie树,是一种树形结构,哈希表的一个变种   二.性质 根节点不包含字符,除根节点以外的每一个节点都只包含一个字符: 从根节点到某一节点,路径上经过的字符串连接起 ...

  6. 【字符串算法】字典树(Trie树)

    什么是字典树 基本概念 字典树,又称为单词查找树或Tire树,是一种树形结构,它是一种哈希树的变种,用于存储字符串及其相关信息. 基本性质 1.根节点不包含字符,除根节点外的每一个子节点都包含一个字符 ...

  7. [转载]字典树(trie树)、后缀树

    (1)字典树(Trie树) Trie是个简单但实用的数据结构,通常用于实现字典查询.我们做即时响应用户输入的AJAX搜索框时,就是Trie开始.本质上,Trie是一颗存储多个字符串的树.相邻节点间的边 ...

  8. Luogu P2922 [USACO08DEC]秘密消息Secret Message 字典树 Trie树

    本来想找\(01Trie\)的结果找到了一堆字典树水题...算了算了当水个提交量好了. 直接插入模式串,维护一个\(Trie\)树的子树\(sum\)大小,求解每一个文本串匹配时走过的链上匹配数和终点 ...

  9. 字典树 Trie树

    什么是Trie树? 形如 其中从根节点到红色节点的路径上的字母所连成的字符串即为一个Trie树上所存的字符串. 比如,这个trie树上有ab,abc,bd,dda这些字符串. 至于怎么构建和查找或添加 ...

随机推荐

  1. 《c程序设计语言》读书笔记--字符串复制

    #include <stdio.h> #define MAXLINE 10 int getLine(char s[], int lim); void copy(char to[], cha ...

  2. php关于static关键字

    静态属性与方法可以在不实例化类的情况下调用,直接使用类名::方法名的方式进行调用.静态属性不允许对象使用->操作符调用.静态方法中,$this伪变量不允许使用.可以使用self,parent,s ...

  3. leetcode:Reverse Linked List II

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  4. Linux和Windows的换行符

    一直对换行符这个东西概念比较模糊,直到最近花了一点时间仔细研究了一下,才彻底搞清楚这个问题,本文前面介绍部分是外文转载,后面例子是个人总结,希望能对大家有一些帮助. 回车符号和换行符号产生背景 关于“ ...

  5. JSON 之 SuperObject(5): Format 与转义字符

    unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  6. Asp.Net正则获取页面a标签里的内容

    Asp.Net正则获取页面a标签里的内容 string url = "http://www.114369.com"; string html = MyCLib.NetClass.S ...

  7. 使用Phalcon开发工具碰到的数据库问题"Table 'XXX' doesn't exist in database when dumping meta-data for XXX"

    使用Phalcon开发工具,通过命令行生成程序框架 设置好config.php,在对数据库进行读取.保存数据的时候出现了问题“Table 'XXX' doesn't exist in database ...

  8. 给新centos系统虚拟机配置网络服务

    记录下今天新建虚拟机的过程吧. 镜像:CentOS-6.3-x86_64-minimal; 虚拟机版本: vm 8.0 LET'S----------------------->GO 手动新建一 ...

  9. javamail模拟邮箱功能--邮件删除-中级实战篇【邮件标记方法】(javamail API电子邮件实例)

    前言: JavaMail jar包下载地址:http://java.sun.com/products/javamail/downloads/index.html 本章可能是讲解javamail的最后一 ...

  10. 《C和指针》读书笔记 第5章-操作符和表达式

    原创文章,转载请注明出处:http://www.cnblogs.com/DayByDay/p/3936503.html