[HackerCup Round1 2] Autocomplete (Trie)
题目链接:https://www.facebook.com/hackercup/problems.php?pid=313229895540583&round=344496159068801
题目大意:自己看去(其实我也说不清)
裸的Trie树,直接看是否存在必须插入的节点。
代码写的太挫了。。将就着看吧。。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cstdlib>
using namespace std;
#define CHARSET 26 const int MAX_NODE = ; struct trieNode{
int ch[CHARSET];
};
trieNode trie[MAX_NODE];
int ptr;
int ans; int T,N;
char str[MAX_NODE]; void init(){
ptr = ans = ;
for(int i=;i<CHARSET;i++){
trie[].ch[i] = -;
}
} int newNode(){
ptr++;
for(int i=;i<CHARSET;i++){
trie[ptr].ch[i] = -;
}
return ptr;
} void insert(const char* str){
bool hasAdded = false;
int len = strlen(str);
int rt = ;
for(int i=;i<len;i++){
int id = str[i] - 'a';
if( trie[rt].ch[id]==- ){
trie[rt].ch[id] = newNode();
if( !hasAdded ){
ans = ans + i + ;
hasAdded = true;
// printf("+%d\n",i+1);
}
}
rt = trie[rt].ch[id];
}
if(!hasAdded){
ans = ans + len;
// printf("+%d\n",len);
}
} int main(){
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
scanf("%d",&T);
for(int cases = ;cases <= T ; cases++){
scanf("%d",&N);
init();
for(int i=;i<N;i++){
scanf("%s",str);
insert(str);
}
printf("Case #%d: %d\n",cases,ans);
}
return ;
}
[HackerCup Round1 2] Autocomplete (Trie)的更多相关文章
- [HackerCup Round1 3] Winning at Sports (动态规划)
题目链接:https://www.facebook.com/hackercup/problems.php?pid=688426044611322&round=344496159068801 题 ...
- HDU 5687 字典树入门
Problem C Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- 【ZJOI2017 Round1练习】D4T2 trie(贪心,状压DP)
题意:现在 Matej 手上有 N 个英文小写字母组成的单词, 他想知道,如果将这 N 个单词中的字母分别进行重新排列,形成的字母树的节点数最少是多少. n<=16,len[i]<=100 ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 数据结构《16》----自动补齐实现《一》----Trie 树
1. 简述 Trie 树是一种高效的字符串查找的数据结构.可用于搜索引擎中词频统计,自动补齐等. 在一个Trie 树中插入.查找某个单词的时间复杂度是 O(len), len是单词的长度. 如果采用平 ...
- trie树---(插入、删除、查询字符串)
HDU 5687 Problem Description 度熊手上有一本神奇的字典,你可以在它里面做如下三个操作: 1.insert : 往神奇字典中插入一个单词 2.delete: 在神奇字 ...
- [LeetCode] Design Search Autocomplete System 设计搜索自动补全系统
Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...
- 字典树 trie
Trie树 Trie树,就是字母树.Trie树是多叉树,每个节点为一个字母.其根节点为象征节点(就是说没有含义,但是存在这个节点),从根节点开始建立,每个节点至多为26个子节点(不要我说 ...
- Trie(前缀树/字典树)及其应用
Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...
随机推荐
- css字体文件
├── glyphicons-halflings-regular.eot├── glyphicons-halflings-regular.svg├── glyphicons-halflings-reg ...
- 【linux】linux如何进入单人维护模式修改root密码
- LwIP情景示例
1. 你使用UDP作为探测包,但被探测的主机不在网络上. 在发送UDP packet之前,LwIP要将其保存下来(分配一个RAM类型的pbuf),并首先发送ARP Request,但得不到回应.如果你 ...
- 51nod 1297 管理二叉树
一个初始为空的二叉搜索树T,以及1到N的一个排列P: {a1, a2, ..., aN}.我们向这个二叉搜索树T添加这些数,从a1开始, 接下来是 a2, ..., 以aN结束.在每一个添加操作后,输 ...
- 源码安装extundelete以及对遇到问题的解决
软件下载:http://sourceforge.net/projects/extundelete/ 1.在安装extundelete包./configure时遇到configure: error: C ...
- IO操作 第一篇 学习(转载)
问题8:如何使用通配符搜索指定目录内的所有文件: 解决方案: 使用DirectoryInfo.GetFiles方法的重载版本,它可以接受一个过滤表达式,返回FileInfo数组,另外它的参数还可以指定 ...
- Exception error message with incorrect line number
In Release mode the number in front of the exception is NOT the line of code. Instead it's an offset ...
- PLSQL_基础系列04_时间间隔INTERVAL(案例)
2014-12-08 Created By BaoXinjian
- RMAN_Oracle RMAN的常用Configure配置
2014-12-09 Created By BaoXinjian
- All_从PO - INV - AP - SLA - GL重要数据表和接口程式(概念)
2014-07-09 Created By BaoXinjian