<题目链接>

题目大意:

给你一些单词,要求输出将该单词完全分成前、后两个单词之后,若这两个单词都在单词库中出现,则输出该单词。

解题分析:

将每个单词的每一位能够拆分的位置全部暴力枚举一遍,若拆分后的两个单词都在单词库中,则直接输出该单词即可,拆分单词的时候用strncpy()函数比较方便。

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int M = 5e4+;
char word[M][]; struct Node{
bool flag;
Node *next[];
Node(){
flag=false;
for(int i=;i<;i++)
next[i]=NULL;
}
};
Node *root=new Node;
Node *now,*newnode;
void Insert(char *str){
now=root;
for(int i=;str[i];i++){
int to=str[i]-'a';
if(now->next[to]==NULL){
now->next[to]=new Node;
}
now=now->next[to];
}
now->flag=true; //标记该节点为单词的结尾
}
bool search(char *str){
now=root;
for(int i=;str[i];i++){
int to = str[i]-'a';
if(now->next[to]==NULL)return false;
now=now->next[to];
}
return now->flag;
}
void delete(Node *rt){
for(int i=;i<;i++)
if(rt->next[i]!=NULL)
delete(rt->next[i]);
delete(rt);
}
int main(){
int cnt=;
while(gets(word[++cnt])&&strlen(word[cnt]))
Insert(word[cnt]);
for(int i=;i<=cnt;i++){
int len=strlen(word[i]);
for(int j=;j<len;j++){
char s1[],s2[];
strncpy(s1,word[i],j+),s1[j+]='\0'; //strncpy中,word[i]代表复制的字符串起始位置,j+1代表要复制的长度
strncpy(s2,word[i]+j+,len-j-),s2[len-j-]='\0'; //后一段字符串
if(search(s1)&&search(s2)){
printf("%s\n",word[i]);
break;
}
}
}
//delete(root);
}

2018-10-29

HDU-1247 Hat’s Words (暴力)【Trie树】的更多相关文章

  1. HDU 1247 Hat’s Words(字典树变形)

    题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...

  2. HDU 1247 Hat’s Words(字典树)

    http://acm.hdu.edu.cn/showproblem.php?pid=1247 题意: 给出一些单词,问哪些单词可以正好由其他的两个单词首尾相连而成. 思路: 先将所有单独插入字典树,然 ...

  3. HDU 1247 Hat’s Words(字典树)题解

    题意:给一个字符串集,要你给出n个字符串s,使s能被所给字符串集中的两个相加所得(ahat=a+hat) 思路:简单字典树题,注意查询的时候要判断所指next是否为NULL,否则会RE非法访问. 代价 ...

  4. HDU 1247 Hat’s Words (字典树 &amp;&amp; map)

    分析:一開始是用递归做的,没做出来.于是就换了如今的数组.即,把每个输入的字符串都存入二维数组中,然后创建字典树.输入和创建完成后,開始查找. 事实上一開始就读错题目了,题目要求字符串是由其它两个输入 ...

  5. hdu 1247:Hat’s Words(字典树,经典题)

    Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. HDU 1247 Hat’s Words(字典树活用)

    Hat's Words Time Limit : 2000 / 1000 MS(Java / Others)    Memory Limit : 65536 / 32768 K(Java / Othe ...

  7. DHU--1247 Hat’s Words && HiHocder--1014 Trie树 (字典树模版题)

    题目链接 DHU--1247 Hat’s Words HiHocder--1014 Trie树 两个一个递归方式一个非递归 HiHocoder #include<bits/stdc++.h> ...

  8. HDU1247 Hat’s Words 【trie树】

    Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  9. HDU 5269 ZYB loves Xor I Trie树

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5269 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  10. HDU 1251 统计难题 (字符串-Trie树)

    统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...

随机推荐

  1. 一种基于NTC的控温电路及软件实现

    NTC(Negative Temperature Coefficient)是一种随温度上升时,电阻值呈指数关系减小的热敏电阻.应用广泛,最近我们就采用了NTC来控制加热并测温,并达到了预期的效果. 1 ...

  2. HTML5从入门到精通(明日科技) 中文pdf扫描版

    HTML5从入门到精通(明日科技) 中文pdf扫描版

  3. 【Myeclipse】用Myeclipse10.5搭建C/C++开发环境

    一.添加CDT到Myeclipse10.5 我的Myeclipse版本是10.5,刚开始用Myeclipse configuration center添加安装,不管是用远程URL还是用本地Archiv ...

  4. linq2db.EntityFrameworkCore 介绍

    linq2db.EntityFrameworkCore 是一个ef core的插件,对linq语法的扩展 对复杂的sql都有很好的支持,他是基于linq2db (provided by LINQ To ...

  5. SpringBoot端口和访问路径

    更改启动端口:在application.properties配置文件中修改server.port=9090 更改项目访问路径:在application.properties配置文件中修改server. ...

  6. 1706: 神奇的编码(zzuli)

    题目描述 假如没有阿拉伯数字,我们要怎么表示数字呢 小明想了一个方法如下: 1 -> A 2 -> B 3 -> C .... 25 -> Y 26 -> Z 27 -& ...

  7. 【python】安装hyperscan-python

    环境centos6.2 1. 先安装hyperscan 主要参考http://blog.csdn.net/cheng_fangang/article/details/51143412 注意:里面2.8 ...

  8. Git使用一:git客户端安装与创建用户

    1.下载并安装Git和图形客户端TortoiseGit Git官网:https://gitforwindows.org/ TortoiseGit官网: https://tortoisegit.org/ ...

  9. Jmeter BeanShell PostProcessor提取json数据

    需求:提取sample返回json数据中所有name字段值,返回的json格式如下: {“body”:{“apps”:[{“name”:”111”},{“name”:”222”}]}} jmeter中 ...

  10. C++ Primer 笔记——控制内存分配

    1.当我们使用一条new表达式时,实际执行了三步操作. new表达式调用一个名为operator new ( 或者operator new[] ) 的标准库函数.该函数分配一块足够大的,原始的,未命名 ...