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轮游戏,最后胜利的人是谁. 思路: 很 ...
随机推荐
- EXTJS Ajax请求中文乱码
设置actionMethods, read为post proxy: { type: 'ajax', url: '../Dictionary/query', actionMethods: { read: ...
- jquery对url中的中文解码
项目中要实现一个select选择器选择后跳转url,并保存selected的值. url是用get来传递参数,所以考虑加载新页面时,读取参数值,并赋值到select中. 但是由于url的参数使用的是中 ...
- ubuntu14.04安装django
1) sudo apt-get install python-pip#安装pip 2) pip install Django==1.8.1
- windows下安装Apache 64bit
文件下载:http://pan.baidu.com/s/1c0oDjFE 一.Apache的安装 http://www.blogjava.net/greatyuqing/archive/2013/02 ...
- DNS基础及域名系统架构
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- Greedy:Subsequence(POJ 3061)
和最短序列 题目大意:找出一个序列中比至少和S相等的最短子序列(连续的) 本来这道题可以二分法来做复杂度O(NlogN),也可以用一个类似于游标卡尺的方法O(N)来做 先来讲游标卡尺法: 因为子序 ...
- ssh自动登陆
突然碰到有人问ssh再传输密钥时候能不手动输入密码,由于没有碰到过这种情况,所以查了一下发现可以用sshpass做到. sshpass [参数] ssh命令: 参数: -p password #将参 ...
- 会话控制(session、cookie)
1.session(1)session存储在服务器的(2)session每个人存一份(3)session有默认的过期时间(4)session里面可以存储任意类型的数据安全,对服务造成压力用法:1.当一 ...
- osg设置相机参数,包括初始位置
严重注意!!!以下设置必须在viewer.realize();之后,否则不起作用!!!! 设置相机的位置,可以通过CameraManipulator(一般是osgGA::TrackballManipu ...
- 晨跑(bzoj 1877)
Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一张学校附近的地图,这张地图中包含N个十 ...