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;
    }
  • 相关阅读:
    NFC性价比高频读卡器首选方案:FM17550
    关于ESP8266和ESP8285的对比
    有没有比NRF51822更好的智能穿戴蓝牙方案
    zigbee CC2530首选方案模组:TZU06A1
    PAN3501兼容AS3933-BTST
    NRF51822和NRF52832的主要区别
    集成模拟温度传感器低成本2.4G CC2500RGPR 中文手册
    USB2.0主机控制器 UPD720114 简单详解
    存储器HK1225-7EQ 使用说明书资料
    爬虫 + 数据
  • 原文地址:https://www.cnblogs.com/weeping/p/5932318.html
Copyright © 2011-2022 走看看