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 ...
随机推荐
- Storm(1)-centos7下安装单机版Strom
1.所需软件: jdk8.zookeeper.storm 2.安装zookeeper单机版 下载:http://zookeeper.apache.org/releases.html#download ...
- Docker & ASP.NET Core 教程
第一篇:把代码连接到容器 第二篇:定制Docker镜像 第三篇:发布镜像 第四篇:容器间的连接 第五篇: Docker & ASP.NET Core (5):Docker Compose AS ...
- sf03_杨辉三角go实现
package main import "fmt" /* 变量规范 全局变量以v_为前缀 函数形参以p_为前缀 函数内部变量,字母数字下划线等普通组合,其中函数返回值以out_为前 ...
- centos7安装并配置postgresql
安装并配置postgresql,参考以下两篇文章即可 https://www.postgresql.org/download/linux/redhat/ http://www.jianshu.com/ ...
- HAL库延时、SYCCNT与SYSTICK
HAL库驱动中,由于某些外设的驱动需要使用超时判断(比如I2C.SPI.SDIO等),需要精确延时(精度为1ms),使用的是SysTick,但是在操作系统里面,我们需要使用SysTick来提供系统时基 ...
- 前台js escape及后台C# Server.UrlEncode 对QueryString传参的含~!@#$%^&*等特殊字符的处理
通常情况下,我们在List列表页面,会包含Create,Edit,Delete等操作按钮, 而通常的处理是: Create按钮跳转到DataCreate.aspx Edit按钮跳转到DataEdit. ...
- [PHP]对象数组和普通数组总结
1.碰到前台将JSON格式数据传递到服务器后台,经php的json_decode函数转换成的数组由于为对象数组,php程序无法对数据进行正常处理的情况,为此需要开发一个PHP回调函数(objarray ...
- kindEditor完整认识 PHP上调用并上传图片说明
最近又重新捣鼓了下kindeditor,之前写的一篇文章http://hi.baidu.com/yanghbmail/blog/item/c681be015755160b1d9583e7.html感觉 ...
- springmvc+mybatis+sql server实现简单登录功能
一.源码: 1.Users.java package com.login.entity; import java.io.Serializable; public class Users impleme ...
- HDU 5407——CRB and Candies——————【逆元+是素数次方的数+公式】
CRB and Candies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...