字典树——Hdu 1247 Hat’s Words
来源:互联网 发布:纸质读书笔记 知乎 编辑:程序博客网 时间:2024/05/29 10:46
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4181 Accepted Submission(s): 1614
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.
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
题目大意:
在所给的单词表中,按字典序输出符合条件的 hat's word ,条件为: hat's word 恰好由所给单词表中的另外两个单词拼接而成。
思路:
对每一个单词判断,该单词所分为的两个单词是否都在所给单词表中,由于单词的数量最大为50000,判断拆分出的单词是否在单词表中是比较耗时的,所以为了提高效率,这里把所给单词另外建立成一棵字典树。
#include <stdio.h>#include <string.h>#define MAX 26#define SIZE 50005#define LENGTH 100typedef struct Trie_Node{Trie_Node *child[MAX];bool flag;//标记是否为一个单词结尾Trie_Node(void){flag = false;memset(child, NULL, sizeof(child));}}*Trie;char wordList[SIZE][LENGTH];void InitTrie(Trie &root){root = new Trie_Node;}void Insert(Trie curNode, char *word){int i, j;for (i = 0; word[i] != '\0'; i++){j = word[i] - 'a';if (curNode->child[j] == NULL){curNode->child[j] = new Trie_Node;}curNode = curNode->child[j];}curNode->flag = true;}bool LookUp(Trie curNode, char *word){int i, j;for (i = 0; word[i] != '\0'; i++){j = word[i] - 'a';if (curNode->child[j] == NULL){return false;}curNode = curNode->child[j];}return curNode->flag;}void HatsWord(Trie root, int size){int i, j, k;Trie curNode = NULL;for (i = 0; i < size; i++){curNode = root;for (j = 0; wordList[i][j] != '\0'; j++){//判断单词的前一部分所组成的单词是否已经出现在字典中if (curNode->flag){//判断剩下部分所组成的单词是否也在字典中if (LookUp(root, &wordList[i][j])){puts(wordList[i]); break;}}k = wordList[i][j] - 'a';if (curNode->child[k] == NULL){break;}curNode = curNode->child[k];}}}int main(void){int i;Trie dict = NULL;InitTrie(dict);for (i = 0; scanf("%s", wordList[i]) != EOF; i++){Insert(dict, wordList[i]);}HatsWord(dict, i);return 0;}
- 字典树——Hdu 1247 Hat’s Words
- 字典树——hdu 1247 Hat’s Words
- HDU 1247 Hat’s Words(字典树)
- hdu 1247 字典树 Hat’s Words
- HDU 1247 Hat’s Words 字典树
- hdu 1247 Hat’s Words 字典树
- HDU 1247 Hat’s Words(字典树)
- hdu 1247 Hat’s Words(字典树)
- hdu 1247 Hat’s Words字典树
- hdu 1247 Hat’s Words(字典树)
- hdu 1247 Hat’s Words 字典树
- 【字典树】HDU 1247 Hat’s Words
- HDU--1247:Hat’s Words (字典树)
- hdu 1247 Hat’s Words(字典树)
- [字典树] HDU 1247 - Hat’s Words
- hdu 1247 Hat’s Words (字典树)
- HDU 1247 Hat’s Words 字典树
- HDU - 1247 Hat’s Words(字典树)
- Yahoo,Msn,Skype,QQ,阿里旺旺在线聊天链接接口调用
- Ant 打包build.xml,并直接发布到tomcat应用
- 对表格处理的JS代码
- 正则转换url参数-int类型
- OpenFlow资源整理
- 字典树——Hdu 1247 Hat’s Words
- MyEclipse8.6 Io 异常: The Network Adapter could not establish the connection
- 域用户自助修改AD属性Web应用 - ADCustomWeb
- android的编译和运行过程
- 网站排障分析常用的命令
- solr链接数据库
- 网站小说采集
- 枚举
- OpenGL函数思考-glHint