Hat’s Words (字典树 + 智能指针shared_ptr)
来源:互联网 发布:域名购买是永久的吗 编辑:程序博客网 时间:2024/06/11 01:31
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 298 Accepted Submission(s): 138Problem 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.
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.
Only one case.
Output
Your output should contain all the hat’s words, one per line, in alphabetical order.
Sample Input
aahathathatwordhzieeword
Sample Output
ahathatword
#include <stdio.h> #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string.h> #include <memory> using namespace std; string sss[55]; struct Node { int flag = 0; shared_ptr<Node> next[50]; }; void insert(const shared_ptr<Node>& root, const string& str) { shared_ptr<Node> cur(root); int i = 0; while (i < str.size()) { int k = str[i] - 'a'; if (cur->next[k] == NULL) { Node *p = new Node; cur->next[k].reset(p); cur = cur->next[k]; } else cur = cur->next[k]; i++; } cur->flag = 1;} bool search(const shared_ptr<Node>& root,const string& str) { shared_ptr<Node> cur(root); for (int i = 0; i < str.size(); ++i) { if (cur->next[str[i] - 'a'] == NULL) return false; cur = cur->next[str[i] - 'a']; } return cur->flag; } int main() { shared_ptr<Node> root(new Node); int cnt = 0; while (cin >> sss[cnt]) { insert(root, sss[cnt++]); } for (int i = 0; i < cnt; ++i) { int len = sss[i].size(); for (int j = 1; j < len - 1; ++j) { string str1(sss[i], 0, j); string str2(sss[i],j); if (search(root, str1) && search(root, str2)){ cout << sss[i] << endl; break; } } } }
0 0
- Hat’s Words (字典树 + 智能指针shared_ptr)
- Hat’s Words(字典树)
- Hat’s Words(字典树)
- hdu Hat‘s Words(字典树)
- 【字典树】 hdu1247 Hat’s Words
- Hat’s Words hdu1247 字典树+搜索
- HDU 1247 Hat’s Words(字典树)
- hdu 1247 字典树 Hat’s Words
- HDU1247-Hat’s Words(字典树)
- HDU 1247 Hat’s Words 字典树
- Hat’s Words(hdu1247字典树)
- hdu1247 Hat’s Words (字典树)
- hdu 1247 Hat’s Words 字典树
- HDU 1247 Hat’s Words(字典树)
- hdu1247 Hat’s Words(字典树)
- [HDOJ 1247] Hat’s Words [字典树]
- hdu 1247 Hat’s Words(字典树)
- hdu 1247 Hat’s Words字典树
- 每天喝6杯清水,就能轻松瘦身!
- [RK3288][Android6.0] WiFi在Setting中的开启过程小结
- Java实现单链表反转
- 在IDEA上创建web项目以及tomcat server的配置
- 机器学习算法集锦:从贝叶斯到深度学习及各自优缺点
- Hat’s Words (字典树 + 智能指针shared_ptr)
- OpenCV3.2计算视差图的流程,没有找到核心算法
- form 表单提交input信息 回车报错405
- CSS如何只改变父元素背景透明度不改变子元素透明度
- Hadoop OutputFormat 介绍
- 写一个简单的存储过程
- Jquery(四)
- mybatis做like模糊查询
- android studio 导入aar (阿里百川 hotfix aar 导入)