HDU1247 Hat’s Words【Trie】
来源:互联网 发布:scratch趣味编程100例 编辑:程序博客网 时间:2024/06/06 11:06
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1247
思路
叫你在字典中找一些字符串,约束条件:可以拆分成两个已有的字符串
直接暴力肯定超时,所以把所有的词都放在字典树里咨询就可以了。
不过这样复杂度还是有点高,但是竟然能过,看来这题数据还是比较小的。
AC代码
#include <iostream>#include <string>using namespace std;struct Trie{ Trie() { for (int i = 0; i < 26; ++i) { next[i] = nullptr; } is_end = false; } bool is_end; Trie* next[26];}root;void insert(string s){ Trie *now = &root; for (int i = 0; i < s.length(); ++i) { if (now->next[s[i] - 'a'] == nullptr) now->next[s[i] - 'a'] = new Trie; now = now->next[s[i] - 'a']; if (i == s.length() - 1) now->is_end = true; }}bool find(string s){ Trie *now = &root; for (int i = 0; i < s.length(); ++i) { now = now->next[s[i] - 'a']; if (now == nullptr) return false; if (i == s.length() - 1) return now->is_end; } return false;}string dic[50000 + 10];int main(){ cin.tie(0); ios::sync_with_stdio(false); string s; int p = 0; while (cin >> s) { insert(s); dic[p++] = s; } for (int i = 0; i < p; ++i) { for (int r = 1; r <= dic[i].length() - 1; ++r) { string a = dic[i].substr(0, r); string b = dic[i].substr(r, dic[i].length() - r); if (find(a) && find(b)) { cout << dic[i] << "\n"; break; } } } return 0;}
0 0
- HDU1247 Hat’s Words【Trie】
- Hat's Words hdu1247 trie
- HDU1247-Hat’s Words(trie树)
- HDU1247 Hat’s Words 【trie树】
- hdu1247 Hat's words trie树
- HDU1247 Hat's Words(Trie树,map)
- hdu1247 Hat’s Words
- HDU1247 Hat’s Words
- hdu1247 Hat’s Words
- hdu1247 hat's words
- hdu1247 Hat’s Words
- hdu1247 Hat’s Words
- hdu1247 Hat’s Words
- HDU1247 Hat’s Words(Trie的动态内存版本)
- hash hdu1247 Hat’s Words
- 【Trie】【HDU1247】【Hat’s Wordsfd2】
- 【字典树】 hdu1247 Hat’s Words
- Hat’s Words hdu1247 字典树+搜索
- ESP8266使用详解
- html+css学习笔记(2)
- 对依赖注入,控制反转,反射的理解
- Maven in Android Eclipse中搭建Maven管理的Android程序
- Python用文件输入替换stdin,stdout
- HDU1247 Hat’s Words【Trie】
- 学校c#大作业在连接mysql数据库过程中遇到的坑们
- 使用远程线程来注入DLL
- 分页存储过程-利用select top和中间变量实现分页
- 本机突然不能访问虚拟机Linux中的项目了(问题解决)
- PHP数组的几个操作,求并集,交集,差集,数组与字符串的相互转换及数组去重
- Thinking in Java 第4章 控制执行流程 【Foreach 语句】
- Sublime text + Latex
- Android Studio 使用入门教程之常用配置