开篇,UVA 755 && POJ 1002 487--3279 (Trie + DFS / sort)
博客第一篇写在11月1号,果然die die die die die alone~
一道不太难的题,白书里被放到排序这一节,半年前用快排A过一次,但是现在做的时候发现可以用字典树加深搜,于是乐呵呵的开始敲了,后来被卡了两天,一直以为算法错了,最后发现是输出答案时忘了回溯,这问题之前没怎么注意过,也算不小的收获。
字典树A了之后换sort来写,没想到快排效率更高,时间减少了一半,在POJ上A了之后重新在UVA上提交,居然WA了,调试半个小时,发现是变长数组的问题,看来UVA上的编译器对c99的支持不太好。
虽然有点水题的感觉,但是收获不小:
1.DFS处理答案的时候要考虑是否回溯
2.更深理解哈希
3.VLA慎用
487-3279 |
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 first line of the input contains the number of datasets in the input. A blank line follows. The first line of each dataset 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. There's a blank line between datasets.
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.
Print a blank line between datasets.
Sample Input
1 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
UVA上的代码如下,POJ上的和UVA有点不同,是单组输入,但算法是一样的
#include<stdio.h>
#include<stdlib.h>
#include<string.h> struct trie
{
struct trie * next[];
int count;
};
struct trie * ROOT = NULL;
char RULE[] = {'','','','','','','','','','','','','','','','','','','','','','','','','',''};
char BOX[];
int FLAG; void insert(char * s);
void dfs(struct trie * temp,int len);
int main(void)
{
int t,n,len;
char s[],number[]; scanf("%d",&t);
while(t --)
{
FLAG = ;
ROOT = (struct trie *)malloc(sizeof(struct trie));
for(int i = ;i < ;i ++)
ROOT -> next[i] = NULL;
ROOT -> count = ; scanf("%d",&n);
while(n --)
{
len = ; scanf("%s",s);
for(int i = ;s[i];i ++) //转化为纯数字形式
{
if(s[i] >= '' && s[i] <= '')
number[len ++] = s[i];
else if(s[i] >= 'A' && s[i] <= 'Z')
number[len ++] = RULE[s[i] - 'A'];
}
number[len] = '\0';
insert(number);
}
dfs(ROOT,);
if(FLAG)
puts("No duplicates.");
puts("");
} return ;
} void insert(char * s) //trie插入函数
{
struct trie * temp = ROOT; for(int i = ;s[i];i ++)
if(temp -> next[s[i] - ''])
temp = temp -> next[s[i] - ''];
else
{
temp -> next[s[i] - ''] = (struct trie *)malloc(sizeof(struct trie));
for(int j = ;j < ;j ++)
temp -> next[s[i] - ''] -> next[j] = NULL;
temp -> next[s[i] - ''] -> count = ; temp = temp -> next[s[i] - ''];
}
temp -> count ++; return ;
} void dfs(struct trie * temp,int len) //深搜输出
{
for(int i = ;i < ;i ++)
if(temp -> next[i])
{
BOX[len] = i + '';
len ++;
if(temp -> next[i] -> count >= )
{
FLAG = ;
BOX[len] = '\0';
for(int j = ;BOX[j];j ++)
{
if(j == )
putchar('-');
printf("%c",BOX[j]);
}
printf(" %d\n",temp -> next[i] -> count);
len --; //注意回溯 continue;
}
dfs(temp -> next[i],len);
len --;
} return ;
}
Trie + DFS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <algorithm>
using namespace std; int RULE[] = {,,,,,,,,,,,,,,,,,,,,,,,,,}; int main(void)
{
int * ans;
int t,n,sum,flag,times,count,i,j,k;
char temp[]; scanf("%d",&t);
while(t --)
{
count = ;
scanf("%d",&n); ans = (int *)malloc(sizeof(int) * n); //如果在UVA上用ans[n]的形式便会WA
while(n --)
{
scanf("%s",temp);
for(i = sum = ;temp[i];i ++) //转化为纯数字形式(哈希)
{
if(temp[i] >= '' && temp[i] <= '')
{
sum *= ;
sum += (temp[i] - '');
}
else if(temp[i] >= 'A' && temp[i] <= 'Z')
{
sum *= ;
sum += (RULE[temp[i] - 'A']);
}
}
ans[count ++] = sum;
}
sort(ans,ans + count); //全部存入数组后排序 for(i = flag = ;i < count - ;i ++)
{
times = ; while(ans[i] == ans[i + ])
{
times ++;
i ++;
}
if(times > )
{
flag = ;
printf("%03d-%04d %d\n",ans[i] / ,ans[i] % ,times); //除法:截去后n位 求余:计算出后n位
}
}
if(!flag)
puts("No duplicates.");
if(t)
puts("");
} return ;
}
sort
开篇,UVA 755 && POJ 1002 487--3279 (Trie + DFS / sort)的更多相关文章
- 字符串专题:map POJ 1002
第一次用到是在‘校内赛总结’扫地那道题里面,大同小异 map<string,int>str 可以专用做做字符串的匹配之类的处理 string donser; str [donser]++ ...
- POJ 1002 487-3279
A - 487-3279 Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- uva 11732 - strcmp() Anyone? 不错的Trie题
题解:http://blog.csdn.net/u013480600/article/details/23122503 我的代码一直TLE,,,看了人家的之后,认为1.链式前向星比較好,2.*dept ...
- [POJ 1002] 487-3279 C++解题报告
487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 228365 Accepted: 39826 D ...
- 括号序列问题 uva 1626 poj 1141【区间dp】
首先考虑下面的问题:Code[VS] 3657 我们用以下规则定义一个合法的括号序列: (1)空序列是合法的 (2)假如S是一个合法的序列,则 (S) 和[S]都是合法的 (3)假如A 和 B 都是合 ...
- POJ 2378 Tree Cutting (DFS)
题目链接:http://poj.org/problem?id=2378 一棵树,去掉一个点剩下的每棵子树节点数不超过n/2.问有哪些这样的点,并按照顺序输出. dfs回溯即可. //#pragma c ...
- bzoj 3439 Kpm的MC密码(Trie+dfs序+主席树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3439 [题意] 给定若干串,问一个串的作为其后缀的给定串集合中的第k小. [思路] 如 ...
- POJ 1321-棋盘问题(DFS 递归)
POJ 1321-棋盘问题 K - DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- UVA 1508 - Equipment 状态压缩 枚举子集 dfs
UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...
随机推荐
- Odoo 开发者模式
这里我们以Odoo V9介绍下Odoo的开发者模式: 进入开发者模式: 登录odoo系统后,点击右上角登陆账号下拉菜单,选择About,然后在弹出菜单里点击Activate the developer ...
- session与缓存
分布式系统开发常见问题-1. session的复制与共享 2. 分布式缓存的设计 1. session的复制与共享 在web应用中,为了应对大规模访问,必须实现应用的集群部署.要实现集群部署主要需要实 ...
- OAB配置
OAB管理: http://blogs.technet.com/b/exchange_chs/archive/2013/01/31/exchange-server-2013-oab-managing- ...
- 10 款精美的 CSS3 全新特效
大家都知道,在网页制作时使用CSS技术,可以有效地对页面的布局.字体.颜色.背景和其它效果实现更加精确的控制.只要对相应的代码做一些简单的修改,就可以改变同一页面的不同部分,或者页数不同的网页的外观和 ...
- Android操作联系人 android开发教程
Android系统中的联系人也是通过ContentProvider来对外提供数据的,我们这里实现获取所有联系人.通过电话号码获取联系人.添加联系人.使用事务添加联系人. 获取所有联系人 1. Andr ...
- 编辑器TP
http://www.itshipin.com/blog/archives/category/php/thinkphp
- XCODE4.6从零开始添加视图
转自:http://www.cnblogs.com/luoxs/archive/2012/09/23/2698995.html 对于很多初学者来说,肯定希望自己尝试不用傻瓜的“Single View ...
- 定时导出Oracle数据表到文本文件的方法
该实例实现了通过windows定时任务来实现了将数据库中指定数据表数据导出为txt文本格式.其思路是通过可执行的bat文件去调用导出数据脚本,然后再在windows定时任务中调用该bat文件来实现.该 ...
- 动漫网站基于jquery的横向手风琴特效
今天给大家分享一款动漫网站基于jquery的横向手风琴特效.这款手风琴特效适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗.效果图如下: 在线预 ...
- 自学Linux命令的四种方法
自学Linux命令的四种方法 导读 童鞋们刚接触linux时,在学习过程中中会遇到不少问题,学习linux摸不着头脑,那么下面介绍四种linux的学习方法,特别适合新手. 方法一:终端"每日 ...