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): 9574    Accepted Submission(s): 3421


    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


    题意:推断一个单词能不能有两个单词组成,能够的话就输出。

    题解:全部单词建成一颗字典树,单词插入结尾记录单词的个数。查询时直接暴力拆单词推断。


    #include<cstring>
    #include<algorithm>
    #include<cstdio>
    #include<iostream>
    #include<cstdlib>
    
    #define N 50020
    
    using namespace std;
    
    char s[N][42];
    
    struct Trie {
        int num;
        struct Trie *nxt[26];
        Trie() {
            num=0;
            for(int i=0; i<26; i++) {
                nxt[i]=NULL;
            }
        }
    };
    
    void Trie_Inser(Trie *p,char s[]) {
        int i=0;
        Trie *q=p;
        while(s[i]) {
            int nx=s[i]-'a';
            if(q->nxt[nx]==NULL) {
                q->nxt[nx]=new Trie;
            }
            i++;
            q=q->nxt[nx];
        }
        q->num+=1;
    }
    
    bool Trie_Serch(Trie *p,char s[],int be,int en) {
        Trie *q=p;
        int i=be;
        while(i<=en) {
            int nx=s[i]-'a';
            if(q->nxt[nx]==NULL)return false;
            q=q->nxt[nx];
            i++;
        }
        if(q->num>0)return true;
        return 0;
    }
    
    int main() {
        //freopen("test.in","r",stdin);
        Trie *p=new Trie;
        int id=1;
        while(~scanf("%s",s[id])) {
            Trie_Inser(p,s[id]);
            id++;
        }
        for(int i=1; i<id; i++) {
            int len=strlen(s[i]);
            int be=0;
            if(len<=1)continue;
            for(int en=0; en<len-1; en++) {
                if(Trie_Serch(p,s[i],be,en)&&Trie_Serch(p,s[i],en+1,len-1)) {
                    printf("%s
    ",s[i]);
                    break;
                }
            }
        }
        return 0;
    }
    


  • 相关阅读:
    奇异值分解(SVD)详解及其应用
    注意力机制最新综述解读
    双目立体视觉的数学原理
    区域生长算法原理及MATLAB实现
    “error LNK2019: 无法解析的外部符号”的几种可能原因
    3D Slicer Adding MRML
    3D Slicer CreateModels-Module Analysis
    3D Slicer 结构的实例分析IGSReader
    3D Slicer VS-Qt5VSaddin-qt4.8.7dev
    3D Slicer Debug or Dev-170918
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5323614.html
Copyright © 2011-2022 走看看