hdu1 247 Hat’s Words(字典树)
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16970 Accepted Submission(s):
6098
concatenation of exactly two other words in the dictionary.
You are to find
all the hat’s words in a dictionary.
one per line, in alphabetical order. There will be no more than 50,000
words.
Only one case.
line, in alphabetical order.
ahat
hat
hatword
hziee
word
hatword
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std; typedef struct node
{
int f;
struct node *nxt[];
}Trie; void insert(Trie *root, char *str)//字典树的更新操作
{
if (root == NULL || *str=='\0') return;
Trie *p = root;
while (*str != '\0')
{
if (p->nxt[*str - 'a'] == NULL)//当前结点为空,就在其下开26个空间
{
Trie *q= (Trie *)malloc(sizeof(Trie));//开辟内存空间
q->f = ;
for (int i = ; i < ; i++)
{
q->nxt[i] = NULL;
}
p->nxt[*str - 'a'] = q;
p = p->nxt[*str - 'a'];//使p指向新开辟的内存空间
}
else p = p->nxt[*str - 'a'];
str += ;
}
p->f = ;//在单词末尾标记
} int find(Trie *root, char *str)
{
Trie *p = root;
while (*str != '\0')
{
if (p->nxt[*str - 'a'] == NULL) return ;
p = p->nxt[*str - 'a'];
str += ;
}
return p->f;
} char cstr[][];
int main()
{
char c1[], c2[];
Trie *root = (Trie*)malloc(sizeof(Trie));//开辟根节点内存空间
root->f = ;
int i;
for (i = ; i < ; i++)
{
root->nxt[i] = NULL;
}
int cnt = ;
while (scanf("%s", cstr[cnt]) != EOF)
{
insert(root, cstr[cnt]);
cnt++;
}
memset(c1, '\0', sizeof(c1));//初始化c1,c2
memset(c2, '\0', sizeof(c2));
int j;
for (i = ; i < cnt; i++)
{
for (j = ; j < strlen(cstr[i]); j++)
{
strcpy(c1, cstr[i]);
c1[j] = '\0';//将cstr中0~j字符串复制的c1
strcpy(c2, cstr[i] + j);//将cstr中j~最后字符串复制到c2
if (find(root, c1) && find(root, c2))
{
printf("%s\n", cstr[i]);
break;
}
}
}
return ;
}
hdu1 247 Hat’s Words(字典树)的更多相关文章
- hdu 1247 Hat’s Words(字典树)
Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDU 1247 - Hat’s Words - [字典树水题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...
- Hat’s Words(字典树)
Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly tw ...
- hdoj 1247 Hat’s Words(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的 ...
- hdu 1247:Hat’s Words(字典树,经典题)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 1247 Hat’s Words(字典树)题解
题意:给一个字符串集,要你给出n个字符串s,使s能被所给字符串集中的两个相加所得(ahat=a+hat) 思路:简单字典树题,注意查询的时候要判断所指next是否为NULL,否则会RE非法访问. 代价 ...
- HDU 1247 Hat’s Words (字典树 && map)
分析:一開始是用递归做的,没做出来.于是就换了如今的数组.即,把每个输入的字符串都存入二维数组中,然后创建字典树.输入和创建完成后,開始查找. 事实上一開始就读错题目了,题目要求字符串是由其它两个输入 ...
- Hat’s Words(字典树的运用)
个人心得:通过这道题,对于树的运用又加深了一点,字典树有着他独特的特点,那个指针的一直转换着实让我好生想半天, 不得不佩服这些发明算法人的大脑. 这题的解决方法还是从网上找到的,还好算法是自己实现得, ...
- HDU 1247 Hat’s Words(字典树变形)
题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...
随机推荐
- MariaDB主从复制搭建
我的github 安装MySQL服务器 安装数据库 yum install -y mariadb-server 初始化数据库 mysql_secure_installation #MySql初始化脚本 ...
- SDN原理 OpenFlow协议 -4
通道 Channel 在前面的OpenFlow的内容中,我们提到了在交换层所采用的流表是控制层的Controller下发的,那么Controller是如何下发流表的呢?中间经过了哪些的流程和步骤?控制 ...
- hdu 2841 Visible Trees 容斥原理
Visible Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pr ...
- 安装lua 环境
lua下载地址:https://www.lua.org/download.html curl -R -O http://www.lua.org/ftp/lua-5.3.5.tar.gz tar zxf ...
- [原][osgearth]earth文件加载道路一初步看见模型道路
时间是2017年2月5日17:16:32 由于OE2.9还没有发布,但是我又急于使用OE的道路. 所以,我先编译了正在github上调试中的OE2.9 github网址是:https://github ...
- 代码注释,神兽护体,代码无bug
/** * * ━━━━━━神兽出没━━━━━━ * ┏┓ ┏┓ * ┏┛┻━━━┛┻┓ * ┃ ┃ * ┃ ━ ┃ * ┃ ┳┛ ┗┳ ┃ * ┃ ┃ * ┃ ┻ ┃ * ┃ ┃ * ┗━┓ ┏━┛ ...
- iTerm2的设置和Zsh.
很好的说明文: https://xiaozhou.net/learn-the-command-line-iterm-and-zsh-2017-06-23.html iTerm2是Mac os用户使用的 ...
- 使用actioncable做的notification(GoRails教学,2课)
GoRails视频系列: 1. 用actioncable建立Notifications 2. 见博客: 3. 非认证/登陆user不能使用actioncable 用ActionCable 建立 ...
- Android手机无线adb
1.首先电脑,手机通过数据线链接电脑,然后通过adb devices 查看到已连接 2.输入:adb tcpip 5555 3.输入:adb connect 222.222.221.137:5555 ...
- linux三尖刀
序 我们都知道,一个可执行程序的基本的生命过程是如此的: (编辑)源文件--->(编译)目标文件--->(链接)可执行文件--->(调试排错)稳定执行 所以,在这个过程中,我们很容易 ...