字典树应用 - poj1002
字典树应用 - 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
代码如下:(没有过TLE,不知道哪里有问题了)
//数字树 - 字典树
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
bool findsolve = false;
struct trie
{
bool isEnd;//标记结束
int cnt;//标记数量
trie * next[10];//标记下一个结点
trie()
{
isEnd = false;
cnt = 0;
for(int i = 0 ; i < 10; i++)
next[i] = NULL;
}
};
trie * root = new trie;
void Insert(char * s)
{
int len = strlen(s);
// cout << len << endl;
trie *p = root, *nw;
for(int i = 6 ; i >= 0 ; i--)
{
if(p->next[s[i]-'0'] == NULL)
{
nw = new trie;
p->next[s[i]-'0'] = nw;
}
p = p->next[s[i]-'0'];
}
p->isEnd = true;
p->cnt++;
}
void del(trie * root)
{
trie*p = root;
if(root == NULL)
return;
for(int i = 0 ; i < 10 ; i++)
{
if(p->next[i] != NULL)
{
del(p->next[i]);
}
}
delete root;
return;
}
bool Search(char *s)
{
trie *p = root;
int len = strlen(s);
for(int i = 0; i < len ; i++)
{
if(p->next[s[i]-'0'] == NULL)
return false;
p = p->next[s[i]-'0'];
}
if(p->isEnd == true)
return true;
return 0;
}
//test:ok
int trans(char *s)
{
int x = 0;
int len = strlen(s);
for(int i = 0 ; i < len ; i++)
{
if(s[i] == '-')
continue;
x *= 10;
if(s[i] >= 'A' && s[i] <= 'Y')
x += (s[i]-'A'-(s[i]>'Q'))/3+2;
else if(s[i] >= '0' && s[i] <= '9')
x += s[i]-'0';
}
return x;
}
void dfs(trie* p,int m,char phone[9])
{
if(p->isEnd == true)
{
if(p->cnt > 1)
{
for(int i = 1; i <= 7 ; i++)
{
if(i == 4)
printf("-");
printf("%c",phone[i]);
}
printf(" %d\n",p->cnt);
findsolve = true;
}
return ;
}
for(int i = 0 ; i < 10 ; i++)
{
if(p->next[i] != NULL)
{
phone[m+1] = (char)(i+'0');
dfs(p->next[i],m+1,phone);
}
}
return ;
}
int main()
{
char phone[100];
freopen("in.txt","r",stdin);
// cin.sync_with_stdio(false);
int n, num;
// cin >> n;
scanf("%d",&n);
char ch[100];
for(int i = 0 ; i < n; i++)
{
scanf("%s",ch);
num = trans(ch);
//test:ok
char ans[100];
int j = 0;
if(num == 0)
{
for(int i = 0 ; i <= 6; i++)
{
ans[i] = '0';
}
ans[7] = '\0';
Insert(ans);
}
else
{
while(num)
{
int a = num % 10;
ans[j++] = (char)(a+'0');
num /= 10;
}
ans[j] = '\0';
Insert(ans);//插入树中
}
}
dfs(root,0,phone);
if(!findsolve)
printf("No duplicates.\n");
return 0;
}
大佬代码:
#include <cstdio>
#include <algorithm>
using namespace std;
char s[31];
int Hash()
{
int sum=0;
for(int i=0,k=0;k<7;i++)
{
if(s[i]>='0'&&s[i]<='9')
{
sum*=10;k++;
sum+=(s[i]-'0');
}
else if(s[i]>='A'&&s[i]<'Z')
{
sum*=10;k++;
sum+=((s[i]-'A'-(s[i]>'Q'))/3+2);
}
}
return sum;
}
int main()
{
int n;scanf("%d",&n);
int data[n];getchar();
for(int tmp=0;tmp<n;tmp++)
{
gets(s);
data[tmp]=Hash();
}
sort(data,data+n);
bool p=false;n--;
for(int i=0,num=1;i<n;i+=num=1)
{
while(data[i]==data[i+1])
{
num++;
i++;
}
if(num>1)
{
printf("%03d-%04d %d\n",data[i]/10000,data[i]%10000,num);
p=true;
}
}
if(!p)printf("No duplicates.\n");
return 0;
}
用map,哈希,字典树都可以解决,希望大佬能看出来我哪里有问题
字典树应用 - poj1002的更多相关文章
- poj1002 字典树+map+查询单词出现次数
487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 309235 Accepted: 55223 Descr ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树+博弈 CF 455B A Lot of Games(接龙游戏)
题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...
- 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
- 山东第一届省赛1001 Phone Number(字典树)
Phone Number Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 We know that if a phone numb ...
- 字典树 - A Poet Computer
The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...
- trie字典树详解及应用
原文链接 http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用 一.知识简介 ...
- HDU1671 字典树
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- 关于一个非常非常无语的bug,与君共勉
今天,哦,不对,是昨天晚上,我花了大概四十分钟去找一个bug,结果还没找到 错误代码" $('#sendAreaInfo').citypicker('reset'); $('#sendAre ...
- python学习笔记(四)— 补充
函数return多个值 函数如果有多个return值,那么会生成一个元组里面 def hello(a,b,c,d): return a,b,c,d res =hello('aa','cc','dd', ...
- A Magic Lamp---hdu3183(链表删除| RMQ)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 给你一个长度<1000的数a,和m<len(a); 让把数a删除m个数字之后剩下的数 ...
- MySQL的表分区详解 - 查看分区数据量,查看全库数据量----转http://blog.csdn.net/xj626852095/article/details/51245844
查看分区数据量,查看全库数据量 USE information_schema; SELECT PARTITION_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA.PAR ...
- 0606-Zuul构建API Gateway-Zuul过滤器以及禁用Zuul过滤器
一.概述 针对Spring Cloud的Zuul配备了许多在代理和服务器模式下默认启用的ZuulFilter bean. 有关启用的可能过滤器,请参阅zuul过滤器包. 二.Zuul过滤器使用 2.1 ...
- 工作中发现Web服务器的磁盘满后故障分析
遇到的问题: 今天收到报警,某台线上的服务器的磁盘已满,但是登上去使用du -sh /log/* 检查, 发现文件的大小远远小于磁盘的空间,此时不知道该如何解决! 解决的方法: 其实,如果只是 ...
- Code signing is required for product type 'Application' in SDK 'iOS 11.2'
在打包的时候出现这样一个错误,Code signing is required for product type 'Application' in SDK 'iOS 11.2' ,就是说代码签名证书 ...
- (转)VC串口小程序(用SerialPort类)
××××××××××××××××××××××××××××××××××××××××××××××××××××× 在MFC里面实现串口通讯有很多方式: 方案一:使用微软公司提供的 串口类,SerialPor ...
- python3 用requests 保存网页以及BeautifulSoup保存图片,并且在本地可以正常显示文章的内容和图片
用requests 模块做了个简单的爬虫小程序,将博客的一篇文章以及图片保存到本地,文章格式存为'.html'.当文章保存到本地后,图片的连接可能是目标站点的绝对或者相对路径,所以要是想在本地也显示图 ...
- 2017浙江省赛 A - Cooking Competition ZOJ - 3958
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3958 题目: "Miss Kobayashi's Drag ...