hdu 1247 Hat’s Words
2015-08-11 17:45
197 查看
点击此处即可传送到 hdu 1247
题目大意:就是给出若干个单词,找出其中能够由其它两个单词连接组合而成的单词。比如上面sample中的”ahat”能够由”a”和”hat”拼凑而成,”hatword”=”hat”+”word,当然,也不一定是非得 “hat” +”什么 “,也可以是”word” + “good”,就是这样了:给出一个例子:
Intput
a
aa
aaa
hat
word
s
hatwords
Output
aa
aaa
解题思路:Trie树,将单词拆分,具体代码上给出详细注释:
上代码:
[code] **Hat’s Words** 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
题目大意:就是给出若干个单词,找出其中能够由其它两个单词连接组合而成的单词。比如上面sample中的”ahat”能够由”a”和”hat”拼凑而成,”hatword”=”hat”+”word,当然,也不一定是非得 “hat” +”什么 “,也可以是”word” + “good”,就是这样了:给出一个例子:
Intput
a
aa
aaa
hat
word
s
hatwords
Output
aa
aaa
解题思路:Trie树,将单词拆分,具体代码上给出详细注释:
上代码:
[code]#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <string> using namespace std; const int maxn = 26; struct Trie//结点声明 { struct Trie *next[maxn];//孩子分支 bool isStr;//标记是否构成单词 }; void Insert(Trie *root,const char *s)//将单词s插入Trie树 { if(root==NULL || *s=='\0') return; //int i; Trie *p = root; while(*s != '\0') { if(p->next[*s-'a'] == NULL) //如果不存在,则建立结点 { Trie *tmp = (Trie*)malloc(sizeof(Trie)); for(int i=0; i<maxn; i++) tmp->next[i] = NULL; tmp->isStr = false; p->next[*s-'a'] = tmp; p = p->next[*s-'a']; } else p = p->next[*s-'a']; s++; } p->isStr = true;//单词结束的地方标记此处可以构成一个单词 } int Find(Trie *root, const char *s) { Trie *p = root; while(p!=NULL && *s!='\0') { p = p->next[*s-'a']; s++; } return (p!=NULL && p->isStr==true);//在单词结束处的标记为true时,单词才存在 } void Del(Trie *root)//释放整个Trie树的空间 { for(int i=0; i<maxn; i++) { if(root->next[i] != NULL) Del(root->next[i]); } free(root); } char s[50000][100]; char str[100]; int main() { int m,n; //n为建立Trie树输入的单词数,m为要查找的单词数 Trie *root = (Trie *)malloc(sizeof(Trie)); for(int i=0; i<maxn; i++) root->next[i] = NULL; root->isStr = false; int cnt = 0; while(scanf("%s",str) != EOF) { strcpy(s[cnt++],str); Insert(root, str); } for(int i=0; i<cnt; i++) { for(int j=1; j<strlen(s[i]); j++) { char tmp1[100] = {'\0'}; char tmp2[100] = {'\0'}; /*strncpy函数是指(char s1,char s2, int len)三个参数,将s2的len个数 复制给s1,但是最后要加'\0'要不然就会。。。。*/ strncpy(tmp1, s[i], j); strncpy(tmp2, s[i]+j, strlen(s[i])-j); if(Find(root, tmp1) && Find(root, tmp2)) { printf("%s\n",s[i]); break; } } } Del(root); return 0; }
相关文章推荐
- 喵哈哈的日常选数问题
- 28-反射机制-06-反射机制(获取Class中的方法)
- 无线网
- 智渔课堂官方免费教程十九:Java常用类之String类
- Spring自定义@Qualifier
- [IOS开发]搜索用谓词NSPredicate
- 28-反射机制-05-反射机制(获取Class中的字段)
- I学霸官方免费教程十九:Java常用类之String类
- NOIP2014提高组 寻找道路
- cocosdx里面A*算法的实现#ifndef __CAT_SPRITE_H__ #define __CAT_SPRITE_H__ #include "HelloWorldScene.h" #incl
- android WebView总结
- java要在命令行运行eclipse的项目的方法
- 28-反射机制-04-反射机制(获取Class中的构造函数)
- 源码 、补码、 反码的详解(转载)
- Truck History POJ 1789【Kruscal算法 || Prim】
- IP协议解读(二)
- 使用Nginx反向代理实现负载均衡
- python 中文异常问题记录
- [AIR] AS3读取txt文档
- 黑马程序员——Java中集合框架总结