#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 聪明的输入法(字典树)的更多相关文章

  1. 字符串hash与字典树

    title: 字符串hash与字典树 date: 2018-08-01 22:05:29 tags: acm 算法 字符串 概述 这篇主要是关于字符串里的 字符串hash 和 字符串字典树,,两个都是 ...

  2. BNU 27847——Cellphone Typing——————【字典树】

    Cellphone Typing Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on UVA. Origi ...

  3. Trie|如何用字典树实现搜索引擎的关键词提示功能

    Trie字典树 Trie字典树又称前缀树,顾名思义,是查询前缀匹配的一种树形数据结构 可以分为插入(创建) 和 查询两部分.参考地址极客时间 下图为插入字符串的过程: 创建完成后,每个字符串最后一个字 ...

  4. ACM之路(15)—— 字典树入门练习

    刷的一套字典树的题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=120748#overview 个人喜欢指针的字典树写法,但是大力 ...

  5. 第三十篇 玩转数据结构——字典树(Trie)

          1.. Trie通常被称为"字典树"或"前缀树" Trie的形象化描述如下图: Trie的优势和适用场景 2.. 实现Trie 实现Trie的业务无 ...

  6. 018(Phone List)(字典树)

    题目:http://ybt.ssoier.cn:8088/problem_show.php?pid=1471 题目思路: 这不就是一个超级明显的字典树嘛 字典树,又称单词查找树,Trie树,是一种树形 ...

  7. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  8. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  9. 字典树+博弈 CF 455B A Lot of Games(接龙游戏)

    题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...

随机推荐

  1. 1.3---字符串重新排列后是否能够变成另一个字符串(CC150)

    import java.util.*; public class Same { public boolean checkSam(String str1, String str2) { // write ...

  2. 如何区分SNAT和DNAT

    从定义来讲它们一个是源地址转换,一个是目标地址转换.都是地址转换的功能,将私有地址转换为公网地址.要区分这两个功能可以简单的由连接发起者是谁来区分:       内部地址要访问公网上的服务时(如web ...

  3. Node.JS初识

    对Node.JS的认识 1.Node 是一个服务器端 JavaScript 解释器: 2.Node 的目标是帮助程序员构建高度可伸缩的应用程序,编写能够处理数万条同时连接到一个物理机的连接代码.处理高 ...

  4. The Imitation Game

    <The Imitation Game>是一部非常好的电影,讲述了人工智能之父——阿兰图灵的传奇一生. 重点讲述了他通过破译德国的通讯密码缩短了二战的持续时间,因而拯救了无数生命的伟大事迹 ...

  5. nginx 原理&知识

    2015年6月4日 17:04:20 星期四 发现两个关于nginx原理的系列文章, 非常好 http://blog.sina.com.cn/s/blog_6d579ff40100wi7p.html ...

  6. 5. javacript高级程序设计-引用类型

    1. 引用类型 1.1 Object类型 创建Object类型有两种方式: 使用new操作符后跟Object构造函数 var person =new Object(); 字符量表示法 var pers ...

  7. HDU 1000 & HDU1001 & 字符串连接

    A + B Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. 2106 Problem F Shuffling Along 中石油-未提交-->已提交

    题目描述 Most of you have played card games (and if you haven’t, why not???) in which the deck of cards ...

  9. Mysql 基础1

    Mysql int 整型float 小数double 小数varchar(20) 字符串bit 布尔型数据datetime 日期时间类型text 长文本 money 存货币image 存二进制数据 数 ...

  10. mybatis 获取自增ID

    在Mybatis Mapper文件中添加属性“useGeneratedKeys”和“keyProperty”,其中keyProperty是Java对象的属性名,而不是表格的字段名. <inser ...