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 ...
随机推荐
- shell 判断是否是目录
创建一个文件和一个文件夹 touch sss mkdir d test.sh #!/bin/bash echo "enter the name:" read filename if ...
- Win7SDK
1.ISO下载地址: http://www.microsoft.com/en-us/download/details.aspx?id=8442 2.可供下载的 版本有3个,网上搜到的解释: GRMSD ...
- Java Spring-传统AOP开发
2017-11-10 17:25:48 Spring中通知Advice类型(增强代码): 前置通知,org.springframework.aop.MethodBeforeAdvice:方法前 后置通 ...
- 跨站脚本功攻击,xss,一个简单的例子让你知道什么是xss攻击
跨站脚本功攻击,xss,一个简单的例子让你知道什么是xss攻击 一.总结 一句话总结:比如用户留言功能,用户留言中写的是网页可执行代码,例如js代码,然后这段代码在可看到这段留言的不同一户的显示上就会 ...
- WPF特效和例子
https://www.cnblogs.com/AaronYang/p/4710428.html
- C# 字符串与字节数组相互转换
https://www.cnblogs.com/xiaoqingshe/p/5882601.html
- git 沙河游戏节点图, 自由沙盒模拟git, 各类交互git命令
git学习练习总资源链接: https://try.github.io/ (练习已通,有document) 本沙盒游戏教学:https://learngitbranching.js.org/?demo ...
- Tornado的cookie过期问题
首先,web应用程序是使用HTTP协议进行数据传输,因为HTTP协议是无状态的,所以一旦提交数据完成后,客户端和服务器端的连接就会被关闭,再次进行数据的交换就得重新建立新的连接,那么,有个问题就是服务 ...
- Jquery倒计时源码分享
在静态页添加显示倒计时的容器,并引用下面脚本,代入时间参数即可使用. timeoutDate——到期时间,时间格式为2014/01/01或2014/1/1 D——天 H——小时 M——分钟 S——秒 ...
- mac下安装apache tomcat
目录 一. 默认版: 二. 自定义下载配置版: ———————————————————————正文—————————————————————————— 一. 默认版: ##一.mac 自带了apach ...