HDU1075 - What Are You Talking About(Trie树)
题目大意
给定一些火星文单词以及对应的英语单词,然后给你一些火星文,要求你翻译成对应的英文
题解
第一次写Trie树!
把所有火星文单词插入到Trie树中,并且每个火星文单词结尾的节点记录相应英文单词的位置,然后进行查询即可~~~~
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
const int maxnode = 3000005;
const int sigma_size = 26;
char dic[maxnode][15];
struct Trie
{
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
Trie(){sz=1;memset(ch[0],0,sizeof(ch[0]));val[0]=-1;}
int idx(char c){return c-'a';}
void insert(char *s,int v)
{
int u=0,n=strlen(s);
for(int i=0;i<n;i++)
{
int c=idx(s[i]);
if(!ch[u][c])
{
memset(ch[sz],0,sizeof(ch[sz]));
val[sz]=-1;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]=v;
}
int find(const char *s)
{
int u=0;
for(int i=0;s[i]!='\0';i++)
{
int c=idx(s[i]);
if(!ch[u][c])
return -1;
u=ch[u][c];
}
return val[u];
} };
Trie trie;
int main()
{
char word[15];
int cnt=0;
scanf("%s",word);
while(scanf("%s",word)!=EOF)
{
if(word[0]=='E') break;
strcpy(dic[cnt],word);
scanf("%s",word);
trie.insert(word,cnt);
cnt++;
}
scanf("%s",word);
getchar();
char ch;
int len=0;
while(scanf("%c",&ch)!=EOF&&ch!='E')
{
if(islower(ch))
word[len++]=ch;
else
{
word[len]='\0';
int pos=trie.find(word);
if(pos>=0)
printf("%s",dic[pos]);
else
if(word[0]!='\0')
printf("%s",word);
printf("%c",ch);
len=0;
}
}
return 0;
}
HDU1075 - What Are You Talking About(Trie树)的更多相关文章
- Trie树入门及训练
什么叫Trie树? Trie树即字典树. 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本 ...
- 【字符串算法】字典树(Trie树)
什么是字典树 基本概念 字典树,又称为单词查找树或Tire树,是一种树形结构,它是一种哈希树的变种,用于存储字符串及其相关信息. 基本性质 1.根节点不包含字符,除根节点外的每一个子节点都包含一个字符 ...
- 基于trie树做一个ac自动机
基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...
- 基于trie树的具有联想功能的文本编辑器
之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...
- hihocoder-1014 Trie树
hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...
- 洛谷P2412 查单词 [trie树 RMQ]
题目背景 滚粗了的HansBug在收拾旧英语书,然而他发现了什么奇妙的东西. 题目描述 udp2.T3如果遇到相同的字符串,输出后面的 蒟蒻HansBug在一本英语书里面找到了一个单词表,包含N个单词 ...
- 通过trie树实现单词自动补全
/** * 实现单词补全功能 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #incl ...
- #1014 Trie树
本题主要是求构造一棵Trie树,即词典树用于统计单词. C#代码如下: using System; using System.Collections.Generic; using System.Lin ...
- Trie树-字典查找
描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一本词典,于是小Hi就向小Ho提出了那个经典的问题: ...
随机推荐
- Linux_Struct file()结构体
struct file结构体定义在/linux/include/linux/fs.h(Linux 2.6.11内核)中,其原型是:struct file { /* * f ...
- We7——很有意思的一个开源CMS
目前做门户.做网站,基本上都需要用到一个系统,那就是CMS内容管理系统:现在开源产品有很多,笔者也是从事这个行业的,国内的各大CMS提供商基本上都试用过,今天向大家推荐一款很有意思的产品——We7CM ...
- SQL语句中使用条件逻辑
select name, sal, case when sal >= 4000 then 'Good' when sal <= 2000 then 'Bad' else 'Ok' end ...
- UGUI-组件
2015-06-22 UGUI 组件 Canvas 画布 The Canvas component represents the abstract space in which the UI is l ...
- Jquer学习
1:什么是Jquery 2:jquery的例子 3:后记
- 下拉框点链接js
$("#input_text").click(function(){ $("#input_fonts").show(); }); $("#input_ ...
- javascript-代码复用模式
代码复用模式 1)使用原型继承 函数对象中自身声明的方法和属性与prototype声名的对象有什么不同: 自身声明的方法和属性是静态的, 也就是说你在声明后,试图再去增 ...
- php smarty foreach循环注意
在template中,要注意{foreach from=$arr item=value}其中的value不需要$美元符号
- 怎么修改tomcat默认访问首页
一般情况下安装好tomcat之后我们的默认访问首页是index了,但我们如果要修改或增加一个默认首页,我们可参考下面办法来解决. 通过 ip:port 访问到的是 tomcat 的管理页面,其他常规部 ...
- jQuery 插件模板
1.为每一个DOM对象创建一个插件对象 模板定义: (function($) { $.pluginName = function(element, options) { var defaults = ...