HDU 1247 Hat’s Words (Trie·指针实现)
2015-07-27 16:43
357 查看
题意 给你一个字典 输出字典中能表示成两个单词连接的所有单词
最基础的字典树应用 先把所有单词加入字典树中 标记每个结点是否为某个单词的结尾 然后查找每个单词 在树上查询过程中遇到单词结尾时 如果剩下的后缀也是一个单词 那当前查询的单词就可以是两个单词的连接了
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
Sample Output
最基础的字典树应用 先把所有单词加入字典树中 标记每个结点是否为某个单词的结尾 然后查找每个单词 在树上查询过程中遇到单词结尾时 如果剩下的后缀也是一个单词 那当前查询的单词就可以是两个单词的连接了
#include <cstdio> #include <cstring> using namespace std; const int N = 50005; int n; char ss [20]; struct Trie { Trie *chi[26]; bool isEnd; Trie() { isEnd = false; memset(chi, NULL, sizeof(chi)); } }*root; void insertTrie(Trie *r, char s[]) { int i = 0, j; while(s[i]) { j = s[i++] - 'a'; if(r->chi[j] == NULL) r->chi[j] = new Trie(); r = r->chi[j]; } r->isEnd = true; } //op标记这次查询是查询后缀还是查询整个 bool searchTrie(Trie *r, char s[], int op) { int i = 0, j; while(s[i]) { j = s[i++] - 'a'; if(r->chi[j] == NULL) return false; r = r->chi[j]; if(!op && r->isEnd && s[i])//前缀是一个单词 { if(searchTrie(root, s + i, 1)) return true; //查询后缀是否是一个单词 } } return op ? r->isEnd : op; } int main() { int m = 0; root = new Trie(); while(~scanf("%s", ss[m])) insertTrie(root, ss[m++]); for(int i = 0; i < m; ++i) if(searchTrie(root, ss[i], 0)) puts(ss[i]); return 0; }
Hat’s Words
Problem DescriptionA 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
相关文章推荐
- ios 判断小数点. 后几位 && 小数点后有数据显示数据,没有则不显示数据
- 注册JDBC驱动的三种方式
- JPA 注解 OneToMany和ManyToOne
- JS禁止选中文本方法
- suse11下创建分区,并设置开机自动挂载
- hive安装
- Python获取CPU使用率、内存使用率、网络使用状态
- allowInvalidCertificates
- nyoj 括号配对问题(栈)
- 自考
- 【试水CAS-4.0.3】第08节_CAS客户端配置单点登出
- Windows下pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat
- Virtualbox网络设置和无UI启动
- 怎么通过 Microsof Office Project 2010 来写功能开发计划
- Android 用户界面【level 1】
- Android SDK Android NDK Android Studio 官方下载地址
- 不重复随机数列生成算法
- 玩转Nodejs的集群
- nyoj269VF
- Error:activity xxx has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41971600