#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. centos PIL 安装

    http://itekblog.com/centos-6-x-install-pil-python-imaging-library-tutorial/

  2. Git – Fast Forward 和 no fast foward

    Git 很是强大,在体验过rebase的华丽之后,再次发现之前在TFS上遇到的问题一下都有解了.但也印证了Git深入并非易事.这篇就谈下一个容易迷糊的概念:Fast forward. Fast-For ...

  3. ios 汉字字符串数组拼音排序

    ios没有提供简单的汉字拼音排序方法,在网上看到了oc方法,这里写以下对应的swift方法 var stringCompareBlock: (String,String)->Bool = { ( ...

  4. java中方法参数的一些总结(1)

    1.问题说明        在C++中,函数调用时有传值调用和传址调用两种方式,但在Java中只有传值调用一种方式.Java中的方法参数为那几种基本数据类型的情况跟C++中一样,传入的只是变量的拷贝. ...

  5. yum Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again

    今天使用yum时出现如上问题,解决办法: 1.编辑:/etc/yum.repos.d/epel.repo 将baseurl的注释取消, mirrorlist注释掉,如图:

  6. JS 保留两位小数问题收集

    1.使用四舍五入的方法,保留小数点后的两位小数: toFixed里面的参数表示保留的小数的位数,范围是0-20,超过20位就会报错了 <script> var num=22.127456; ...

  7. Ajax如何使用Session

    在Ajax中有时会使用到Session,在aspx.cs文件这样获取: string name = Session["name"]; 但是在Ajax中就不能这样获取Session, ...

  8. UICollectionView cellForItemAtIndexPath 方法不走

    在storyboard 中 UICollectionView cellForItemAtIndexPath not called 被坑了好久,各种问题点查找,终于解决了 解决办法: self.auto ...

  9. C#复制、粘贴文本信息到剪贴板

    复制:private void button1_Click(object sender, System.EventArgs e) { // Takes the selected text from a ...

  10. tableview详细介绍

    tableview详细介绍: https://www.baidu.com/link?url=MU5a5om66vYEKAcnvmXCeCwMGetezW5o2X11OUnwN7-fb_jWPx6xyv ...