一. 题目

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. 如何实现wpf的多国语言

    http://www.cnblogs.com/horan/archive/2012/04/20/wpf-multilanguage.html 4.0版本的locbaml http://michaels ...

  2. js之json

    关于json不了解的,请点击:http://www.json.org/json-zh.html json对象的属性必须要用双引号,值为字符串类型也只能使用双引号,例:{"name" ...

  3. Android Studio插件

    awesome-android-ui(UI范例库) https://github.com/wasabeef/awesome-android-ui/tree/master/pages LMBanners ...

  4. 【C#学习笔记】文本复制到粘贴板

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. Ensemble Learning 之 Gradient Boosting 与 GBDT

    之前一篇写了关于基于权重的 Boosting 方法 Adaboost,本文主要讲述 Boosting 的另一种形式 Gradient Boosting ,在 Adaboost 中样本权重随着分类正确与 ...

  6. 【英语】Bingo口语笔记(72) - play系列

  7. C++类构造函数初始化列表

    C++类构造函数初始化列表 构造函数初始化列表以一个冒号开始,接着是以逗号分隔的数据成员列表,每个数据成员后面跟一个放在括号中的初始化式.例如: class CExample {public:     ...

  8. 在centOS中加入本地ISO yum源

    注:本文转载自<liujun_live的博客>,感谢原博主的辛勤写作:原文地址:http://blog.sina.com.cn/s/blog_8ea8e9d50101em6f.html 在 ...

  9. 使用Rxjava缓存请求

    最近,我尝试使用RxJava开发了一款闲时备份app.我必须承认,一旦你get到了正确的方式,RxJava几乎感觉就像作弊.一切看起来更简洁,多个请求能够被组合,且非常容易控制.通过在UI线程观察和在 ...

  10. vs2012 Silverlight项目签名报错异常的处理方式

    项目刚生成为vs2012,原先的Silverlight项目,点击签名,竟然有问题,给上个截图 悲剧了,没有签名证书,就无法实现自动更新,想着vs2012可能几个更新没有安装吧,但是自己手动下载竟然一两 ...