zoukankan      html  css  js  c++  java
  • HDU 1247 Hat’s Words (字典树)

    Hat’s Words

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

    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<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<iostream>
    using namespace std;
    
    typedef struct Trie_Node
    {
        bool isWord;
        struct Trie_Node *next[26];
    }Trie;
    char s[50010][50];
    
    void Trie_insert(Trie *root,char *str)
    {
        Trie *p=root;
        int len=strlen(str);
        for(int i=0;i<len;i++)
        {
            int k=str[i]-'a';
            if(p->next[k]==NULL)
            {
                Trie *t=new Trie;
                for(int j=0;j<26;j++) t->next[j]=NULL;
                t->isWord=false;
                p->next[k]=t;
            }
            p=p->next[k];
        }
        p->isWord=true;
    }
    bool Trie_search(Trie *root,char *str)
    {
        Trie *p=root;
        int len=strlen(str);
        for(int i=0;i<len;i++)
        {
            int k=str[i]-'a';
            if(p->next[k]==NULL) return false;
            p=p->next[k];
        }
        return p->isWord;
    }
    void Trie_del(Trie *root)
    {
        for(int i=0;i<26;i++)
        {
            if(root->next[i]!=NULL)
                Trie_del(root->next[i]);
        }
        free(root);
    }
    
    int main()
    {
        int tot=0;
        Trie *root=new Trie;
        for(int i=0;i<26;i++)
            root->next[i]=NULL;
        root->isWord=false;
        while(scanf("%s",s[tot])!=EOF)
        {
            Trie_insert(root,s[tot++]);
        }
        for(int i=0;i<tot;i++)
        {
            int len=strlen(s[i]);
            for(int j=1;j<len-1;j++)
            {
                char temp1[50]={''};
                char temp2[50]={''};
                strncpy(temp1,s[i],j);
                strncpy(temp2,s[i]+j,len-j);
                if(Trie_search(root,temp1)&&Trie_search(root,temp2))
                {
                    printf("%s
    ",s[i]);
                    break;
                }
            }
        }
        Trie_del(root);
        return 0;
    }
  • 相关阅读:
    Mac OS X配置环境变量
    react navite 学习资料
    协议是人造的交互(通信)规则
    语言的本质是更好的对客观世界作出抽象和描述
    编程语言评价标准:冯诺伊曼体系
    afnetwork moya 都符合通信协议七层模型
    Async/await promise实现
    协程 和 async await
    phpStorm字体大小无法调整, 怎么办?
    Composer常见问题
  • 原文地址:https://www.cnblogs.com/wangdongkai/p/5738373.html
Copyright © 2011-2022 走看看