Hat’s Words(字典树)

Hat’s Words

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

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

题解:加了个val判断是否为一个完整的单词;本来还以为是只与hat结合呐,神经的只减去hat,谁知道,自己想错了。。。是两个单词的结合;

代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 #define mem(x,y) memset(x,y,sizeof(x))
 8 const int INF=0x3f3f3f3f;
 9 const double PI=acos(-1.0);
10 const int MAXN=50010;
11 int ch[MAXN][30],word[MAXN],val[MAXN];
12 char dt[50010][110];
13 int sz;
14 void initial(){
15     sz=1;
16     mem(ch[0],0);mem(word,0);mem(val,0);
17 }
18 void join(char *s){
19     int len=strlen(s),k=0,j;
20     for(int i=0;i<len;i++){
21         j=s[i]-‘a‘;
22         if(!ch[k][j]){
23             mem(ch[sz],0);
24         //    val[sz]=0;
25             ch[k][j]=sz++;
26         }
27         k=ch[k][j];
28         word[k]++;
29     }
30     val[k]=1;
31 }
32 bool find(char *s){
33     int len=strlen(s),k=0,j;
34     for(int i=0;i<len;i++){
35         j=s[i]-‘a‘;
36         k=ch[k][j];
37         if(!word[k])return false;
38     }
39     if(val[k])return true;
40     else return false;
41 }
42 int main(){
43     initial();
44     int t=0;
45     char s[110],l[110],r[110];
46     while(~scanf("%s",dt[t]))join(dt[t++]);
47     for(int i=0;i<t;i++){
48         int len=strlen(dt[i]);
49         if(len<=1)continue;
50         for(int j=1;j<len;j++){
51             strcpy(r,dt[i]+j);
52             strcpy(l,dt[i]);
53             l[j]=‘\0‘;
54             if(find(l)&&find(r)){
55                 puts(dt[i]);break;
56             }
57         }
58     }
59     return 0;
60 }
时间: 2024-12-18 15:26:44

Hat’s Words(字典树)的相关文章

hdu 1247 Hat’s Words 字典树

// hdu 1247 Hat's Words 字典树 // // 题目大意: // // 在一些字符串中,找到这样字符串:由两个其他的字符串构成 // // 解题思路: // // 字典树,先将这些字符串插入到字典树中,然后枚举断点,如果 // 字符串的前后两段都找到了,输出该串即可~ // // 感悟: // // 这道题目的话,就是字典树上的暴力嘛,细节方面还是要多多注意 // val值还是不能少哟~因为查找到了该串,不一定是一个单词,可能 // 是中间的一个节点,即某个字符串的前缀~~~

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, on

hdu 1247 Hat’s Words 字典树,还是比较有意思的题目

Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8843    Accepted Submission(s): 3171 Problem Description A hat's word is a word in the dictionary that is the concatenation of exactl

HDU 1247 Hat’s Words (字典树&#183;Trie)

题意  给你一个字典  输出字典中能表示成两个单词连接的所有单词 最基础的字典树应用  先把所有单词加入字典树中  标记每个结点是否为某个单词的结尾  然后查找每个单词  在树上查询过程中遇到单词结尾时  如果剩下的后缀也是一个单词  那当前查询的单词就可以是两个单词的连接了 #include <cstdio> #include <cstring> using namespace std; const int N = 50005; int n; char ss[N][20]; st

hdu1247 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, on

hdu 1247 Hat’s Words (字典树模板)

//那个单词是有出现的两个单词构成的 # include <cstdio> # include <cstring> # include <algorithm> # include <iostream> # define MAX 26 using namespace std; typedef struct Trie_Node { bool isWord; struct Trie_Node *next[MAX]; } Trie; char s[50000][50

hdoj 1247 Hat’s Words(字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的字符串. 对于长度为LEN的字符串,其可能由LEN种可能的拼接可能:现在问题转化为查找能够拼接成该字符串的可能的两个字符串是否都在 输入的字符串中,使用字典树可以实现快速的字符串查找,算法复杂度为O(N*M),N为输入字符串的个数,M为字符串长度. 代码如下: #include <cstdio>

HDU 1247 Hat&#39;s Words (字典树)

[题目链接]click here~~ [题目大意]A hat's word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary. ,找出由两个子字符串组成的字符串. [解题思路]字典树 #include <bits/stdc++.h> using namespace std; const int N=5*1e4+100; const int MOD=

HDU 1247 Hat’s Words (字典树 &amp;amp;&amp;amp; map)

分析:一開始是用递归做的,没做出来.于是就换了如今的数组.即,把每个输入的字符串都存入二维数组中,然后创建字典树.输入和创建完成后,開始查找. 事实上一開始就读错题目了,题目要求字符串是由其它两个输入的字符串组成的前后缀,自己根本没有推断前缀是否满足.就直接推断后缀,一直想不通自己哪里错了,非常羞愧,水平还是不行. 查找分为前缀查找和后缀查找.事实上两个函数基本差点儿相同的.以下放代码. #include <cstdio> #include <cstring> #include &

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

Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7282    Accepted Submission(s): 2639 Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly