HDU-1247 Hat’s Words (暴力)【Trie树】
<题目链接>
题目大意:
给你一些单词,要求输出将该单词完全分成前、后两个单词之后,若这两个单词都在单词库中出现,则输出该单词。
解题分析:
将每个单词的每一位能够拆分的位置全部暴力枚举一遍,若拆分后的两个单词都在单词库中,则直接输出该单词即可,拆分单词的时候用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树】的更多相关文章
- HDU 1247 Hat’s Words(字典树变形)
题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...
- HDU 1247 Hat’s Words(字典树)
http://acm.hdu.edu.cn/showproblem.php?pid=1247 题意: 给出一些单词,问哪些单词可以正好由其他的两个单词首尾相连而成. 思路: 先将所有单独插入字典树,然 ...
- HDU 1247 Hat’s Words(字典树)题解
题意:给一个字符串集,要你给出n个字符串s,使s能被所给字符串集中的两个相加所得(ahat=a+hat) 思路:简单字典树题,注意查询的时候要判断所指next是否为NULL,否则会RE非法访问. 代价 ...
- HDU 1247 Hat’s Words (字典树 && map)
分析:一開始是用递归做的,没做出来.于是就换了如今的数组.即,把每个输入的字符串都存入二维数组中,然后创建字典树.输入和创建完成后,開始查找. 事实上一開始就读错题目了,题目要求字符串是由其它两个输入 ...
- 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(字典树活用)
Hat's Words Time Limit : 2000 / 1000 MS(Java / Others) Memory Limit : 65536 / 32768 K(Java / Othe ...
- DHU--1247 Hat’s Words && HiHocder--1014 Trie树 (字典树模版题)
题目链接 DHU--1247 Hat’s Words HiHocder--1014 Trie树 两个一个递归方式一个非递归 HiHocoder #include<bits/stdc++.h> ...
- HDU1247 Hat’s Words 【trie树】
Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- 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 ...
- HDU 1251 统计难题 (字符串-Trie树)
统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...
随机推荐
- vue element-UI 升级报错Cannot find module "element-ui/lib/theme-default/index.css"
饿了么 用之前的版本 有些组件跟api 不一样了所以更新了最新的版本,发现 报一堆错误 主要错误是这个 Cannot find module "element-ui/lib/theme-de ...
- Java中Super和final关键字以及异常类
1.final类不能有子类,也就谈不上继承的说法,如果用final修饰成员变量或者局部变量,那成了常量需要制定常量的值. 2.对象的上转型对象,上转型对象不能操作子类新增的成员变量,不能调用子类新增的 ...
- Happiness
1575: Happiness 时间限制: 1 Sec 内存限制: 1280 MB 题目描述 Chicken brother is very happy today, because he atta ...
- (转)一位资深程序员大牛给予Java初学者的学习路线建议
Java学习这一部分其实也算是今天的重点,这一部分用来回答很多群里的朋友所问过的问题,那就是你是如何学习Java的,能不能给点建议?今天我是打算来点干货,因此咱们就不说一些学习方法和技巧了,直接来谈每 ...
- AI学习吧-购物车-添加商品接口
create接口流程 需求:向购物车添加商品 流程:写shopping_cart路由--->写ShoppingCart视图函数--->使用Authuser校验用户是否登录--->首先 ...
- spring cloud Eureka常见问题总结
Spring Cloud中,Eureka常见问题总结. 指定Eureka的Environment 1 eureka.environment: 指定环境 参考文档:https://github.com/ ...
- Python语音识别(计算器)
第一步关于导入模块的事,我试了好几个方法才发现在好像win7系统没有语音识别功能,我用了win10的又需要重新下载一个包 这样子,win32com.client模块就可以使用了 import win3 ...
- unable to access android sdk add-on list(转)
造成这个问题的原因可能有多种,下面两种方法,我亲自测试后可用,如果都不行,请在评论里告诉我,我会尽快帮你分析解决.左侧的文章分类中,Android Studio编译构建错误记录了我开发中遇到的所有编译 ...
- Spring Cloud、Spring Boot与Docker 学习资料汇总
使用Spring Cloud与Docker实战微服务https://gitee.com/itmuch/spring-cloud-bookhttps://eacdy.gitbooks.io/spring ...
- VS2017使用文档
参考链接:https://docs.microsoft.com/zh-cn/visualstudio/debugger/?view=vs-2017