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. [luogu3601]签到题

    [luogu3601]签到题 luogu 求\[\sum_{i=l}^ri-\phi(i)\] 一个朴素的想法是枚举l~r,根号求\(\phi\),显然这样是\((r-l)\sqrt r\),时间无法 ...

  2. Cordova+FrameWork7开发简单教程

    1: 环境要有:(一个不会搭建环境的程序员,要么学,要么退出编程 ) 环境这里我只说需要什么: 1>AndroidStudio 3.0 (2.几的版本总会出问题.我喜欢用新版本) 2>co ...

  3. leetcode第一刷_Permutations

    生成全排列的经典问题.递归方法的典范. bool visited[10000]; void getPermutation(vector<int> &num, vector<v ...

  4. SM30维护视图创建【转】

           在SAP中,经常需要自定义数据库表.而且可能需要人工维护数据库表中的数据,可以通过SM30进行维护数据:但是SM30事务的权限太大,不适宜将SM30直接分配:因此,可以通过给维护表分配事 ...

  5. 使用 Python 为 KVM 编写脚本,第 1 部分: libvirt

    虚拟化是目前市场上大多数服务器操作系统的标准设备.在 Linux® 的世界里,服务器虚拟化有两个主要选择:基于 Kernel 的虚拟机 (KVM) 和 Xen.KVM 是 Red Hat 和其他公司采 ...

  6. c语言操作mysql数据库

    c语言操作Mysql数据库,主要就是为了实现对数据库的增.删.改.查等操作,操作之前,得先连接数据库啊,而连接数据库主要有两种方法.一.使用mysql本身提供的API,在mysql的安装目录中可可以看 ...

  7. 在vi或vim上查找字符串

    从开头搜索 在命令模式下,输入/你要查找的字符 按下回车,可以看到vim把光标移动到该字符处 再按n(小写)查看下一个匹配 按N(大写)查看上一个匹配, capslock切换大小写,也可以在小写状态下 ...

  8. 有关java之反射的使用

    1 public class Demo02 { 2 @SuppressWarnings("all") 3 public static void main(String[] args ...

  9. 牛客练习赛13 B 幸运数字Ⅱ 【暴力】【二分】

    题目链接 https://www.nowcoder.com/acm/contest/70/B 思路 没有代码限制 先打表 打出 幸运数字的表 然后 二分查找 第一个 大于 r 的幸运数字 然后 往 L ...

  10. MySQL与Oracle的语法区别详细对比

    MySQL与Oracle的语法区别详细对比 Oracle和mysql的一些简单命令对比在本文中将会涉及到很多的实例,感兴趣的你不妨学习一下,就当巩固自己的知识了   Oracle和mysql的一些简单 ...