zoukankan      html  css  js  c++  java
  • hdu1247 Hat’s Words

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1247

    题目:

    Hat’s Words

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 13765    Accepted Submission(s): 4927


    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
     

     思路:1.用Trie树,枚举每个单词,看他是否由两个单词组成,判断过程其实就是字典树的查找。时间复杂度是O(n*len)

        2.map爆一发,map哈希的复杂度不清楚,不过也是枚举每个单词是否由两个单词组成,枚举过程时间复杂度也是O(n*len),总体复杂度和Trie复杂度应该差不多。

        3.我看到我的运行时间很短,可能暴力枚举加map也能过。枚举str[i],str[j],判断map[str[i]+str[j]]存不存在即可,不知道能过否

    代码:

    #include <stdio.h>
    #include <stdlib.h>
    
    using namespace std;
    
    
    
    #define MAXNUM 26
    //定义字典树结构体
    typedef struct Trie
    {
        bool flag;//从根到此是否为一个单词
        Trie *next[MAXNUM];
    }Trie;
    //声明一个根
    Trie *root;
    char ss[50006][100];
    //初始化该根
    void init()
    {
        root = (Trie *)malloc(sizeof(Trie));
        root->flag=false;
        for(int i=0;i<MAXNUM;i++)
        root->next[i]=NULL;
    }
    //对该字典树的插入单词操作
    void insert(char *word)
    {
        Trie *tem = root;
        while(*word!='')
        {
            if(tem->next[*word-'a']==NULL)
            {
                Trie *cur = (Trie *)malloc(sizeof(Trie));
                for(int i=0;i<MAXNUM;i++)
                cur->next[i]=NULL;
                cur->flag=false;
                tem->next[*word-'a']=cur;
            }
            tem = tem->next[*word-'a'];
            word++;
        }
        tem->flag=true;
    }
    bool search2(char *word)
    {
        Trie *tem = root;
        char *p=word;
        while(*p)
        {
            if(tem==NULL||tem->next[*p-'a']==NULL)
                return false;
            tem=tem->next[*p-'a'];
            p++;
        }
        return tem->flag;
    }
    //查询一个单词的操作
    bool search1(char *word)
    {
        Trie *tem = root;
        for(int i=0;word[i]!='';i++)
        {
            tem=tem->next[word[i]-'a'];
            if(tem->flag&&search2(&word[i+1]))
                return 1;
        }
        return 0;
    }
    
    int main(void)
    {
    
        int n=1;
        init();
        while(~scanf("%s",ss[n]))
            insert(ss[n]),n++;
        for(int i=1;i<n;i++)
        if(search1(ss[i]))
            printf("%s
    ",ss[i]);
        return 0;
    }
  • 相关阅读:
    使用postman模拟上传文件到springMVC的坑:the request was rejected because no multipart boundary was found
    win10 安装多个版本的jdk,如何切换
    String类的substring方法
    tomcat7.0配置CORS(跨域资源共享)
    win7下安装centos6.5后,开机无法进入选择双系统启动界面,只能启动centos的解决办法
    java位运算
    JDK源码--ArrayList浅析
    使用Jasperreporter生成入库出库单打印等报表操作
    centos6.5下安装zip格式的tomcat7和tomcat8,并同时运行
    Centos7配置文件共享服务器SAMBA三步曲(转)
  • 原文地址:https://www.cnblogs.com/weeping/p/5932318.html
Copyright © 2011-2022 走看看