AOJ673 聪明的输入法(字典树)
#include<cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define num(x) x-'a';
#define MAX 1000000
struct Trie{
int next[];
int count;
int prefix;//记录以此为前缀串的数量
}tree[MAX];
char suffix[];//后缀
int f,length;
int dfs(int depth,int node){//查找出现次数最多的串,返回以此出发串出现频率最大值
int best=,sel;
if(tree[node].count&&(tree[node].count>f)){
f=tree[node].count;
length=depth;
best=f;
}
for(int i=;i<;i++){
int j=tree[node].next[i];
if(j&&tree[j].prefix>f){//摆脱TLE的重要剪枝
int t=dfs(depth+,tree[node].next[i]);
if(best<t){
best=t;
sel=i;
}
}
}
if(best==f){
suffix[depth]=(char)(sel+'a');
}
return best;
}
int insert(char *s){
int len=strlen(s),node=;
static int next=;
if(next==){
memset(&tree[],,sizeof(Trie));
}
for(int i=;i<len;i++){
int c=num(s[i]);
if(!tree[node].next[c]){
memset(&tree[++next],,sizeof(Trie));
tree[node].next[c]=next;
}
node=tree[node].next[c];
tree[node].prefix++;
}
return ++tree[node].count;
}
void search(char *s){
int node=,len=strlen(s);
for(int i=;i<len;i++){
int c=num(s[i]);
if(!tree[node].next[c]){
printf("%s\n",s);
return;
}
node=tree[node].next[c];
}
printf("%s",s);
length=f=;
dfs(,node);//深搜找后缀
for(int i=;i<length;i++){
printf("%c",suffix[i]);
}
printf("\n");
}
int main(){
int t;
char str[];
scanf("%d",&t);
while(t--){
scanf("%s",str);
search(str);
insert(str);
}
return ;
}
AOJ673 聪明的输入法(字典树)的更多相关文章
- 字符串hash与字典树
title: 字符串hash与字典树 date: 2018-08-01 22:05:29 tags: acm 算法 字符串 概述 这篇主要是关于字符串里的 字符串hash 和 字符串字典树,,两个都是 ...
- BNU 27847——Cellphone Typing——————【字典树】
Cellphone Typing Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on UVA. Origi ...
- Trie|如何用字典树实现搜索引擎的关键词提示功能
Trie字典树 Trie字典树又称前缀树,顾名思义,是查询前缀匹配的一种树形数据结构 可以分为插入(创建) 和 查询两部分.参考地址极客时间 下图为插入字符串的过程: 创建完成后,每个字符串最后一个字 ...
- ACM之路(15)—— 字典树入门练习
刷的一套字典树的题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=120748#overview 个人喜欢指针的字典树写法,但是大力 ...
- 第三十篇 玩转数据结构——字典树(Trie)
1.. Trie通常被称为"字典树"或"前缀树" Trie的形象化描述如下图: Trie的优势和适用场景 2.. 实现Trie 实现Trie的业务无 ...
- 018(Phone List)(字典树)
题目:http://ybt.ssoier.cn:8088/problem_show.php?pid=1471 题目思路: 这不就是一个超级明显的字典树嘛 字典树,又称单词查找树,Trie树,是一种树形 ...
- 萌新笔记——用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轮游戏,最后胜利的人是谁. 思路: 很 ...
随机推荐
- 39 网络相关函数(七)——live555源码阅读(四)网络
39 网络相关函数(七)——live555源码阅读(四)网络 39 网络相关函数(七)——live555源码阅读(四)网络 简介 14)readSocket从套接口读取数据 recv/recvfrom ...
- Python 命名空间
通俗的来说,Python中所谓的命名空间可以理解为一个容器.在这个容器中可以装许多标识符.不同容器中的同名的标识符是不会相互冲突的.理解python的命名空间需要掌握三条规则: 第一,赋值(包括显式赋 ...
- dict.items vs six.iteritems
python2里面,dict.items返回的是数组,six.iteritems(dict)则返回生成器. 意味着,dict很大的时候,后者不占用内存. >>> import six ...
- An exception occurred during a WebClient request
System.Net.WebException was caught HResult=-2146233079 Message=An exception occurred during a WebCli ...
- 转: Oracle表空间查询
1.查询数据库中的表空间名称 1)查询所有表空间 select tablespace_name from dba_tablespaces; select tablespace_name from us ...
- c#.net对excel的操作——创建一个excel报表两个sheet就是2个表分别添加内容
添加引用:Microsoft.Office.Interop.Excel //创建excel对象,就是实例化一个excel对象 Application excel=new Appl ...
- C#之数据分页
方法一:临时datatable 创建临时表,临时变量 DataTable dt = null; //临时表 ; //总分页数 ; //当前页数 ; //每页的数量 加载数据到临时表,该方法测试放到了窗 ...
- C#之键值对
1.初始化一个键值对 //初始化定义一个键值对,注意最后的括号 Dictionary<int, string> dic = new Dictionary<int, string> ...
- poj 1700
http://poj.org/problem?id=1700 题目大意就是一条船,有N个人需要过河,求N个人最短过河的时间 #include <stdio.h> int main() { ...
- hdu2196
基本的树形dp,需要dfs三次,第一次求每个点最远的后代,第二次和第三次每个点的孩子分别从左到右和从右到左遍历. #include <cstdio> #include <vector ...