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

题目意思是   给你一定的单词,要你找出能够通过其他两个单词组成的单词,数据有50000多,字典树了

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef struct Trie {
    int flag;
    Trie* next[26];
} Trie;
Trie* root;
int flag;
char a[50000][50];
void Inti()//初始化
{
    root = (Trie*)malloc(sizeof(Trie));
    root->flag = 0;
    for (int i = 0; i < 26; i++)
        root->next[i] = NULL;
}
void ChaRu(char *a)//建立字典树    插入单词
{
    Trie* p = root, *q;
    int l = strlen(a);
    for (int i = 0; i < l; i++) {
        int id = a[i] - 'a';
        if (p->next[id] == NULL) {
            q = (Trie *)malloc(sizeof(Trie));
            q ->flag = 0;
            for (int i = 0; i < 26; i++)
                q->next[i] = NULL;
            p->next[id] = q;
        }
       /* else if (p->next[id]->flag) {
            flag = 1;
        }*/
        p = p->next[id];
    }
    p->flag = 1;
}
int CZ(char *a)//查找单词
{
    Trie *p=root;
    int len=strlen(a);
    for(int i=0;i<len;i++)
    {
        int id=a[i]-'a';
        p=p->next[id];
        if(p==NULL)
            return 0;
    }
    return p->flag;
}
int SF(Trie *p)//释放空间    这个题不用也可以过
{
//    Trie *p=root;
    if(p==NULL)return 0;
    for(int i=0;i<26;i++)
    {
        if(p->next[i]!=NULL)
            SF(p->next[i]);
    }
    free(p);
    return 0;
}
int main()
{

    Inti();
    int n=0;
    while (~scanf("%s", &a[n])) {
       // flag = 0;
       //printf("%s",a[i]);
        ChaRu(a[n]);
        n++;
    }

    for(int i=0;i<n;i++)
    {
        int len=strlen(a[i]);
        for(int j=0;j<len;j++)
        {
            char str1[50]={'\0'},str2[50]={'\0'};//新建两个临时数组
            strncpy(str1,a[i],j);                //把第i个单词进行拆分,分别放入两个临时数组里面
            strncpy(str2,a[i]+j,len-j);
            if(CZ(str1)&&CZ(str2))            //通过查找函数进行查找,如果两个返回都是1,代表能够组成
            {
                printf("%s\n",a[i]);
                break;
            }
        }
    }
    SF(root);//释放内存(这个题可以不要)
    return 0;
}

Hat’s Words(字典树)

时间: 2024-08-01 22:17:12

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

hdu 1247 Hat’s Words 字典树

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

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