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;
    }
  • 相关阅读:
    对初学者的几点建议
    关于.net的一些资源网站
    iis 经常出现的问题以及解决方案
    C#编程规范(2008年4月新版)
    优秀ASP.NET程序员的修炼之路
    网站软件开发规范(某门户网站的)
    asp.net跳转页面的三种方法比较
    H5开发相关资料
    C#简介
    用C#获取硬盘序列号,CPU序列号,网卡MAC地址
  • 原文地址:https://www.cnblogs.com/wangdongkai/p/5738373.html
Copyright © 2011-2022 走看看