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)
    Total Submission(s): 3565    Accepted Submission(s): 1355


    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
     
    Author
    戴帽子的
     
    Recommend
    Ignatius.L
     
     
    字典树的题目。
    就是看单词是不是可以由其他两个单词合并起来。
    建树。
    然后将每个单词进行划分,看存不存在。
     
    // HDU 1247 Hat¡¯s Words
    //×ÖµäÊ÷
    
    #include<iostream>
    #include<string.h>
    #include<stdlib.h>
    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    const int MAX=26;
    
    typedef struct Trie_Node
    {
        bool isWord;
        struct Trie_Node *next[MAX];
    }Trie;
    char s[50010][50];
    
    void insert(Trie *root,char *word)
    {
        Trie *p=root;
        int i=0;
        while(word[i]!='\0')
        {
            if(p->next[word[i]-'a']==NULL)
            {
               // Trie *temp=(Trie *)malloc(sizeof(Trie));
               Trie *temp=new Trie;
                for(int j=0;j<MAX;j++)
                 temp->next[j]=NULL;
                temp->isWord=false;
                p->next[word[i]-'a']=temp;
            }
            p=p->next[word[i]-'a'];
            i++;
        }
        p->isWord=true;
    }
    bool search(Trie *root,char *word)//查找单词是否勋在
    {
        Trie *p=root;
        for(int i=0;word[i]!='\0';i++)
        {
            if(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);
    }
    int main()
    {
       // freopen("in.txt","r",stdin);
       // freopen("out.txt","w",stdout);
        int i,j;
        int cnt=0;
        char str[50];
      //  Trie *root=(Trie *)malloc(sizeof(Trie));
      Trie *root=new Trie;
        for(i=0;i<MAX;i++)
           root->next[i]=NULL;
        root->isWord=false;
        while(scanf("%s",s[cnt])!=EOF)
        {
            insert(root,s[cnt++]);
        }
        for(i=0;i<cnt;i++)
        {
            int len=strlen(s[i]);
            for(j=1;j<len-1;j++)
            {
                char temp1[50]={'\0'};
                char temp2[50]={'\0'};
                strncpy(temp1,s[i],j);
                strncpy(temp2,s[i]+j,len-j);
                if(search(root,temp1)&&search(root,temp2))
                {
                    printf("%s\n",s[i]);
                    break;
                }
            }
        }
        del(root);
        return 0;
    }
  • 相关阅读:
    Kendo UI for ASP.NET MVC 的一些使用经验
    AI智能技术监控学生上课行为,智慧管理加强校园教学质量
    如何通过ffmpeg 实现实时推流和拉流保存的功能
    如何测试流媒体服务器的并发能力?
    TSINGSEE青犀视频H265播放器FLV.js播放一段时间后报内存不足怎么处理?
    互动电视的未来应该是什么样的?
    牛客练习赛89 题解
    【洛谷P3934】炸脖龙 I
    【CF1463F】Max Correct Set
    【CF1496F】Power Sockets
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2624786.html
Copyright © 2011-2022 走看看