[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.当然很多名字的意义其实有交 ...
随机推荐
- javascript中的cookie,以及事件解析
Cookie: 它的意思是在本地的客户端的磁盘上以很小的文件形式保存数据,Cookie的处理原则上需要在服务器环境下运行,目前Chrome不可以在客户端操作Cookie,其他浏览器均可以, Coo ...
- HackerRank "Array and simple queries" !
The most interesting, flexible and juicy binary tree problem I have ever seen. I learnt it from here ...
- LitDB文章
阅读目录 1.LiteDB初步介绍 2.LiteDB使用基本案例 3.LiteDB的技术细节 4.资源其他 今天给大家介绍一个不错的小巧轻量级的NoSQL文件数据库LiteDB.本博客在2013年也介 ...
- bzojj1764: [Baltic2009]monument
Description 给一个p*q*r的立方体,它由p*q*r个1*1*1的小立方体构成.每个立方体要么被虫蛀,要么不被.现在郑爽要选出一个a*a*b的立方体(方向任意),使得它没有被虫蛀过,并且4 ...
- HDU3415
题目大意: 给出一个有N个数字(-1000..1000,N<=10^5)的环状序列,找出一个长度不大于k的连续子序列,使其和最大. 分析: 我们可以将环状序列从某处切开,变成一行,然后复制前n- ...
- BeX5平台简明部署过程
http://wex5.com/cn/concise-deployment/ BeX5平台简明部署过程 该文章主要介绍BeX5平台开发完成后,资源部署至正式环境的过程. 一. 获取BeX5企业快速开发 ...
- 免安装Oracle客户端使用PLSQL Developer 7/8 连接Oracle10/11g
众所周知,Oralce的客户端几百兆太大,网上也有许多DIR的处理.这里的处理使用官方提供ORALCE工具包Instant Client Package! 下载地址:http://www.oracle ...
- 86. Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 黄聪:MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 解决方法(转)
转自:http://www.cnblogs.com/susuyu/archive/2013/05/28/3104249.html 环境:linux,mysql5.5.21 错误:Host is blo ...
- Platform Invoke
PInvoke 允许managed code 来调用在DLL中实施的unmanged function. Platform invoke relies on metadata to locate ex ...