Hat’s Words

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11894    Accepted Submission(s): 4239

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
 题意:‘帽子’单词:由其他两个单词拼接而成。输出所有的帽子单词。字典树。
静态建树:
#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=;
const int N=;
struct node{
bool val;
node* next[N];
};
node* root;
node memory[MAXN];
int ant; node* create()
{
node* p=&memory[ant++];
for(int i=;i<N;i++)
{
p->next[i]=NULL;
p->val=false;
}
return p;
} void insert(char *s)
{
node* p=root;
for(int i=;s[i];i++)
{
int k=s[i]-'a';
if(p->next[k]==NULL) p->next[k]=create();
p=p->next[k];
}
p->val=true;//若能走到最末端,则返回true;
} bool search(char *s)
{
node* p=root;
for(int i=;s[i];i++)
{
int k=s[i]-'a';
if(p->next[k]==NULL) return false;
p=p->next[k];
}
return p->val;//若只是某个已插入单词的前缀,则返回false;
} int main()
{
int cnt=;
root=create();
char word[MAXN][];
while(scanf("%s",word[cnt])!=EOF)
{
//if(word[cnt][0]=='0') break;
insert(word[cnt]);
cnt++;
}
for(int i=;i<cnt;i++)
{
for(int j=;word[i][j];j++)
{
char fr[]={'\0'};
char re[]={'\0'};
strncpy(fr,word[i],j);
strncpy(re,word[i]+j,strlen(word[i])-j);
if(search(fr)&&search(re))
{
printf("%s\n",word[i]);
break;
}
}
} return ;
}

动态建树:

#include"cstdio"
#include"cstring"
#include"cstdlib"
using namespace std;
const int MAXN=;
const int N=;
struct Node{
bool x;
Node* next[N];
Node()
{
x=false;
for(int i=;i<N;i++) next[i]=NULL;
}
};
char word[MAXN][];
int cnt;
Node *root;
void Insert(char s[])
{
Node *p=root;
for(int i=;s[i];i++)
{
int k=s[i]-'a';
if(p->next[k]==NULL) p->next[k]=new Node();
p=p->next[k];
}
p->x=true;
}
bool Search(char s[])
{
Node *p=root;
for(int i=;s[i];i++)
{
int k=s[i]-'a';
if(p->next[k]==NULL) return false;
p=p->next[k];
}
return p->x;
}
void Del(Node *p)
{
for(int i=;i<N;i++)
{
if(p->next[i]!=NULL)
{
Del(p->next[i]);
}
}
delete p;
}
int main()
{
root=new Node();
while(scanf("%s",word[cnt])!=EOF)
{
//if(word[cnt][0]=='0') break;
Insert(word[cnt]);
cnt++;
}
for(int i=;i<cnt;i++)
{
for(int j=;word[i][j+];j++)
{
char fr[]={'\0'};
char re[]={'\0'};
strncpy(fr,word[i],j);
strncpy(re,word[i]+j,strlen(word[i])-j);
if(Search(fr)&&Search(re))
{
printf("%s\n",word[i]);
break;
}
}
}
Del(root);
return ;
}
 

HDU1247(经典字典树)的更多相关文章

  1. hdu1247(字典树+枚举)

    Hat's Words(hdu1247) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...

  2. Hihicoder 题目1 : Trie树(字典树,经典题)

    题目1 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编 ...

  3. poj 2503:Babelfish(字典树,经典题,字典翻译)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Descr ...

  4. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  5. hdu 1247:Hat’s Words(字典树,经典题)

    Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  7. hdu 1251:统计难题(字典树,经典题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  8. HDU 4825 Xor Sum(经典01字典树+贪心)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

  9. HDu-1247 Hat’s Words,字典树裸模板!

    Hat's Words 题意:给出一张单词表求有多少个单词是由单词表里的两个单词组成,可以重复!按字典序输出这些单词. 思路:先建一个字典树,然后枚举每个单词,把每个单词任意拆分两部分然后查找. 目测 ...

随机推荐

  1. spark0.9.0安装

    利用周末的时间安装学习了下最近很火的Spark0.9.0(江湖传言,要革hadoop命,O(∩_∩)O),并体验了该框架下的机器学习包MLlib(spark解决的一个重点就是高效的运行迭代算法),下面 ...

  2. shell 日期加减运算

    比如今日是2012-04-22 $ date -d "+1 day" +%Y-%m-%d 2012-04-23   $ date -d "-1 day" +%Y ...

  3. python 3 递归调用与二分法

    递归调用与二分法 1.递归调用 递归调用:在调用一个函数的过程中,直接或间接地调用了函数本身. 示例: def age(n): if n == 1: return 18 # 结束条件 return a ...

  4. python中reduce()函数

    reduce()函数也是Python内置的一个高阶函数.reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接收 ...

  5. 第二十四篇、socketserver源码剖析

    这里选择的是python2.7(python3和2.7的源码基本类似) #!/usr/bin/env python # -*- coding:utf-8 -*- import SocketServer ...

  6. 剑指offer——求1+2+...+n

    方法一.通过在类的构造函数中执行加的过程. #include <iostream> using namespace std; class Base { public: Base(){n++ ...

  7. UOJ278 【UTR #2】题目排列顺序

    本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/转载请注明出处,侵权必究,保留最终解释权!   题目链接: http://uoj.ac/co ...

  8. jquery实现自定义弹出框

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 深入理解JVM - 晚期(运行期)优化

    在部分商用虚拟机中,Java程序最初是通过解释器(Interpreter)进行解释执行的,当虚拟机发现某个方法或者代码块的运行特别频繁时,就会把这些代码认定为“热点代码”(Hot Spot Code) ...

  10. 消息队列(Message Queue)基本概念

    背景 之前做日志收集模块时,用到flume.另外也有的方案,集成kafaka来提升系统可扩展性,其中涉及到消息队列当时自己并不清楚为什么要使用消息队列.而在我自己提出的原始日志采集方案中不适用消息队列 ...