hdu-1247 Hat’s Words---字典树模板
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1247
题目大意:
给出一些单词,以EOF结束,看其中哪一个单词可以由其他两个单词组成,将其输出
解题思路:
将所有单词存入字典树中,每个单词拆成两部分查询是不是字典树中的单词。
此处是查询是不是单词,需要加单词标记数组,在每个单词最后一位的末尾那个节点标记true
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<algorithm>
#include<vector>
#include<sstream>
#define lowbot(i) (i&(-i))
using namespace std; const int maxn = 1e6 + ;
int tree[maxn][];
//字典树tree[u][v]表示编号为u的节点的下一个字母为v连接的节点的编号
int idx(char c){ return c - 'a'; }//可以写成宏定义
int tot = ;//根节点编号为1
bool is_word[maxn];//单词结束标记
void Insert(char s[], int u)//u表示根节点
//插入字符串s
{
for(int i = ; s[i]; i++)
{
int c = idx(s[i]);
if(!tree[u][c])
tree[u][c] = ++tot;
u = tree[u][c];
}
is_word [u] = true; //查询单词的时候需要标记最后一个节点的地方是单词
} bool Find(char s[], int u)
//查询s是否是前缀
{
for(int i = ; s[i]; i++)
{
int c = idx(s[i]);
if(!tree[u][c])
return false;
u = tree[u][c];
}
//return true;
return is_word[u]; //查询单词的时候,需要返回当前是不是单词结束标志
}
char s[][], s1[];
int main()
{
int n = ;
while(scanf("%s", s[n]) != EOF)Insert(s[n++], ); for(int i = ; i < n; i++)
{
for(int j = ; s[i][j]; j++)//从下标1开始,从0开始的话s1就变成空串了
{
memcpy(s1, s[i], sizeof(s[i]));
s1[j] = ;
//分割成两个字符串: s1和(s[i] + j)
//cout<<s1<<" "<<(s[i] + j)<<endl;
if(Find(s1, ) && Find(s[i] + j, ))
{
cout<<s[i]<<endl;
break;
}
}
}
return ;
}
hdu-1247 Hat’s Words---字典树模板的更多相关文章
- HDU 1247 - Hat’s Words - [字典树水题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...
- hdu 1247 Hat’s Words(字典树)
Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- hdoj 1247 Hat’s Words(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的 ...
- HDU 1251 统计难题(字典树模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...
- Hdu 1247 Hat's Words(Trie树)
Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- 字典树模板题(统计难题 HDU - 1251)
https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...
- HDU - 1251 字典树模板题
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部 ...
- 字典树模板 HDU - 1251
题意: 给一些单词,换行键后,查找以后输入的单词作为前缀的话们在之前出现过几次. 思路: 字典树模板----像查字典的顺序一样 #include<string> #include<s ...
- HDU 4757 Tree 可持久化字典树
Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4757 Des ...
随机推荐
- 2.3 Rust函数
2.3 函数 [root@itoracle src]# cargo new functions Created binary (application) `functions` package [ro ...
- 15-----BBS论坛
BBS论坛(十五) 15.1.登录界面完成 (1)front/signbase.html {% from 'common/_macros.html' import static %} <!DOC ...
- Office 2016 word无法粘贴(Ctrl + V)
最近下载了一个 Office 2016 专业版 使用,发现 word 无法使用 Ctrl + V 粘贴东西,由于经常需要复制粘贴东西,无法粘贴影响很大 查了很多资料,尝试过很多的方法,终于发现问题的所 ...
- table size script :
I think Jonathan Lewis has explained the algorithm before, but it's alsosomething that we have inves ...
- C语言判别输入的东东
梗概:现在很多用C语言写出来的作业,都是用户输入后,电脑对应操作的.其实这样有没有漏洞呢? 这样的管理系统,相信大家也不陌生,我们这里不是谈它的功能和怎样实现..我们就谈谈最后一行.[输入序号].其实 ...
- JavaScript高级程序设计--对象创建的三种方法
创建对象的三种方法: 1.工厂模式 工厂模式是软件工程领域广为人知的设计模式,这种模式抽象了创建具体对象的过程.下面是使用工厂函数创建对象的的一个例子. 2.构造函数: 从上面的例子中,我们看到构造函 ...
- mongodb3集群搭建
三台服务器:先设置hosts 10.0.0.231 node1 10.0.0.232 node2 10.0.0.233 node3 1:下载 mongodb-linux-x86_64-rhel70-3 ...
- Windows Server 2012 R2
Windows Server 2012 R2 历史上的Server有2003 server, 2008 server, 2012 server windows server 2012 r2对计算机的消 ...
- [巩固C#] 一、特性是什么东东
阅读目录 关闭 前言 特性是什么? 那么什么是“元数据”? 特性到底是什么? 我们自定义一个特性玩玩 什么是命名参数? 我们来继续要看看AttributeUsage(这个描... 自定义特性可 ...
- Win2D 官方文章系列翻译 - 像素格式
本文为个人博客备份文章,原文地址: http://validvoid.net/win2d-pixel-formats/ DirectXPixelFormat 枚举 包含了 Direct3D 和 DXG ...