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轮游戏,最后胜利的人是谁. 思路: 很 ...
随机推荐
- fastx_toolkit软件使用说明
高通量测序数据下机后的原始fastq文件,包含4行,其中一行为质量值,另外一行则为对应序列,我们都了解高通量的数据处理首先要进行质量控制,这些过程包括去接头.过滤低质量reads.去除低质量的3'和5 ...
- centos 无线网卡安装,网卡rtl8188ee
驱动: http://www.realtek.com.tw/downloads/downloadsView.aspx?Langid=1&PNid=48&PFid=48&Leve ...
- eclipes的Spring注解SequenceGenerator(name="sequenceGenerator")报错的解决方式
eclipes的Spring注解SequenceGenerator(name="sequenceGenerator")报错的解决方式 右键项目打开Properties—>JA ...
- non
I p(I q){r p(c((q>9?q-p(q/10):q)+'0')),q*10; }
- 转:JQuery选择器
选择器是jQuery最基础的东西,本文中列举的选择器基本上囊括了所有的jQuery选择器,也许各位通过这篇文章能够加深对jQuery选择器的理 解,它们本身用法就非常简单,我更希望的是它能够提升个人编 ...
- Buffer和Cache的区别
- IIS 处理请求 原理
有时候我们会发现当我们访问一个IIS网站时,使用网址可以正常访问,但是使用IP却不行,这是什么原因呢? 原来IIS可以使用一个IP地址和端口绑定多个网站,这些网站的IP地址与端口都一样,因此在客户端或 ...
- 一个Try多个Catch需要注意的事项
一个程序包含一个try块和两个catch块,两个catch子句都有能力捕捉一个try块发出的异常,若两个catch子句次序不同时程序结果会发生变化吗? 一个try块后有两个catch块,这很正常,因为 ...
- Shell 脚本 ,, 根据进程号退出 从而关机
#!/bin/bash fun(){ ps -ef | grep "$1" | awk '{print $2 $8}' > /usr/file while read line ...
- Starting MySQL.The server quit without updating PID file (xxxx.pid).[FAILED]
mysql无法正常启动,查看日志报如下异常 --07T01::.929615Z [ERROR] Fatal error: Please read "Security" sectio ...