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];
void insert(Trie *root,char *word)//生成字典树
{
    Trie *p=root;
    while(*word!='\0')
    {
        if(p->next[*word-'a']==NULL)
        {
            Trie *temp=(Trie*)malloc(sizeof(Trie));
            for(int i=0; i<MAX; i++)
            {
                temp->next[i]=NULL;
            }
            temp->isWord=false;
            p->next[*word-'a']=temp;
        }
        p=p->next[*word-'a'];
        word++;
    }
    p->isWord=true;
}
bool search(Trie *root,char *word)//查找单词是否存在
{
    Trie *p=root;
    for(int i=0; word[i]!='\0'; i++)
    {
        if(p==NULL||p->next[word[i]-'a']==NULL)
            return false;
        p=p->next[word[i]-'a'];
    }
    return p->isWord;
}
void del(Trie *root)//释放空间
{
    for(int i=0; i<MAX; i++)
    {
        if(root->next[i]!=NULL)
        {
            del(root->next[i]);
        }
    }
    free(root);//释放malloc申请的空间
}
int main()
{
    int i,j;
    int count=0;
    char str[50];
    Trie *root=(Trie*)malloc(sizeof(Trie));
    for(i=0; i<MAX; i++)
    {
        root->next[i]=NULL;
    }
    root->isWord=false;
    while(~scanf("%s",str))
    {
        strcpy(s[count++],str);
        insert(root,str);
    }
    for(i=0; i<count; i++)
    {
        for(j=1; j<=strlen(s[i])-1; j++)
        {
            char temp1[50]= {'\0'};
            char temp2[50]= {'\0'};
            strncpy(temp1,s[i],j);//枚举两部分
            strncpy(temp2,s[i]+j,strlen(s[i])-j);
            if(search(root,temp1)&&search(root,temp2))
            {
                printf("%s\n",s[i]);
                break;
            }
        }
    }
    del(root);
    return 0;
}

时间: 2024-08-01 10:46:12

hdu 1247 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

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’s Words Trie树(+测试数据)

Hat’s Words Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1247 Description A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.You a

HDU 1251 统计难题(字典树模板题 || map运用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一

HDU 1251 统计难题(字典树模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 const int maxn = 1000005; 6 7 int num = 0; 8 9 struct Tr

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(字典树,经典题)

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