hdu--(1247)Hat’s Words(trie树)
时间:2022-05-05
本文章向大家介绍hdu--(1247)Hat’s Words(trie树),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8343 Accepted Submission(s): 3004
Problem Description
A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary. You are to find all the hat’s words in a dictionary.
Input
Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words. Only one case.
Output
Your output should contain all the hat’s words, one per line, in alphabetical order.
Sample Input
a ahat hat hatword hziee word
Sample Output
ahat
hatword
Author
戴帽子的
简述题意: 给你很多单词,查找能够由其中的两个单词拼合而成的单词,并按字典序输出.....
代码:
1 #include<cstdio>
2 #include<cstring>
3 #include<cstdlib>
4 typedef struct node
5 {
6 struct node *child[26];
7 bool tail;
8 }Trie;
9 char mat[50005][15];
10 void Insert(char *s,Trie *root)
11 {
12 int i,pos,j;
13 Trie *cur=root,*curnew;
14 for(i=0;s[i]!=' ';i++){
15 pos=s[i]-'a';
16 if(cur->child[pos]==NULL)
17 {
18 curnew=(Trie *)malloc(sizeof(Trie));
19 for(j=0;j<26;j++)
20 curnew->child[j]=NULL;
21 curnew->tail=false;
22 cur->child[pos]=curnew;
23 }
24 cur=cur->child[pos];
25 }
26 cur->tail=true;
27 }
28 bool check(char *s,Trie *root)
29 {
30 int i,pos;
31 Trie *cur=root;
32 for(i=0;s[i]!=' ';i++)
33 {
34 pos=s[i]-'a';
35 if(cur->child[pos]==NULL) return 0;
36 cur=cur->child[pos];
37 }
38 if(cur->tail==1)return 1;
39 return 0;
40 }
41 bool query(char *s,Trie *root)
42 {
43 int i,pos;
44 Trie *cur=root;
45 for(i=0;s[i]!=' ';i++)
46 {
47 pos=s[i]-'a';
48 if(cur->child[pos]==NULL) return 0;
49 else
50 if(cur->tail==1&&check(s+i,root))
51 return 1;
52 cur=cur->child[pos];
53 }
54 return 0;
55 }
56 int main()
57 {
58 int i=0,j;
59 #ifdef LOCAL
60 freopen("test.in","r",stdin);
61 #endif
62 Trie *root=(Trie *)malloc(sizeof(Trie));
63 root->tail=0;
64 for(j=0;j<26;j++)
65 root->child[j]=NULL;
66 while(scanf("%s",mat[i])!=EOF){
67 Insert(mat[i],root);
68 i++;
69 }
70 for(j=0;j<i;j++)
71 {
72 if(query(mat[j],root))
73 printf("%sn",mat[j]);
74 }
75 return 0;
76 }
- 如何使用SAML配置Cloudera Manager的身份验证
- 如何使用Shibboleth搭建IDP服务并集成OpenLDAP
- 如何获取Hive正在执行或者已结束的的MapReduce作业的SQL语句
- 如何启用Oozie的HA
- 如何使用Oozie API接口向Kerberos环境的CDH集群提交Spark作业
- 如何降级Cloudera Manager和CDH
- 如何在CDH中安装和使用StreamSets
- 如何使用Oozie API接口向非Kerberos环境的CDH集群提交Shell工作流
- 如何使用Oozie API接口向非Kerberos环境的CDH集群提交Java作业
- 如何通过Livy的RESTful API接口向Kerberos环境的CDH集群提交作业
- 如何使用Oozie API接口向非Kerberos环境的CDH集群提交Spark作业
- Joomla 权限提升漏洞(CVE-2016-9838)分析
- Firefox - SVG cross domain cookie vulnerability
- 当代 Web 的 JSON 劫持技巧
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法