[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.当然很多名字的意义其实有交 ...
随机推荐
- 编写使用SpringSecurity的JUnit测试提醒
近日在使用SpringSecurity的项目中发现一个小问题,就是在接口上加了@Secured标注限制调用接口权限时,某些JUnit无法正常调用了. 例如: @Secured(PrivilegeDAO ...
- MAT Memory Analyzer Tool 插件安装(图解)
MAT Memory Analyzer Tool 插件安装(图解) 原文地址 @author YHC 前段时间做了一个项目,Exception in thread "main" j ...
- UI-UIImageView和Image的区别
1.UIImageView图片视图控件 继承于UIView 用于显示图片在应用程序中 2.UIImage 是将真实图片文件转化为程序中的图片,然后3.UIImageView是Image的载体,负责显示 ...
- SQLite介绍、学习笔记、性能测试
SQLite介绍.学习笔记.性能测试 哪些人,哪些公司或软件在用SQLite: Nokia's Symbian,Mozilla,Abobe,Google,阿里旺旺,飞信,Chrome,FireFox可 ...
- noaman日志第一条:2015-1024;“Hello.World”
在南京的不知道第几个周末,一夜的煎熬终于活过来了.清早起来开通了自己的博客,第一条说说就记录开通博客这个事件.没有别的. 之后我会着重记录每天看书内容,以及所要编写的重要程序,一点一滴地积累希望能收获 ...
- C#实现监控网络流量
本文转载自:http://blog.csdn.net/zhanjianshinian/article/details/8177851 public partial class NetJiankongF ...
- bzoj4561: [JLoi2016]圆的异或并
Description 在平面直角坐标系中给定N个圆.已知这些圆两两没有交点,即两圆的关系只存在相离和包含.求这些圆的异或面 积并.异或面积并为:当一片区域在奇数个圆内则计算其面积,当一片区域在偶数个 ...
- 如何重装air
参考这里 很多年没有装过系统了,手贱用xxcleaner清理了下,好吧,我觉得只能重装了,直接贴过程吧 ,开机同时command+R,进入菜单 ,抹掉磁盘 ,重启,等在线更新(看各位运气了,网速好的话 ...
- Linux中文件描述符fd和文件指针flip的理解
转自:http://www.cnblogs.com/Jezze/archive/2011/12/23/2299861.html 简单归纳:fd只是一个整数,在open时产生.起到一个索引的作用,进程通 ...
- python使用xlrd模块读写Excel文件的方法
本文实例讲述了python使用xlrd模块读写Excel文件的方法.分享给大家供大家参考.具体如下: 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi ...